Skip to content

Add direct support for dataclasses #26

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

Merged
merged 3 commits into from
Apr 21, 2025
Merged

Add direct support for dataclasses #26

merged 3 commits into from
Apr 21, 2025

Conversation

lagru
Copy link
Member

@lagru lagru commented Apr 21, 2025

Dataclasses require special treatment compared to normal classes when typed with a stub file. Unlike with attributes of normal classes we want to preserve whether there is a default value. So

# file.py

from dataclasses import dataclass, field, KW_ONLY, InitVar
from typing import ClassVar, Final

class Foo:
    a: float
    b: int = 3
    c: str = None
    _: KW_ONLY
    d: dict[str, Any] = field(default_factory=dict)
    e: InitVar[tuple] = tuple()
    f: ClassVar
    g: ClassVar[float]
    h: Final[ClassVar[int]] = 1

should become

# file.pyi

from dataclasses import dataclass, field, KW_ONLY, InitVar
from typing import ClassVar, Final, 

class Foo:
    a: float
    b: int = ...
    c: str = ...
    _: KW_ONLY
    d: dict[str, Any] = ...
    e: InitVar[tuple] = ...
    f: ClassVar
    g: ClassVar[float]
    h: Final[ClassVar[int]]

Note the class variable g that is still treated like a normal attribute – its value is stripped.

@lagru lagru added the enhancement New feature or request label Apr 21, 2025
@lagru lagru mentioned this pull request Apr 21, 2025
28 tasks
@lagru lagru merged commit d141a03 into main Apr 21, 2025
4 checks passed
@lagru lagru deleted the dataclasses branch April 21, 2025 11:07
@lagru lagru added this to the v0.3 (MVP) milestone May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant