Skip to content

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

Merged
JukkaL merged 9 commits into
python:masterfrom
tyralla:fix/better_reveal_type_unions
Jul 7, 2025
Merged

Combine the revealed types of multiple iteration steps in a more robust manner.#19324
JukkaL merged 9 commits into
python:masterfrom
tyralla:fix/better_reveal_type_unions

Conversation

@tyralla

@tyrallatyralla commented Jun 21, 2025

Copy link
Copy Markdown
Collaborator

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.

@github-actions

This comment has been minimized.

@tyrallatyralla mentioned this pull request Jun 21, 2025

@CoolCat467CoolCat467 left a comment

Copy link
Copy Markdown
Contributor

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!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@JukkaLJukkaL 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.

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

Comment threadmypy/errors.py Outdated
from mypy.nodes import Context
from mypy.options import Options
from mypy.scope import Scope
from mypy.typeops import make_simplified_union

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.

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
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

done

Comment threadmypy/errors.py Outdated
self.seen_import_error = True

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

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.

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
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

done

Comment threadmypy/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
Copy Markdown
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
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

done

Comment threadmypy/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
Copy Markdown
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
Copy Markdown
CollaboratorAuthor

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.

@github-actions

Copy link
Copy Markdown
Contributor

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

@tyralla

Copy link
Copy Markdown
CollaboratorAuthor

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 JukkaLJuly 4, 2025 23:02
@JukkaL
JukkaL merged commit 1fde143 into python:masterJul 7, 2025
19 checks passed
esarp pushed a commit that referenced this pull request Jul 10, 2025
…st manner. (#19324)
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.
Sign up for freeto 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

@tyralla@JukkaL@CoolCat467