Add capsys to enforce-type-hints plugin (#120653)

pull/120684/head^2
epenet 2024-06-27 19:54:44 +02:00 committed by GitHub
parent eb2d2ce1b3
commit f4b76406f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 10 deletions

View File

@ -79,7 +79,7 @@ _INNER_MATCH_POSSIBILITIES = [i + 1 for i in range(5)]
_TYPE_HINT_MATCHERS.update(
{
f"x_of_y_{i}": re.compile(
rf"^(\w+)\[{_INNER_MATCH}" + f", {_INNER_MATCH}" * (i - 1) + r"\]$"
rf"^([\w\.]+)\[{_INNER_MATCH}" + f", {_INNER_MATCH}" * (i - 1) + r"\]$"
)
for i in _INNER_MATCH_POSSIBILITIES
}
@ -102,6 +102,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"area_registry": "AreaRegistry",
"async_setup_recorder_instance": "RecorderInstanceGenerator",
"caplog": "pytest.LogCaptureFixture",
"capsys": "pytest.CaptureFixture[str]",
"current_request_with_host": "None",
"device_registry": "DeviceRegistry",
"enable_bluetooth": "None",

View File

@ -2,6 +2,8 @@
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.srp_energy.const import CONF_IS_TOU, DOMAIN
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_SOURCE, CONF_USERNAME
@ -23,8 +25,9 @@ from . import (
from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_srp_energy_config_flow")
async def test_show_form(
hass: HomeAssistant, mock_srp_energy_config_flow: MagicMock, capsys
hass: HomeAssistant, capsys: pytest.CaptureFixture[str]
) -> None:
"""Test show configuration form."""
result = await hass.config_entries.flow.async_init(
@ -140,7 +143,7 @@ async def test_flow_entry_already_configured(
async def test_flow_multiple_configs(
hass: HomeAssistant, init_integration: MockConfigEntry, capsys
hass: HomeAssistant, init_integration: MockConfigEntry
) -> None:
"""Test multiple config entries."""
# Verify mock config setup from fixture

View File

@ -55,6 +55,7 @@ def test_regex_get_module_platform(
("list[dict[str, Any]]", 1, ("list", "dict[str, Any]")),
("tuple[bytes | None, str | None]", 2, ("tuple", "bytes | None", "str | None")),
("Callable[[], TestServer]", 2, ("Callable", "[]", "TestServer")),
("pytest.CaptureFixture[str]", 1, ("pytest.CaptureFixture", "str")),
],
)
def test_regex_x_of_y_i(
@ -1264,6 +1265,7 @@ def test_pytest_fixture(linter: UnittestLinter, type_hint_checker: BaseChecker)
def sample_fixture( #@
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
capsys: pytest.CaptureFixture[str],
aiohttp_server: Callable[[], TestServer],
unused_tcp_port_factory: Callable[[], int],
enable_custom_integrations: None,

View File

@ -6,6 +6,7 @@ from typing import Any
from unittest.mock import Mock, patch
import pytest
from typing_extensions import Generator
from homeassistant.auth.providers import homeassistant as hass_auth
from homeassistant.core import HomeAssistant
@ -15,7 +16,7 @@ from tests.common import register_auth_provider
@pytest.fixture(autouse=True)
def reset_log_level():
def reset_log_level() -> Generator[None]:
"""Reset log level after each test case."""
logger = logging.getLogger("homeassistant.core")
orig_level = logger.level
@ -24,7 +25,7 @@ def reset_log_level():
@pytest.fixture
def provider(hass):
def provider(hass: HomeAssistant) -> hass_auth.HassAuthProvider:
"""Home Assistant auth provider."""
provider = hass.loop.run_until_complete(
register_auth_provider(hass, {"type": "homeassistant"})
@ -33,7 +34,11 @@ def provider(hass):
return provider
async def test_list_user(hass: HomeAssistant, provider, capsys) -> None:
async def test_list_user(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test we can list users."""
data = provider.data
data.add_auth("test-user", "test-pass")
@ -47,7 +52,10 @@ async def test_list_user(hass: HomeAssistant, provider, capsys) -> None:
async def test_add_user(
hass: HomeAssistant, provider, capsys, hass_storage: dict[str, Any]
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
) -> None:
"""Test we can add a user."""
data = provider.data
@ -64,7 +72,11 @@ async def test_add_user(
data.validate_login("paulus", "test-pass")
async def test_validate_login(hass: HomeAssistant, provider, capsys) -> None:
async def test_validate_login(
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test we can validate a user login."""
data = provider.data
data.add_auth("test-user", "test-pass")
@ -89,7 +101,10 @@ async def test_validate_login(hass: HomeAssistant, provider, capsys) -> None:
async def test_change_password(
hass: HomeAssistant, provider, capsys, hass_storage: dict[str, Any]
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
) -> None:
"""Test we can change a password."""
data = provider.data
@ -108,7 +123,10 @@ async def test_change_password(
async def test_change_password_invalid_user(
hass: HomeAssistant, provider, capsys, hass_storage: dict[str, Any]
hass: HomeAssistant,
provider: hass_auth.HassAuthProvider,
capsys: pytest.CaptureFixture[str],
hass_storage: dict[str, Any],
) -> None:
"""Test changing password of non-existing user."""
data = provider.data