Skip to content

Prevent a crash when InitVar is redefined with a method in a subclass #19453

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

Conversation

sterliakov
Copy link
Collaborator

Fixes #19443. This case is too niche (and should be trivially avoidable), so just not crashing should be good enough. The value is indeed redefined, and trying to massage the plugin to move the X-redefinition back to X in names is not worth the effort IMO.

Copy link
Contributor

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

@hauntsaninja hauntsaninja merged commit 6403752 into python:master Jul 16, 2025
20 checks passed
# be reachable from the containing class by its name.
# The redefinition is never removed, use this as a sanity check to verify
# the reasoning above.
assert f"{defn.name}-redefinition" in defn.info.names
Copy link
Member

Choose a reason for hiding this comment

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

@sterliakov What about multiple redefinitions? Will this fix still work? The current logic in semanal.py is

            if i == 1:
                new_name = f"{name}-redefinition"
            else:
                new_name = f"{name}-redefinition{i}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The first redefinition cannot be InitVar, so should not get deleted. I can replace this with any(name.startswith(f"{defn.name}-redifinition") for name in info.names), but I couldn't invent the case where name-redefinition is also gone

Copy link
Member

Choose a reason for hiding this comment

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

OK, if you can't trigger it, it is fine.

hauntsaninja pushed a commit that referenced this pull request Jul 27, 2025
…#19453)

Fixes #19443. This case is too niche (and should be trivially
avoidable), so just not crashing should be good enough. The value is
indeed redefined, and trying to massage the plugin to move the
`X-redefinition` back to `X` in names is not worth the effort IMO.
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.

[1.16 regression] Crashes on a combination of dataclass inheritance, InitVar, and properties.
3 participants