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
--strict-equality should be disabled in functions with constrained type variables #6735
deffoo(x: AnyStr) ->AnyStr:
ifx=='': # False positive here
...
returnx
With --strict-equality this produces a "Non-overlapping equality check ...", because the function body is checked multiple times with both values of AnyStr.
The way I look at it, this flag makes comparisons more like other operators. E.g. a + 0 is invalid when a has type str. And if we have
T=TypeVar('T', int, str)
deffoo(a: T):
a+0
that's an error because for some value of T, a + 0 is a type error.
Similarly, I expect that when we have this flag, if the function has a == 0, it would be considered an error. And hence also for AnyStr. (And in all likelihood, that example does have a bug.)
I admit that the second example is more reasonable (and presumably is more like the real-world case you encountered).
But it still feels a case of "two wrongs can make a right", and pragmatically I'd expect that such cases are outnumbered by cases where the comparison if AnyStr to a concrete str (or bytes) is a bug, just like writing return '' would be a bug in that function.
(and presumably is more like the real-world case you encountered)
No, the real world use case was like:
X=TypeVar('X', One, Two, Three)
deffoo(x: X) ->Tuple[X, str]:
ifx==One('special value'):
raiseValueError('Do not do foo with special value!')
returnx, bar(x)
and pragmatically I'd expect that such cases are outnumbered by cases where the comparison if AnyStr to a concrete str (or bytes) is a bug
So far I didn't find any actual bugs using current semantics, but found some false positives.
Type variable with value restriction does not seem to be a very commonly used feature (outside stdlib stubs), so even a small number of false positives can indicate a pretty high false positive rate. In particular, Ivan's experiment where this came up targeted a pretty small codebase, I think.
Activity
gvanrossum commentedon Apr 27, 2019
Why is that a false positive? It is broken for bytes.
ilevkivskyi commentedon Apr 27, 2019
This flag indicates comparisons that can never be True, while this one definitely can be True.
gvanrossum commentedon Apr 27, 2019
Did this come up in a real world use case?
The way I look at it, this flag makes comparisons more like other operators. E.g.
a + 0is invalid whenahas typestr. And if we havethat's an error because for some value of
T,a + 0is a type error.Similarly, I expect that when we have this flag, if the function has
a == 0, it would be considered an error. And hence also forAnyStr. (And in all likelihood, that example does have a bug.)ilevkivskyi commentedon Apr 27, 2019
Yes.
This is a bad analogy.
What about
Mypy shows two errors here, but to me this looks perfectly OK.
gvanrossum commentedon Apr 27, 2019
I admit that the second example is more reasonable (and presumably is more like the real-world case you encountered).
But it still feels a case of "two wrongs can make a right", and pragmatically I'd expect that such cases are outnumbered by cases where the comparison if
AnyStrto a concretestr(orbytes) is a bug, just like writingreturn ''would be a bug in that function.ilevkivskyi commentedon Apr 27, 2019
No, the real world use case was like:
So far I didn't find any actual bugs using current semantics, but found some false positives.
JukkaL commentedon Apr 29, 2019
Type variable with value restriction does not seem to be a very commonly used feature (outside stdlib stubs), so even a small number of false positives can indicate a pretty high false positive rate. In particular, Ivan's experiment where this came up targeted a pretty small codebase, I think.
gvanrossum commentedon Apr 29, 2019