1

I'm trying to annotate a transformation decorator that builds a dynamic data-class with the inclusion of several new functions. I'm trying to make use of dataclass_transform function but I'm not sure what the best way to design the typehints are.

To clear things up here's a simple example:

import types
from typing import *

class Field:
    """Custom Dataclass Field"""
    ...

class Base:
    """Custom Dataclass base for additional functions"""
    __fields__: List[Field]

    def encode(self) -> bytes:
        ...

    @classmethod
    def decode(cls, data: bytes) -> Self:
        ... 

T = TypeVar('T')

@dataclass_transform()
def transform(newclass: Type[T]) -> ?:
    return types.new_class('test', (newclass, Base), {})

I'm trying to figure out the ? portion at the end of the transform function.

So far from my research, I can't seem to find any existing type annotations that really support returning a type with dynamic multiple-inheritance. Maybe some combination of Type, NewType, or Generic will work? Is there a way to do this in any form? Any help is appreciated. Thanks!

IHaxYou
  • 53
  • 7
  • 1
    Does [this](https://github.com/python/typing/issues/213) answer your question? – Daniil Fajnberg May 09 '23 at 08:06
  • @DaniilFajnberg an "Intersection" type seems exactly what I'm looking for yes. Seems like it's unfortunately not yet included in standard python, but that's good to know about thank you. – IHaxYou May 13 '23 at 20:10
  • @SUTerliakov Indeed, it seems like this also addresses an Intersection type with additional options. Perfect thank you! A shame the Intersection isn't included in the typing stdlib yet. – IHaxYou May 13 '23 at 20:12
  • Feel free to create a draft PEP, if you feel strongly on-topic. I can assist somehow, if you'd like - however, beware that it's an extremely complex feature, and a very detailed explanation of how Intersection should work will be necessary (here's why PEP is almost a necessity). Alternatively, you can fork `mypy` and add Intersection to `typing_extensions`, building an MVP in collaboration with interested people. If you have such plans, consider writing to python-typing mailing list first to get some advice and let the community know in advance. – STerliakov May 13 '23 at 20:21
  • 1
    @SUTerliakov, It would be very nice, but unfortunately I don't have the time to dedicate for something as complex like that which I'm also not that familiar with. The good news is I think someone else has already put in most of the work lol. It seems like some [has forked mypy and made an implementation already](https://github.com/python/typing/issues/213#issuecomment-1520514170), so now it just needs to go through whatever approval process exists. – IHaxYou May 13 '23 at 20:24
  • WOW, thank you for this reference! I haven't seen that issue comment, and hope to give that fork a try next week. Awesome! – STerliakov May 13 '23 at 20:41
  • @SUTerliakov Yes, `basedmypy` lives up to its name. – Daniil Fajnberg May 14 '23 at 08:28

0 Answers0