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

Consider making DataclassLike non-final #16

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

Consider making DataclassLike non-final #16

NeilGirdhar opened this issue Sep 19, 2023 · 2 comments

Comments

@NeilGirdhar
Copy link

NeilGirdhar commented Sep 19, 2023

I want to derive a protocol from DataclassLike:

@runtime_checkable
class TDataclassInstance(DataclassLike, Protocol):
    static_fields: ClassVar[list[str]]
    dynamic_fields: ClassVar[list[str]]

This is for the return value of a dataclass_transform-marked constructor.


Another option would be to make final protocols support being inherited by other protocols, i.e., final only blocks non-protocol class inheritance.

@JelleZijlstra
Copy link
Collaborator

I'm not sure why we made it @final, and I'd probably support removing the decorator. Making a Protocol final feels kind of like going against the point of a Protocol.

@AlexWaygood
Copy link
Collaborator

AlexWaygood commented Sep 19, 2023

The rationale for making it @final was that people might erroneously infer that they could create a dataclass by inheriting from DataclassLike, just as you can create an Iterator by inheriting from collections.abc.Iterator or create a pydantic model by inheriting from BaseModel. dataclasses works differently from all of those in that the library has decorator-based semantics rather than an inheritance-based model, and the idea was that it would be good to emphasise this fact, and thereby clarify what the protocol was for (just type hints, it doesn't have any runtime effect).

python/typing_extensions#115 (comment)

That rationale probably makes a lot more sense when it comes to adding this class to the stdlib than it does when it comes to adding it to this library; we're much less likely to get newbies here who don't know how to create a dataclass. But if the idea is that the things in this library are candidates for possible inclusion in the stdlib, then maybe we should try to keep things looking pretty much as they would if they were in the stdlib.

Anyway, wanting to extend the protocol is a good use case for making it no longer @final, so I'm okay with changing this here.

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

3 participants