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

Sample use case: Returning from a dataclass-transform #27

Open
NeilGirdhar opened this issue Sep 19, 2023 · 0 comments
Open

Sample use case: Returning from a dataclass-transform #27

NeilGirdhar opened this issue Sep 19, 2023 · 0 comments
Labels
sample use case here to make sure none of these get lost

Comments

@NeilGirdhar
Copy link
Collaborator

This is a typical use of the PEP 681 dataclass_transform:

class DataclassInstance(Protocol):  # See usefule_types.experimental.DataclassLike
    __dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]


@overload
@dataclass_transform(frozen_default=True, field_specifiers=(field,))
def dataclass(*, init: bool = True, repr_: bool = True, eq: bool = True,
              order: bool = False) -> Callable[[type[Any]], type[DataclassInstance]]:
    ...


@overload
@dataclass_transform(frozen_default=True, field_specifiers=(field,))
def dataclass(cls: type[Any], /, *, init: bool = True, repr_: bool = True, eq: bool = True,
              order: bool = False) -> type[DataclassInstance]:
    ...

What we would like to have, in fact, would be the following return types:

@overload
@dataclass_transform(frozen_default=True, field_specifiers=(field,))
def dataclass[T](*, init: bool = True, repr_: bool = True, eq: bool = True,
              order: bool = False) -> Callable[[type[T]], type[T] & type[DataclassInstance]]:
    ...


@overload
@dataclass_transform(frozen_default=True, field_specifiers=(field,))
def dataclass[T](cls: type[T], /, *, init: bool = True, repr_: bool = True, eq: bool = True,
              order: bool = False) -> type[T] & type[DataclassInstance]:
    ...

This would allow the type check to know that the returned dataclass is both a DataclassInstance (supports dataclasses.fields, dataclasses.replace, etc.) as well as remaining T.

@mikeshardmind mikeshardmind added the sample use case here to make sure none of these get lost label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sample use case here to make sure none of these get lost
Projects
None yet
Development

No branches or pull requests

2 participants