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

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

This comment has been minimized.

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
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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