microsoft / microsoft/pyright

Sequence pattern sometimes matches strings

Open
#11,099 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.