Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c11ce8

Browse files
authored
Disable --strict-equality inside functions that have type variables with value restrictions (#7304)
1 parent 4544705 commit 6c11ce8

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

mypy/checkexpr.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,19 @@ def dangerous_comparison(self, left: Type, right: Type,
20282028
"""
20292029
if not self.chk.options.strict_equality:
20302030
return False
2031+
if self.chk.binder.is_unreachable_warning_suppressed():
2032+
# We are inside a function that contains type variables with value restrictions in
2033+
# its signature. In this case we just suppress all strict-equality checks to avoid
2034+
# false positives for code like:
2035+
#
2036+
# T = TypeVar('T', str, int)
2037+
# def f(x: T) -> T:
2038+
# if x == 0:
2039+
# ...
2040+
# return x
2041+
#
2042+
# TODO: find a way of disabling the check only for types resulted from the expansion.
2043+
return False
20312044
if isinstance(left, NoneType) or isinstance(right, NoneType):
20322045
return False
20332046
if isinstance(left, UnionType) and isinstance(right, UnionType):

test-data/unit/check-expressions.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,18 @@ class Custom:
22592259
Custom() == int()
22602260
[builtins fixtures/bool.pyi]
22612261

2262+
[case testStrictEqualityDisabledWithTypeVarRestrictions]
2263+
# flags: --strict-equality
2264+
from typing import TypeVar
2265+
2266+
T = TypeVar('T', str, int)
2267+
2268+
def f(x: T) -> T:
2269+
if x == int(): # OK
2270+
...
2271+
return x
2272+
[builtins fixtures/bool.pyi]
2273+
22622274
[case testUnimportedHintAny]
22632275
def f(x: Any) -> None: # E: Name 'Any' is not defined \
22642276
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Any")

0 commit comments

Comments
 (0)