Skip to content

Commit

Permalink
Enable pydantic mypy plugin (#87415)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Feb 9, 2023
1 parent 2d2ff19 commit 67499e0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ on:
env:
CACHE_VERSION: 5
PIP_CACHE_VERSION: 4
MYPY_CACHE_VERSION: 3
MYPY_CACHE_VERSION: 4
HA_SHORT_VERSION: 2023.3
DEFAULT_PYTHON: "3.10"
ALL_PYTHON_VERSIONS: "['3.10']"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/lametric/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async def _async_step_create_entry(self, host: str, api_key: str) -> FlowResult:
model=Model(
cycles=2,
frames=[Simple(text="Connected to Home Assistant!", icon=7956)],
sound=Sound(id=NotificationSound.WIN),
sound=Sound(sound=NotificationSound.WIN),
),
)
)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/lametric/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def async_send_message(self, message: str = "", **kwargs: Any) -> None:

sound = None
if CONF_SOUND in data:
sound = Sound(id=data[CONF_SOUND], category=None)
sound = Sound(sound=data[CONF_SOUND], category=None)

notification = Notification(
icon_type=NotificationIconType(data.get(CONF_ICON_TYPE, "none")),
Expand Down
7 changes: 3 additions & 4 deletions homeassistant/components/lametric/services.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Support for LaMetric time services."""
from __future__ import annotations

from collections.abc import Sequence

from demetriek import (
AlarmSound,
Chart,
Goal,
LaMetricError,
Model,
Notification,
Expand Down Expand Up @@ -113,12 +112,12 @@ async def _async_service_message(call: ServiceCall) -> None:
async def async_send_notification(
coordinator: LaMetricDataUpdateCoordinator,
call: ServiceCall,
frames: Sequence[Chart | Simple],
frames: list[Chart | Goal | Simple],
) -> None:
"""Send a notification to an LaMetric device."""
sound = None
if CONF_SOUND in call.data:
sound = Sound(id=call.data[CONF_SOUND], category=None)
sound = Sound(sound=call.data[CONF_SOUND], category=None)

notification = Notification(
icon_type=NotificationIconType(call.data[CONF_ICON_TYPE]),
Expand Down
7 changes: 7 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

[mypy]
python_version = 3.10
plugins = pydantic.mypy
show_error_codes = true
follow_imports = silent
ignore_missing_imports = true
Expand All @@ -26,6 +27,12 @@ disallow_untyped_defs = true
warn_return_any = true
warn_unreachable = true

[pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true

[mypy-homeassistant.*]
no_implicit_reexport = true

Expand Down
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ freezegun==1.2.2
mock-open==1.4.0
mypy==1.0.0
pre-commit==3.0.0
pydantic==1.10.4
pylint==2.16.0
pylint-per-file-ignores==1.1.0
pipdeptree==2.3.1
Expand Down
22 changes: 21 additions & 1 deletion script/hassfest/mypy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

GENERAL_SETTINGS: Final[dict[str, str]] = {
"python_version": ".".join(str(x) for x in REQUIRED_PYTHON_VER[:2]),
"plugins": ", ".join(["pydantic.mypy"]),
"show_error_codes": "true",
"follow_imports": "silent",
# Enable some checks globally.
Expand Down Expand Up @@ -84,6 +85,18 @@
"disallow_any_generics",
]

# Plugin specific settings
# Bump mypy cache when updating! Some plugins don't invalidate the cache properly.
# pydantic: https://docs.pydantic.dev/mypy_plugin/#plugin-settings
PLUGIN_CONFIG: Final[dict[str, dict[str, str]]] = {
"pydantic-mypy": {
"init_forbid_extra": "true",
"init_typed": "true",
"warn_required_dynamic_aliases": "true",
"warn_untyped_fields": "true",
}
}


def _strict_module_in_ignore_list(
module: str, ignored_modules_set: set[str]
Expand Down Expand Up @@ -132,7 +145,7 @@ def _generate_and_validate_strict_typing(config: Config) -> str:
return "\n".join(_sort_within_sections(lines)) + "\n"


def _generate_and_validate_mypy_config(config: Config) -> str:
def _generate_and_validate_mypy_config(config: Config) -> str: # noqa: C901
"""Validate and generate mypy config."""

# Filter empty and commented lines.
Expand Down Expand Up @@ -199,6 +212,13 @@ def _generate_and_validate_mypy_config(config: Config) -> str:
for key in STRICT_SETTINGS:
mypy_config.set(general_section, key, "true")

for plugin_name, plugin_config in PLUGIN_CONFIG.items():
if not plugin_config:
continue
mypy_config.add_section(plugin_name)
for key, value in plugin_config.items():
mypy_config.set(plugin_name, key, value)

# By default enable no_implicit_reexport only for homeassistant.*
# Disable it afterwards for all components
components_section = "mypy-homeassistant.*"
Expand Down

0 comments on commit 67499e0

Please sign in to comment.