typing icon indicating copy to clipboard operation
typing copied to clipboard

Clarification on typing.dataclass_transform() defaults

Open superbobry opened this issue 2 years ago • 4 comments

I cannot quite pinpoint the interaction of the defaults in typing.dataclass_transform() and __init_subclass__/__new__ parameters. PEP-681 does not seem to cover that at all, so perhaps I am missing something very obvious. Consider the following snippet

from typing import dataclass_transform
@dataclass_transform(order_default=True)
class Base:
    def __init_subclass__(cls, *, order: bool = False): ...


class A(Base): ...

How should a type checker resolve the conflict between order_default= and order= in __init_subclass__? Is A ordered here? Is this a static error?

superbobry avatar Jul 03 '23 21:07 superbobry

@superbobry, thanks for your question! Just wanted to respond to say that I saw this. Coincidentally, @erictraut and I, the authors of PEP 681, are both on vacation at the moment. One of us will reply when we're back.

debonte avatar Jul 05 '23 15:07 debonte

@superbobry, sorry for the slow reply. As @debonte mentioned, we were both on vacation.

To answer your question above, it wouldn't make sense to default order to False in __init_subclass__ and then default it to True in the dataclass_transform. If you specify order_default=True in the dataclass_transform, then you are attesting to the fact that your implementation defaults to order=True (regardless of whether your implementation uses a custom metaclass or __init_subclass__.

erictraut avatar Jul 14 '23 15:07 erictraut

Thanks for the clarification, Eric! Does that mean that the snippet in my original post should produce a static error?

superbobry avatar Jul 14 '23 19:07 superbobry

No, static type checkers won't check for this error. If you assert that the order is True by default, then your implementation should honor that. This would be a really esoteric bug for static type checkers to explicitly check for.

erictraut avatar Jul 14 '23 19:07 erictraut

Is there anything left to do here?

JelleZijlstra avatar May 23 '24 05:05 JelleZijlstra

From my perspective, no there's nothing left to do here.

erictraut avatar May 23 '24 05:05 erictraut