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

[dataclass_transform] fix frozen behavior for base classes with direct metaclasses #14878

Merged
merged 1 commit into from
Mar 15, 2023

Conversation

wesleywright
Copy link
Collaborator

@wesleywright wesleywright commented Mar 11, 2023

Fixes #14857. The initial implementation overlooked this statement in PEP 681:

Similarly, a class that directly specifies a metaclass that is decorated with dataclass_transform is considered neither frozen nor non-frozen.

As far as I can tell, this is a special case that only applies to classes that directly specify a dataclass_transform metaclass. This is import for projects like Pydantic, which requires clients to use a base class but still supports a frozen parameter.

Note that this allows mixed frozen and non-frozen behavior if the base class does have fields:

@dataclass_transform()
class Meta(type): ...
class Base(metaclass=Meta, frozen=False):
    base: int = 0
class Foo(Base, frozen=True):
    foo: int = 0

foo = Foo()
foo.foo = 1 # an error because Foo is frozen
foo.base = 1 # NOT an error — Base is neither frozen nor non-frozen

While this is probably surprising behavior, it seems to match the text of the PEP as well as the behavior of Pyright.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

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, looks good!

@JukkaL JukkaL merged commit a6b5b1e into python:master Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot inherit frozen dataclass from a non-frozen one error when using pydantic frozen=True
3 participants