Closed autinerd closed 7 months ago
You may want to have a
be a TypedDict
, something like
from typing import TypedDict, NotRequired, Any
class A_Type(TypedDict):
services: NotRequired[list[dict[str, Any]]]
def fun(a: A_Type) -> list[dict[str, Any]]:
services: list[dict[str, Any]]
if "services" in a:
return a["services"]
# services being assigned other ways
services = []
return services
In my case it is too versatile to have a TypedDict. https://github.com/home-assistant/core/pull/114528/files
hass.data
is a dict which contains all kinds of different stuff for each integration/platform etc. in Home Assistant. There is currently work done to improve the typing topic, but until then we need these type annotations.
I'm curious if you could do:
services: list[dict[str, Any]] = a["services"]
return services
That might be easier to "detect" / allow.
I'm curious if you could do:
services: list[dict[str, Any]] = a["services"] return services
That might be easier to "detect" / allow.
This is already working without issue, but then you have to annotate the variable multiple times.
Oh, hah, I didn't realize that. I think we can support the variant from your first post.
Hello together,
When introducing the RET rules to Home Assistant I currently have this case (simplified):
where RET504 is flagging, but when fixed mypy is complaining because of the Any type. The current options are 1. use
cast
, 2. declare the type again or 3. add a# noqa
.It would be cool that when a type declaration of the variable is found, that this assignment followed by a return would not get flagged.