-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
frozen dataclass inheritance is not strictly checked in multiple inheritance #109409
Comments
Are you saying |
import dataclasses
@dataclasses.dataclass
class NotFrozen:
pass
@dataclasses.dataclass(frozen=True)
class Frozen:
pass
# this class fails to create with
# TypeError: cannot inherit non-frozen dataclass from a frozen one
@dataclasses.dataclass
class NotFrozenChild(NotFrozen, Frozen):
pass
# this class has no issue with creation
@dataclasses.dataclass(frozen=True)
class FrozenChild(NotFrozen, Frozen):
pass The expected behavior would be that FrozenChild should fail to create with a TypeError: cannot inherit frozen dataclass from a non-frozen one. Instead FrozenChild creates with no issues (though linters such as pylance do catch this issue). |
Yes, I also belive that this is a bug, here's how users can face it: def mutate_not_frozen(instance: NotFrozen) -> None:
x.some_field = 1
mutate_not_frozen(NotFrozen()) # ok
mutate_not_frozen(FrozenChild()) # type-checkers will allow it, but will fail in runtime Interesting, it should fail here: # Raise an exception if we're frozen, but none of our bases are.
if not any_frozen_base and frozen:
raise TypeError('cannot inherit frozen dataclass from a '
'non-frozen one') and it fails correctly with just a single parent, but multiple parents should be checked with |
Since this is a change in behavior, I'm inclined to call it a feature request and not backport it. |
…ass mixins (gh-109437) Fix inheritance of frozen dataclass from non-frozen dataclass mixins
Thanks, @sobolevn ! |
…dataclass mixins (pythongh-109437) Fix inheritance of frozen dataclass from non-frozen dataclass mixins
Bug report
Bug description:
The dataclass inheritance hierarchy is supposed to require all classes to be either frozen or non frozen, this works properly for checking that an unfrozen class does not inherit from any frozen classes, but it allows frozen classes to inherit from unfrozen ones as long as there's at least one frozen class in the MI
CPython versions tested on:
3.10
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: