You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Binder should reason about complex expressions #2199
changed the title [-]None checking (binder?) should reason about complex expressions[/-][+]Binder should reason about complex expressions[/+]on Sep 30, 2016
Actually, these things are pretty difficult to do correctly/safely, because you have to worry about e.g.:
fromtypingimport*a= ['x', None] # type: List[Optional[str]]foriinrange(len(a)):
ifa[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 --strict-optional, and that's tedious.
The binder already isn't safe, so this wouldn't fundamentally change anything. The binder currently seems to handle a[0] but not a[i]. Adding support for the latter hopefully wouldn't be hard. [These only seem to work with isinstance tests. A test such as if a[0]: doesn't seem to be supported, but I assume that it wouldn't be hard to fix.]
Example of unsafety:
from typing import *
a = ['x', 1] # type: List[Union[int, str]]
for i in range(len(a)):
if isinstance(a[0], int):
a[i] = 'x'
reveal_type(a[0]) # int, even though it's clearly str
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.
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 --strict-optional, and that's tedious.
There is a lot of code like this in mypy itself: if x[i] is not None: ..., if x.attr is not None: ..., and None not in x (for collections). I think it makes sense to raise priority to normal here.
Activity
Fix strict none errors in the test subpackage.
ddfisher commentedon Sep 30, 2016
Yeah, this looks like a general binder feature to me.
[-]None checking (binder?) should reason about complex expressions[/-][+]Binder should reason about complex expressions[/+]ddfisher commentedon Sep 30, 2016
Actually, these things are pretty difficult to do correctly/safely, because you have to worry about e.g.:
gvanrossum commentedon Sep 30, 2016
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
--strict-optional, and that's tedious.Add mypy.ini. Fix strict none errors in the test subpackage. (#2200)
JukkaL commentedon Oct 4, 2016
The binder already isn't safe, so this wouldn't fundamentally change anything. The binder currently seems to handle
a[0]but nota[i]. Adding support for the latter hopefully wouldn't be hard. [These only seem to work withisinstancetests. A test such asif a[0]:doesn't seem to be supported, but I assume that it wouldn't be hard to fix.]Example of unsafety:
gvanrossum commentedon Oct 4, 2016
JukkaL commentedon Oct 4, 2016
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.
ilevkivskyi commentedon May 2, 2017
There is a lot of code like this in mypy itself:
if x[i] is not None: ...,if x.attr is not None: ..., andNone not in x(for collections). I think it makes sense to raise priority to normal here.