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 'transformed' frozen model is not hashable #3545

Closed
layday opened this issue Dec 19, 2021 · 2 comments
Closed

Dataclass 'transformed' frozen model is not hashable #3545

layday opened this issue Dec 19, 2021 · 2 comments

Comments

@layday
Copy link
Contributor

layday commented Dec 19, 2021

With the new dataclass transform decorator, Pyright reports for frozen models that they are not hashable:

# pyright: strict

from typing import Hashable


from pydantic import BaseModel

class Foo(BaseModel, frozen=True):
    pass

foo: Hashable = Foo()  # Error: Expression of type "Foo" cannot be assigned to declared type "Hashable"
reveal_type(Foo.__hash__)  # Type of "Foo.__hash__" is "None"


from dataclasses import dataclass

@dataclass(frozen=True)
class Bar:
    pass

bar: Hashable = Bar()  # No error
reveal_type(Bar.__hash__)  # Type of "Bar.__hash__" is "(self: Bar) -> int"

Originally posted by @layday in #2721 (comment)

@layday
Copy link
Contributor Author

layday commented Dec 19, 2021

Peculiarly if I change eq_default to false on the ModelMetaclass then it type checks but I don't see why that should be.

Okay, yeah, it just straight up doesn't seem to pick up frozen. Because, for dataclasses, "... If eq is true and frozen is false, __hash__() will be set to None" (which is how hashing behaves in Python more generally - implementing __eq__ automatically sets __hash__ to None).

@layday
Copy link
Contributor Author

layday commented Dec 31, 2021

I'll open an issue on Pyright's tracker when I get around to it.

@layday layday closed this as completed Dec 31, 2021
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

No branches or pull requests

1 participant