Use precise types instead of Any, and avoid unnecessary typing.cast by relying on type-guard utilities and existing type narrowing. In tests, prefer the concrete pytest types/fixtures (e.g., pytest.MonkeyPatch) over broad Any.
Apply this as a style rule:
Any when a concrete type exists.typing.cast purely to satisfy typing after runtime checks, prefer type-guard helpers (e.g., is_dict, is_list) that narrow types.Example:
import pytest
import typing as t
from .typing_utils import is_dict, is_list
@pytest.mark.respx()
def test_something(monkeypatch: pytest.MonkeyPatch) -> None:
...
def _deep_merge_extra_fields(existing: object, new: object) -> object:
if is_dict(existing) and is_dict(new):
# type is narrowed by is_dict
for k, v in new.items():
...
return existing
if is_list(existing) and is_list(new):
# type is narrowed by is_list
existing.extend(new)
return existing
return new
Enter the URL of a public GitHub repository