Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disabled overlap checks in assert #17984

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4725,7 +4725,11 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
self.check_final(s)

def visit_assert_stmt(self, s: AssertStmt) -> None:
self.expr_checker.accept(s.expr)
# Disable comparison overlap checks on assert statements to prevent false positives
with self.msg.filter_errors(
filter_errors=lambda name, info: info.code == codes.COMPARISON_OVERLAP
):
self.expr_checker.accept(s.expr)

if isinstance(s.expr, TupleExpr) and len(s.expr.items) > 0:
self.fail(message_registry.MALFORMED_ASSERT, s)
Expand All @@ -4736,6 +4740,10 @@ def visit_assert_stmt(self, s: AssertStmt) -> None:
self.expr_checker.analyze_cond_branch(else_map, s.msg, None)
self.push_type_map(true_map)

# Disable unreachable warning on assert statements to prevent false positives
if not true_map:
self.binder.suppress_unreachable_warnings()

def visit_raise_stmt(self, s: RaiseStmt) -> None:
"""Type check a raise statement."""
if s.expr:
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,7 @@ assert a == b

R2 = Dict[int, R2]
c: R2
assert a == c # E: Non-overlapping equality check (left operand type: "Dict[str, R]", right operand type: "Dict[int, R2]")
assert a == c
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ def test2(switch: FlipFlopEnum) -> None:

switch.mutate()

assert switch.state == State.B # E: Non-overlapping equality check (left operand type: "Literal[State.A]", right operand type: "Literal[State.B]")
reveal_type(switch.state) # E: Statement is unreachable
assert switch.state == State.B
reveal_type(switch.state)

def test3(switch: FlipFlopEnum) -> None:
# Same thing, but using 'is' comparisons. Previously mypy's behaviour differed
Expand All @@ -739,8 +739,8 @@ def test3(switch: FlipFlopEnum) -> None:

switch.mutate()

assert switch.state is State.B # E: Non-overlapping identity check (left operand type: "Literal[State.A]", right operand type: "Literal[State.B]")
reveal_type(switch.state) # E: Statement is unreachable
assert switch.state is State.B
reveal_type(switch.state)
[builtins fixtures/primitives.pyi]

[case testNarrowingEqualityRequiresExplicitStrLiteral]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ else:
def foo(c: int) -> None:
reveal_type(c) # N: Revealed type is "builtins.int"
assert not isinstance(c, int)
reveal_type(c) # E: Statement is unreachable
reveal_type(c)
[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagWithBadControlFlow4]
Expand Down Expand Up @@ -1338,7 +1338,7 @@ def test_typed_fn(obj) -> None:
obj.reload()

assert obj.prop is False
reveal_type(obj.prop) # E: Statement is unreachable
reveal_type(obj.prop)

[case testUnreachableCheckedUntypedFunction]
# flags: --warn-unreachable --check-untyped-defs
Expand All @@ -1350,7 +1350,7 @@ def test_untyped_fn(obj):
obj.reload()

assert obj.prop is False
reveal_type(obj.prop) # E: Statement is unreachable
reveal_type(obj.prop)

[case testConditionalTypeVarException]
# every part of this test case was necessary to trigger the crash
Expand Down
Loading