Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,19 @@ def dangerous_comparison(self, left: Type, right: Type,
"""
if not self.chk.options.strict_equality:
return False
if self.chk.binder.is_unreachable_warning_suppressed():
# We are inside a function that contains type variables with value restrictions in
# its signature. In this case we just suppress all strict-equality checks to avoid
# false positives for code like:
#
# T = TypeVar('T', str, int)
# def f(x: T) -> T:
# if x == 0:
# ...
# return x
#
# TODO: find a way of disabling the check only for types resulted from the expansion.
return False
if isinstance(left, NoneType) or isinstance(right, NoneType):
return False
if isinstance(left, UnionType) and isinstance(right, UnionType):
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,18 @@ class Custom:
Custom() == int()
[builtins fixtures/bool.pyi]

[case testStrictEqualityDisabledWithTypeVarRestrictions]
# flags: --strict-equality
from typing import TypeVar

T = TypeVar('T', str, int)

def f(x: T) -> T:
if x == int(): # OK
...
return x
[builtins fixtures/bool.pyi]

[case testUnimportedHintAny]
def f(x: Any) -> None: # E: Name 'Any' is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Any")
Expand Down