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
After Optional[str] == 'literal', infer that it's not optional #1825
We should probably fix this more generally with Unions:
deff(a: Union[int, str]) ->strifa=='<string>': returna# mypy should understand that a must be a string hereelse: returnstr(a)
Are we okay with saying that equality must imply some sort of type equivalence? (In particular, that a == b implies a is either a super- or sub-class of b.)
I was more thinking that the join of the two types should not be object.
We should carefully introduce and test such a change because I expect it'll cause some noise in various large projects (most of which I expect to be questionable code in those projects, but some might be due to our rules needing some exception, and either wait it'll take a while to sort it all out).
FWIW the same rule should apply to x in c and x not in c -- the type of x and the type of c's items must have a non-trivial join.
Asuming that on a == 'text', a is string is somewhat unsafe. There's a slight danger given that == can be redefined, so I could have a class with an __eq__ method that considers itself equal to string but doesn't have other string properties. A similar problem occurs with "in" given that it depends on __eq__.
The only operator where I think this could safely be done is "is" and "is not", but that doesn't cover the example.
In general that assumption is unsafe, but here a has type Optional[str] = Union[None, str] and it's quite reasonable to assume that == will not be redefined on None.
Activity
ddfisher commentedon Jul 21, 2016
We should probably fix this more generally with Unions:
Are we okay with saying that equality must imply some sort of type equivalence? (In particular, that
a == bimpliesais either a super- or sub-class ofb.)gvanrossum commentedon Jul 21, 2016
I was more thinking that the join of the two types should not be object.
We should carefully introduce and test such a change because I expect it'll cause some noise in various large projects (most of which I expect to be questionable code in those projects, but some might be due to our rules needing some exception, and either wait it'll take a while to sort it all out).
FWIW the same rule should apply to
x in candx not in c-- the type of x and the type of c's items must have a non-trivial join.dmoisset commentedon Aug 5, 2016
Asuming that on
a == 'text', a is string is somewhat unsafe. There's a slight danger given that == can be redefined, so I could have a class with an__eq__method that considers itself equal to string but doesn't have other string properties. A similar problem occurs with "in" given that it depends on__eq__.The only operator where I think this could safely be done is "is" and "is not", but that doesn't cover the example.
rwbarton commentedon Aug 5, 2016
In general that assumption is unsafe, but here
ahas typeOptional[str] = Union[None, str]and it's quite reasonable to assume that==will not be redefined onNone.