mirror of
https://github.com/home-assistant/core.git
synced 2025-08-01 03:35:09 +02:00
Check if requirement is typed in strict_typing IQS validation (#133415)
* Check if requirement is typed in strict_typing IQS validation * Apply suggestions from code review * Apply suggestions from code review * Return a list * Adjust * Improve
This commit is contained in:
@@ -4,6 +4,7 @@ https://developers.home-assistant.io/docs/core/integration-quality-scale/rules/s
|
||||
"""
|
||||
|
||||
from functools import lru_cache
|
||||
from importlib import metadata
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
@@ -24,6 +25,29 @@ def _strict_typing_components(strict_typing_file: Path) -> set[str]:
|
||||
)
|
||||
|
||||
|
||||
def _check_requirements_are_typed(integration: Integration) -> list[str]:
|
||||
"""Check if all requirements are typed."""
|
||||
invalid_requirements = []
|
||||
for requirement in integration.requirements:
|
||||
requirement_name, requirement_version = requirement.split("==")
|
||||
# Remove any extras
|
||||
requirement_name = requirement_name.split("[")[0]
|
||||
try:
|
||||
distribution = metadata.distribution(requirement_name)
|
||||
except metadata.PackageNotFoundError:
|
||||
# Package not installed locally
|
||||
continue
|
||||
if distribution.version != requirement_version:
|
||||
# Version out of date locally
|
||||
continue
|
||||
|
||||
if not any(file for file in distribution.files if file.name == "py.typed"):
|
||||
# no py.typed file
|
||||
invalid_requirements.append(requirement)
|
||||
|
||||
return invalid_requirements
|
||||
|
||||
|
||||
def validate(
|
||||
config: Config, integration: Integration, *, rules_done: set[str]
|
||||
) -> list[str] | None:
|
||||
@@ -35,4 +59,9 @@ def validate(
|
||||
"Integration does not have strict typing enabled "
|
||||
"(is missing from .strict-typing)"
|
||||
]
|
||||
if untyped_requirements := _check_requirements_are_typed(integration):
|
||||
return [
|
||||
f"Requirements {untyped_requirements} do not conform PEP 561 (https://peps.python.org/pep-0561/)",
|
||||
"They should be typed and have a 'py.typed' file",
|
||||
]
|
||||
return None
|
||||
|
Reference in New Issue
Block a user