Skip to content

Match statement syntax error does not obey version guards #19923

@wyattscarpenter

Description

@wyattscarpenter

This phenomenon was originally discovered by @dmarteau in #19892.

I wrote and ran the following two tests under Python 3.12 and mypy master (1.19.0+dev) and they succeeded.

[case testUnreachableAfterToplevelAssertFlaggy]
# flags: --python-version=3.9 --show-error-codes
# This test may false negative succeed if you are actually running python 3.9
import sys
reveal_type(0)  # N: Revealed type is "Literal[0]?"
x: int | tuple[int]  # E: X | Y syntax for unions requires Python 3.10  [syntax]
"a"+1  # E: Unsupported operand types for + ("str" and "int")  [operator]
assert sys.version_info > (3,9)
reveal_type('')  # No error here :-)
x: int | tuple[int] # a little 3.10 syntax to check
[builtins fixtures/ops.pyi]

[case testUnreachableAfterToplevelAssertConfigy]
# This test may false negative succeed if you are act>
# flags: --config-file=tmp/foo.ini --show-error-codes
import sys
reveal_type(0)  # N: Revealed type is "Literal[0]?"
x: int | tuple[int]  # E: X | Y syntax for unions requires Python 3.10  [syntax]
"a"+1  # E: Unsupported operand types for + ("str" and "int")  [operator]
assert sys.version_info > (3,9)
reveal_type('')  # No error here :-)
x: int | tuple[int] # a little 3.10 syntax to check

[file foo.ini]
\[mypy]
python_version=3.9

[builtins fixtures/ops.pyi]

However, swapping in a match statement instead of a union bar gets rid of all the other errors and produces

error: Pattern matching is only supported in Python 3.10 and greater  [syntax]

Weird!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions