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

RET504 "false positive" when using variables for typing #10732

Closed
autinerd opened this issue Apr 2, 2024 · 5 comments · Fixed by #10741
Closed

RET504 "false positive" when using variables for typing #10732

autinerd opened this issue Apr 2, 2024 · 5 comments · Fixed by #10741
Assignees
Labels
bug Something isn't working

Comments

@autinerd
Copy link
Contributor

autinerd commented Apr 2, 2024

Hello together,

When introducing the RET rules to Home Assistant I currently have this case (simplified):

def fun(a: dict[str, Any]) -> list[dict[str, Any]:
    services: list[dict[str, Any]]
    if "services" in a:
        services = a["services"]
        return services
    ... # services is further assigned and used

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.

@diceroll123
Copy link
Contributor

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

@autinerd
Copy link
Contributor Author

autinerd commented Apr 2, 2024

In my case it is too versatile to have a TypedDict. https://github.com/home-assistant/core/pull/114528/files

https://github.com/home-assistant/core/pull/114528/files#diff-da81d27b5f83f07ead4bcbb0f29527511a2d7985acbe1cae45636e28202857ccR63-R74

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.

@charliermarsh charliermarsh added the bug Something isn't working label Apr 2, 2024
@charliermarsh
Copy link
Member

I'm curious if you could do:

services: list[dict[str, Any]] = a["services"]
return services

That might be easier to "detect" / allow.

@autinerd
Copy link
Contributor Author

autinerd commented Apr 2, 2024

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.

@charliermarsh
Copy link
Member

Oh, hah, I didn't realize that. I think we can support the variant from your first post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants