Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain None (unreachable) when typemap is None with type(x) is Foo check #18486

Merged

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Jan 18, 2025

Fixes #18428.

Prevents rewriting None ("unreachable") typemaps as empty dicts ("nothing to infer")

Copy link
Contributor

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

@sterliakov sterliakov marked this pull request as ready for review January 20, 2025 00:32
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Looks good! One nit that might be a little faster

Comment on lines +5917 to 5921
if all(m is None for m in list_maps):
return None
result_map = {}
for d in list_maps:
if d is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if all(m is None for m in list_maps):
return None
result_map = {}
for d in list_maps:
if d is not None:
result_map = None
for d in list_maps:
if d is not None:
if result_map is None:
result_map = {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd keep my version if you don't mind: this part isn't hot (only reachable when we actually have some type(x) <op> Something checks), and I find it more explicit/slightly easier to follow. This shouldn't have any measurable performance impact.

@hauntsaninja hauntsaninja merged commit b82697b into python:master Jan 27, 2025
18 checks passed
x612skm pushed a commit to x612skm/mypy-dev that referenced this pull request Feb 24, 2025
…check (python#18486)

Fixes python#18428. 

Prevents rewriting `None` ("unreachable") typemaps as empty dicts
("nothing to infer")
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.

A constrained TypeVar value fails type() narrowing tests
2 participants