[2.1 regression] Incompatible types in capture pattern, can't assign a variable when using string unpacking
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
After upgrading to specifically MyPy 2.1.0 from 2.0.0 on Python 3.13.13 causes a Incompatible types in capture pattern (pattern captures type "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']") [misc] error.
Looking at your release page (https://mypy.readthedocs.io/en/stable/changelog.html) the only particular ticket that looks familiar is https://github.com/python/mypy/pull/21405.
To Reproduce
def run_funct(cmd: str, *arguments: str) -> None:
try:
start_time = time()
match cmd, *arguments:
case ("aff", address, "True" | "true" | "False" | "false" as sd):
address = group_address(address)
CONSOLE.aff(address, sd=sd.lower() == "true")
case ("deaff", address):
address = group_address(address)
CONSOLE.deff(address)
case ("set_phase", "TDMA" | "FDMA" | "NATIVE" as phase): # ERROR: "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']
CONSOLE.call_rate = call_rate_mapping[phase]
Note: I tried creating a gist in the playground but it doesn't seem to pickup the errors.
Note: This only occurs when as phase is added to set the variable name.
Expected Behavior
MyPy shouldn't be treating the as phase setting as setting to a Literal, or should be able to support comparisons with str. Setting any string through the match statement with tuple[str] unpacking set for a tuple of [str, str] should be allowed to be assigned to a varible
Actual Behavior
mypy src
src/consolesimulator3/scripts/run_console_service.py:78: error: Incompatible types in capture pattern (pattern captures type "str", variable has type "Literal['TDMA', 'FDMA', 'NATIVE']") [misc]
Found 1 error in 1 file (checked 34 source files)
Your Environment
[tool.mypy]
exclude = [".*tests/"]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
extra_checks = true
implicit_optional = true
no_implicit_reexport = true
no_warn_no_return = true
strict_equality = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
[[tool.mypy.overrides]]
module = ["beaupy.*", "rpyc.*", "construct.*", "sip_parser.*"]
ignore_missing_imports = true
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the reported match statement with MyPy 2.1.0 and 2.0.0 using the Python snippet and strict configuration shown. Read the changelog and PR #21405, then trace handling of capture patterns with tuple string unpacking. Done means the regression is fixed and covered by a test showing that the annotated string capture is accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100