-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Binder should reason about complex expressions #2199
Comments
This shows several different approaches to dealing with #2199 for p[i].arg.
Yeah, this looks like a general binder feature to me. |
Actually, these things are pretty difficult to do correctly/safely, because you have to worry about e.g.: from typing import *
a = ['x', None] # type: List[Optional[str]]
for i in range(len(a)):
if a[i]:
a[0] = None # how do we detect something like this?
print(i, '<' + a[i] + '>') |
I agree it's complicated (and it would be even more complicated if we considered other threads). I'm just pointing out that if we have a lot of code like this (and I've seen a lot of it in Dropbox repos) is all needs to be refactored just to shut up |
* Fix strict none errors in the test subpackage. This shows how to deal with #2199 for `p[i].arg`. * Run mypy over itself in --strict-optional mode. Add mypy.ini to suppress most errors (for now).
The binder already isn't safe, so this wouldn't fundamentally change anything. The binder currently seems to handle Example of unsafety:
|
How feasible would it be to make the binder notice such a potentially
conflicting assignments and undo what it has learned about a[0]? (I guess
this could be made arbitrarily difficult by trying to consider all forms of
aliasing, but I'm not asking for that -- just simple cases. I expect that
cases like this last example are rare but do occur. I also assume that most
cases where an inference about a complex expression is made and then used
later cover only a very small area (often just within one test, e.g. `if
a[i].arg is not None and valid(a[i].arg):`).
|
I have no idea. We could perhaps generate a sample of such things from a large corpus and manually analyze them. I doubt we can reason this from first principles. |
There is a lot of code like this in mypy itself: |
Here's a very simple example:
We get an error on the last line:
Of course I can refactor this by using
but that's just busy-work and doesn't necessarily make the code more readable.
(I found this in some mypy test code where the actual expression was
p[i].arg
so this probably needs to become fairly powerful.)The text was updated successfully, but these errors were encountered: