I'm trying to deepen my understanding of how Python's dataclasses module works, particularly the role of type hints in defining class attributes.
When I define a dataclass like this:
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
I notice that the type hints (str
for name and int
for age) are used by the dataclass decorator to generate various methods like __init__
, __repr__
, and __eq__
(I'm only interested in __init__
). This automatic generation makes it very convenient, but I'm curious about the underlying mechanisms that enable this functionality.
Specifically, I have a question:
How does the dataclass decorator utilize type hints to generate these methods?
I’ve copied and pasted the same code from the original dataclasses file into my working directory with the name custom_dataclass (for testing purposes), but my linter did not recognize the __init__
attributes.
In picture1, you can see that when I try to instantiate the Person
class, I correctly visualize all attributes mentioned in the Person
class. This class uses the original @dataclass
decorator. However, when I try the same with PersonCustom
using the @custom_dataclass
decorator, no attributes appear at all, as shown in picture2.
If anyone has any answers or insights, I’d like to hear them. Thanks in advance for your help!
I've also tried to see declarative_base
method from sqlalchemy
like
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)
User()
but there no attributes appear either
[sqlalchemy] no attributes appear either
Well, there aren't any type hints involved.
Rather we're assigning Column
objects to those names,
and each such object is storing a certain sql type
(not a hint, not an annotation).
the type hints ... are used by the dataclass decorator
Yes. It accesses them via an API call. Details are in the documentation.
It appears your custom decorator is not making use of dataclass_transform() and is not doing work similar to what the @dataclass decorator does. Which is unsurprising, as there's a lot of work involved, at least when addressing the general case, about 1.5 KLOC of source code.