Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to template result wrapper #82575

Merged
merged 4 commits into from Nov 24, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions homeassistant/helpers/template.py
Expand Up @@ -168,10 +168,10 @@ class ResultWrapper:
render_result: str | None


def gen_result_wrapper(kls):
def gen_result_wrapper(kls: type[set | list | dict]) -> type:
"""Generate a result wrapper."""

class Wrapper(kls, ResultWrapper):
class Wrapper(kls, ResultWrapper): # type: ignore[valid-type,misc]
jbouwh marked this conversation as resolved.
Show resolved Hide resolved
"""Wrapper of a kls that can store render_result."""

def __init__(self, *args: Any, render_result: str | None = None) -> None:
Expand All @@ -184,7 +184,7 @@ def __str__(self) -> str:
if kls is set:
return str(set(self))

return cast(str, kls.__str__(self))
return kls.__str__(self)

return self.render_result

Expand Down Expand Up @@ -212,10 +212,8 @@ def __str__(self) -> str:
return self.render_result


RESULT_WRAPPERS: dict[type, type] = {
kls: gen_result_wrapper(kls) # type: ignore[no-untyped-call]
for kls in (list, dict, set)
}
_types: tuple[type[dict | list | set], ...] = (dict, list, set)
RESULT_WRAPPERS: dict[type, type] = {kls: gen_result_wrapper(kls) for kls in _types}
Comment on lines +215 to +216
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, mypy recognises the tuple as tuple[ABCMeta, ...]

RESULT_WRAPPERS[tuple] = TupleWrapper


Expand Down