Skip to content

Fix crash on invalid redefinition in dataclass - #22095

Merged
ilevkivskyi merged 6 commits into
python:masterfrom
ilevkivskyi:fix-datacls-redef
Oct 5, 2026
Merged

ilevkivskyi merged 6 commits into
python:masterfrom
ilevkivskyi:fix-datacls-redef

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #20476

Fix seems to be quite simple: if there are some weird redefinitions, just ignore them, an error is already given.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

Both errors in homeassistant are correct (inconsistent self-binding), but the error message in the first one is really confusing. I will check if it is easy to fix.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

Actually, I think the second one is wrong, we should expand the self-type there.

@ilevkivskyi

Copy link
Copy Markdown
Member Author

OK, the situation is quite messy. So I will try to simply restore the existing behavior.

@github-actions

This comment has been minimized.

@ilevkivskyi

ilevkivskyi commented Oct 3, 2026 •

Copy link
Copy Markdown
Member Author

After even more thinking, the dataclasses are different from regular classes in how they handle callables. So let's not go down this rabbit hole, and focus on simply fixing the crash.

I will however keep all the tests fore future reference.

@github-actions

This comment has been minimized.

foo = "test"
foo: int = 42 # E: Name "foo" already defined on line 5

reveal_type(Test) # N: Revealed type is "def () -> __main__.Test"

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.

Maybe also reveal the type of attribute foo?

class Test:
foo = fn
foo: int = 42 # E: Name "foo" already defined on line 8
[builtins fixtures/dataclasses.pyi]

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.

Maybe also reveal type of attribute foo?

@github-actions

github-actions Bot commented Oct 5, 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. ✅

@ilevkivskyi
ilevkivskyi merged commit 7899b43 into python:master Oct 5, 2026
25 checks passed
@ilevkivskyi
ilevkivskyi deleted the fix-datacls-redef branch October 5, 2026 13:40
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.

mypy crashes on a dataclass

2 participants