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
18 changes: 16 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3800,8 +3800,22 @@ def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
elif operator == "is" or operator == "is not":
right_type = self.accept(right) # validate the right operand
sub_result = self.bool_type()
if not self.chk.can_skip_diagnostics and self.dangerous_comparison(
left_type, right_type, identity_check=True
if (
not self.chk.can_skip_diagnostics
and self.dangerous_comparison(left_type, right_type, identity_check=True)
# Allow dangerous identity comparisons with objects explicitly typed as Any
and not (
isinstance(left, NameExpr)
and isinstance(left.node, Var)
and not left.node.is_inferred
and isinstance(get_proper_type(left.node.type), AnyType)
)
and not (
isinstance(right, NameExpr)
and isinstance(right.node, Var)
and not right.node.is_inferred
and isinstance(get_proper_type(right.node.type), AnyType)
)
):
# Show the most specific literal types possible
left_type = try_getting_literal(left_type)
Expand Down
45 changes: 45 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,51 @@ if 1 in ('x', 'y'): # E: Non-overlapping container check (element type: "int",
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testStrictEqualityWithAnySentinel]
# flags: --strict-equality
from __future__ import annotations
from typing import Any, cast

class A: ...
class B: ...

sentinel: Any = object()

def f1(a: A = sentinel, b: B = sentinel):
if a is sentinel and b is sentinel:
raise


def f2(a: A | None = sentinel, b: B | None = sentinel):
if a is sentinel and b is sentinel:
raise


sentinel_strict = object()

def f3(a: A = sentinel_strict, b: B = sentinel_strict): # E: Incompatible default for parameter "a" (default has type "object", parameter has type "A") \
# E: Incompatible default for parameter "b" (default has type "object", parameter has type "B")
if a is sentinel_strict and b is sentinel_strict: # E: Non-overlapping identity check (left operand type: "B", right operand type: "A")
raise


def f4(a: A | None = sentinel_strict, b: B | None = sentinel_strict): # E: Incompatible default for parameter "a" (default has type "object", parameter has type "A | None") \
# E: Incompatible default for parameter "b" (default has type "object", parameter has type "B | None")
if a is sentinel_strict and b is sentinel_strict: # E: Non-overlapping identity check (left operand type: "B | None", right operand type: "A | None")
raise


sentinel_inferred = cast(Any, object())

def f5(a: A = sentinel_inferred, b: B = sentinel_inferred):
if a is sentinel_inferred and b is sentinel_inferred: # E: Non-overlapping identity check (left operand type: "B", right operand type: "A")
raise

def f6(a: A | None = sentinel_inferred, b: B | None = sentinel_inferred):
if a is sentinel_inferred and b is sentinel_inferred: # E: Non-overlapping identity check (left operand type: "B | None", right operand type: "A | None")
raise
[builtins fixtures/bool.pyi]

[case testOverlappingAnyTypeWithoutStrictOptional]
# flags: --no-strict-optional --strict-equality
from typing import Any, Optional
Expand Down
Loading