Sequence pattern sometimes matches strings
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
Pyright version: 1.1.407, Python version: 3.14
Pyright understands that str and bytes objects don't match sequence patterns in the match statement:
def foo(x: str | bool) -> int:
match x:
case [*xs]: return 100 # reportUnnecessaryComparison: Pattern will never be matched for subject type "str | bool"
case _: return 200
However, that does not happen when str (or bytes) is part of a larger sequence type:
from collections.abc import Sequence
def foo(x: Sequence[object] | bool) -> int:
match x:
case [*_]: return 100
case True: return 200
case False: return 300
# if `x` is a `str` or `bytes` object, no patterns will be matched here
y = foo("banana")
pyright does not raise any issues with the foo function. y is inferred to be int, but is None at runtime.
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 two match-statement examples from the issue and compare Pyright's diagnostics with the stated runtime behavior. Trace the sequence-pattern analysis used for Sequence[object] and verify that the completed change reports the impossible or non-exhaustive case without affecting the direct str and bytes examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100