Skip to content

Conversation

@cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Aug 26, 2025

At the moment mypy only infers the type of variables captures inside a match statement. This PR adds additional type information to the match subject itself, similar to isinstance calls.

class A:
    a: str | None

m: object
match m:
    case A(a="Hello"):
        reveal_type(m.a)  # str | None -> Literal['Hello']

Fixes #14911

@cdce8p cdce8p force-pushed the match-subject-inference branch from 5f62435 to 9cf17b9 Compare August 26, 2025 12:26
@cdce8p cdce8p changed the title Improved match subject inference Improve match subject inference Aug 26, 2025
@cdce8p cdce8p added the topic-match-statement Python 3.10's match statement label Aug 26, 2025
@cdce8p cdce8p force-pushed the match-subject-inference branch from 9cf17b9 to c132ec4 Compare August 26, 2025 12:30
@cdce8p cdce8p added the topic-type-narrowing Conditional type narrowing / binder label Aug 26, 2025
@github-actions

This comment has been minimized.

@cdce8p cdce8p marked this pull request as draft August 26, 2025 12:52
@cdce8p cdce8p force-pushed the match-subject-inference branch from ae3cc0e to 244b1e4 Compare August 26, 2025 21:16
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@cdce8p cdce8p marked this pull request as ready for review August 26, 2025 23:38
@cdce8p cdce8p marked this pull request as draft August 27, 2025 01:21
@cdce8p cdce8p force-pushed the match-subject-inference branch from f614291 to 1da718a Compare August 27, 2025 13:09
@cdce8p cdce8p marked this pull request as ready for review August 27, 2025 13:10
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@cdce8p cdce8p force-pushed the match-subject-inference branch from 9524293 to f34840b Compare September 25, 2025 23:12
@github-actions

This comment has been minimized.

@cdce8p cdce8p force-pushed the match-subject-inference branch from f34840b to df6f5dd Compare December 14, 2025 23:09
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@cdce8p cdce8p force-pushed the match-subject-inference branch from 9a50d9a to b77b374 Compare December 15, 2025 10:10
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/assertion/rewrite.py:1023: error: Unused "type: ignore" comment  [unused-ignore]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type narrowing on object attribute with match statement doesn't apply

1 participant