Skip to content

Combine the revealed types of multiple iteration steps in a more robust manner. #19324

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

Merged
merged 9 commits into from
Jul 7, 2025

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Jun 21, 2025

This PR fixes a regression introduced in #19118 and discussed in #19270. The combination of the revealed types of individual iteration steps now relies on collecting the original type objects instead of parts of preliminary revealed_type notes. As @JukkaL suspected, this approach is much more straightforward than introducing a sufficiently complete revealed_type note parser.

Please note that I appended a commit that refactors already existing code. It is mainly code-moving, so I hope it does not complicate the review of this PR.

@tyralla tyralla requested review from JukkaL and hauntsaninja June 21, 2025 22:38

This comment has been minimized.

Copy link
Contributor

@CoolCat467 CoolCat467 left a comment

Choose a reason for hiding this comment

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

Can't say I completely understand the code, but functionality shown from the test changes looks great!

This comment has been minimized.

This comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for fixing error reporting! Left a few comments about style/structural issues, but otherwise looks good.

mypy/errors.py Outdated
@@ -15,6 +15,8 @@
from mypy.nodes import Context
from mypy.options import Options
from mypy.scope import Scope
from mypy.typeops import make_simplified_union
Copy link
Collaborator

Choose a reason for hiding this comment

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

This causes a problematic import dependency and makes import cycles worse in the mypy codebase. You should be able to give the responsibility for calling this to the caller, or move the relevant code to another module.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

mypy/errors.py Outdated
@@ -596,18 +585,20 @@ def _add_error_info(self, file: str, info: ErrorInfo) -> None:
if info.code in (IMPORT, IMPORT_UNTYPED, IMPORT_NOT_FOUND):
self.seen_import_error = True

@property
def watchers(self) -> Iterator[ErrorWatcher]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: It's a bit unusual to have a generator that is a property. I think it would be better to make this a normal method. It may also help performance when compiled with mypyc (assuming this is a performance critical).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

mypy/messages.py Outdated
for error_info in iter_errors.yield_uselessness_error_infos():
self.fail(*error_info[:2], code=error_info[2])
for note_info, context in iter_errors.yield_revealed_type_infos():
self.reveal_type(note_info, context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's reasonable to perform the make_simplified_union here, since mypy.messages already depends on type ops.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

mypy/errors.py Outdated
context = Context(line=note_info[0], column=note_info[1])
context.end_line = note_info[2]
context.end_column = note_info[3]
yield make_simplified_union(types), context
Copy link
Collaborator

Choose a reason for hiding this comment

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

Call UnionType.make_union here to avoid make_simplified_union dependency here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems unnecessary, as we now use make_simplified_union elsewhere, so I decided to return the original list of types here simply. I hope that's okay.

Copy link
Contributor

github-actions bot commented Jul 4, 2025

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

@tyralla
Copy link
Collaborator Author

tyralla commented Jul 4, 2025

Thanks for the reviews!

I adjusted everything as suggested, except for the tiny difference of avoiding UnionType.make_union (as mentioned above).

@tyralla tyralla requested a review from JukkaL July 4, 2025 23:02
@JukkaL JukkaL merged commit 1fde143 into python:master Jul 7, 2025
19 checks passed
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