Skip to content

Tell mypy et.al that Unset is always falsy #714

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

Closed
taasan opened this issue Dec 28, 2022 · 3 comments
Closed

Tell mypy et.al that Unset is always falsy #714

taasan opened this issue Dec 28, 2022 · 3 comments
Labels
✨ enhancement New feature or improvement 👋 good first issue Good for newcomers

Comments

@taasan
Copy link

taasan commented Dec 28, 2022

Is your feature request related to a problem? Please describe.

mypy doesn't understand that Unset is always falsy

if api_object.neighbours:
    for x in api_object.neighbours:  # <-- mypy: error: Item "Unset" of "Union[Unset, None, List[Neighbour]]" has no attribute "__iter__" (not iterable)  [union-attr]
        do_something_with(x)

Describe the solution you'd like

I would like mypy to understand this automatically

Type annotation on Unset.__bool__() could be Literal[False] instead of bool, but typing.Literal is not available for python < 3.8.

@taasan taasan added the ✨ enhancement New feature or improvement label Dec 28, 2022
@dbanty dbanty added the 👋 good first issue Good for newcomers label Jan 6, 2023
@dbanty
Copy link
Collaborator

dbanty commented Jan 6, 2023

Have you been able to test the type annotation on __bool__, and does it work? We could add that under a Python version check so that folks using newer versions will get the benefit (with an issue to ourselves to remove the check once we drop 3.7 support later this year)

@taasan
Copy link
Author

taasan commented Jan 9, 2023

I have tested this with mypy, and it does work

dbanty added a commit that referenced this issue Apr 30, 2023
…ents to check type) [#714, #752]. Thanks @taasan & @mcclurem! (#752)

* help mypy figure out that 'Unset' is always falsey

* chore: Regen tests

---------

Co-authored-by: Morgan McClure <[email protected]>
Co-authored-by: Dylan Anthony <[email protected]>
Co-authored-by: Dylan Anthony <[email protected]>
@dbanty
Copy link
Collaborator

dbanty commented Apr 30, 2023

Done in the version I'm releasing now!

@dbanty dbanty closed this as completed Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or improvement 👋 good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants