-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy doesn't understand that Optional is no longer Optional in an 'if' clause #8000
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
Comments
Hi Yury! You need to do There has been previous discussion about allowing |
We do support narrowing with `if var`. The issue here is that the use is in
a lambda, which could get called after the value is changed.
…On Thu, Nov 21, 2019, 16:01 Ethan Smith ***@***.***> wrote:
Hi Yury! You need to do if self.ignore is None for mypy to pick up that
you want self.ignore to be non-None.
There has been previous discussion about allowing if var to narrow var,
but I believe we decided to not go down that route as it could get
complicated?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8000?email_source=notifications&email_token=AACTC7NF4ZSVQ3DDW2RJ6WDQU4OOFA5CNFSM4JQJQVSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEE4CEVA#issuecomment-557326932>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACTC7MJRHLA6IMTB5GUEIDQU4OOFANCNFSM4JQJQVSA>
.
|
Hi!
Yeah, changing to
Upd: Ideally mypy should infer that the lambda is used synchronously in this case. Would it be possible to fix this? Or should our code be rewritten to close over a temp variable? |
Oh huh, I must have missed that, apologies! |
Definitelly possible, but it may be non-trivial and I am not sure we will have time for this, but PRs are very welcome.
Yep, a temp variable is a quick and simple workaround. |
Thank you guys for a quick turnaround! |
Alternatively, adding an assertion also seems to satisfy mypy. Is that reliable? I'm finding this problem generally emerges when an instance variable must be initialized to |
Yes, an assertion works well. For the instance variable thing you can lie to mypy and just give it a non-None type and use type ignore. Then it's on you to make sure your init logic is right. Alternatively, you could use a @Property that contains an assert and returns a non-Optional type |
mypy doesn't understand that Optional is no longer Optional in an 'if' clause:
test.py:
The text was updated successfully, but these errors were encountered: