FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Preserve narrowing in unreachable code by hauntsaninja · Pull Request #20710 · python/mypy · GitHub

Repository navigation

Preserve narrowing in unreachable code - #20710

Merged
hauntsaninja merged 1 commit into
python:masterfrom
hauntsaninja:narrow74
Feb 3, 2026
Merged

hauntsaninja merged 1 commit into
python:masterfrom
hauntsaninja:narrow74

Conversation

hauntsaninja commented Jan 31, 2026 •
edited
Loading

Copy link
Copy Markdown
Collaborator

In the future, it would be nice to have an option to continue to check unreachable code. See #18707 from A5rocks. This PR lays some semantics preserving groundwork for that.

This is effectively a subset of #18707 , but I go just a little bit further and change the type of TypeMap to exclude None

Btw A5rocks if you are willing to rebase your PR, that would be great. I think --check-unreachable will be a very useful feature.

Co-authored-by: A5rocks

hauntsaninja changed the title preserve type info in unreachable branches Preserve narrowing in unreachable code Jan 31, 2026

Copy link
Copy Markdown
Contributor

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

hauntsaninja marked this pull request as ready for review January 31, 2026 05:07
hauntsaninja mentioned this pull request Jan 31, 2026
hauntsaninja requested a review from A5rocks January 31, 2026 05:08

JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Not a full review, but I'm interested if this affects performance.

Comment thread mypy/checker.py

# The second pass narrows down the types and type checks bodies.
unmatched_types: TypeMap = None
unmatched_types: TypeMap = {s.subject: UninhabitedType()}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Now we construct some extra temporary dictionaries and UninhabitedType objects. Can you measure if this has measurable performance impact?

hauntsaninja commented Feb 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Collaborator Author

There is maybe some perf impact, something like 0.3% on self check

I think it's well worth the cost. Not checking unreachable code is one of the most surprising mypy behaviours and it's a thing that makes accurately improving narrowing right now quite scary (see e.g. #20660 (comment) or fixing #12535 )

I also do think I can maybe claw some of it back in future refactors

A5rocks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Makes sense!

If perf really matters, maybe we can replace the dictionaries with an associative list or something like that. Or even a 2-tuple if a function is guaranteed to return at most 1 result! But IMO it's worth it to take a slight perf hit in order to provide new functionality.

hauntsaninja commented Feb 3, 2026 •
edited
Loading

Copy link
Copy Markdown
Collaborator Author

Yes, I was experimenting with 2-tuples and this is promising (but a larger change). Esp since we actually probably want to switch to combining keyed by literal_hash anyway...

I also recently landed several percentage point improvements, so I feel it is okay if I spend 0.3% :-)

hauntsaninja merged commit 606973f into python:master Feb 3, 2026
24 checks passed
hauntsaninja deleted the narrow74 branch February 3, 2026 05:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL