Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reflect on TypeVar in Custom Generic Class method

Consider the code bellow.

>>> class A(typing.Generic[T]):
...     @classmethod
...     def foo(cls) -> None:
...         print(typing.get_args(cls))
...
>>> A[int].foo()
()
>>> typing.get_args(A[int])
(<class 'int'>,)

Is it possible to infer the concrete type stored in T in the A.foo() class method?

This is a rather generic (sic) example. A real-world use case would be to implement a generic constructor which calls T type’s constructors or generic factory.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is an open issue on typing repo for this particular case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading