Generic[T] base class – how to get type of T from within instance?

Assume you have a Python class that inherits from Generic[T]. Is there any way to get a hold of the actual type passed in from within the class/instance?

For example,

from typing import TypeVar, Type
T = TypeVar('T')

class Test(Generic[T]):
    def hello(self):
      my_type = T  # this is wrong!
      print( "I am {0}".format(my_type) )

Test[int]().hello() # should print "I am int"

On here, it is suggested the type arg would be present in in the args field of the type. And indeed,

print( str( Test[int].__args__ ) )

would print (<class ‘int’>,). However, I can’t seem to access this from within the instance directly, e.g. substituting

      my_type = self.__class__.__args__ # this is also wrong (None)

doesn’t seem to to the trick.

Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is no supported API for this. Under limited circumstances, if you’re willing to mess around with undocumented implementation details, you can sometimes do it, but it’s not reliable at all.


First, mypy doesn’t require you to provide type arguments when assigning to a generically-typed variable. You can do things like x: Test[int] = Test() and neither Python nor mypy will complain. mypy infers the type arguments, but Test is used at runtime instead of Test[int]. Since explicit type arguments are awkward to write and carry a performance penalty, lots of code only uses type arguments in the annotations, not at runtime.

There’s no way to recover type arguments at runtime that were never provided at runtime.


When type arguments are provided at runtime, the implementation does currently try to preserve this information, but only in a completely undocumented internal attribute that is subject to change without notice, and even this attribute might not be present. Specifically, when you call

Test[int]()

, the class of the new object is Test rather than Test[int], but the typing implementation attempts to set

obj.__orig_class__ = Test[int]

on the new object. If it cannot set __orig_class__ (for example, if Test uses __slots__), then it catches the AttributeError and gives up.

__orig_class__ was introduced in Python 3.5.3; it is not present on 3.5.2 and lower. Nothing in typing makes any actual use of __orig_class__.

The timing of the __orig_class__ assignment varies by Python version, but currently, it’s set after normal object construction has already finished. You will not be able to inspect __orig_class__ during __init__ or __new__.

These implementation details are current as of CPython 3.8.2.


__orig_class__ is an implementation detail, but at least on Python 3.8, you don’t have to access any additional implementation details to get the type arguments. Python 3.8 introduced typing.get_args, which returns a tuple of the type arguments of a typing type, or () for an invalid argument. (Yes, there was really no public API for that all the way from Python 3.5 until 3.8.)

For example,

typing.get_args(Test[int]().__orig_class__) == (int,)

If __orig_class__ is present and you’re willing to access it, then __orig_class__ and get_args together provide what you’re looking for.

Method 2

you can use self.__orig_class__:

from typing import TypeVar, Type, Generic
T = TypeVar('T')

class Test(Generic[T]):

    def hello(self):
        print( "I am {0}".format(self.__orig_class__.__args__[0].__name__))

Test[int]().hello()
# I am int

Method 3

This is possible if you’re willing to adjust the syntax of how your class is instantiated a little bit.

import typing

T = typing.TypeVar('T')
class MyGenericClass(typing.Generic[T]):
    def __init__(self, generic_arg: typing.Type[T]) -> None:
        self._generic_arg = generic_arg

    def print_generic_arg(self) -> None:
        print(f"My generic argument is {self._generic_arg}")

    def print_value(self, value: T) -> None:
        print(value)

my_instance = MyGenericClass(int)  # Note, NOT MyGenericClass[int]().

my_instance.print_generic_arg()  # Prints "My generic argument is <class 'int'>".

reveal_type(my_instance)  # Revealed type is MyGenericClass[builtins.int*].
                          # Note the asterisk, indicating that 
                          # builtins.int was *inferred.*

my_instance.print_value(123)  # OK, prints 123.
my_instance.print_value("abc")  # Type-checking error, "abc" isn't an int.

As one of the other answers here explains, trying to retrieve the type argument at runtime from the instance’s type can be inherently problematic. In part, because the instance wasn’t necessarily even created with a type argument to begin with.

So, this approach works around that by coming at the problem the other way. We require the caller to pass the type as an argument to __init__. Then, instead of trying to derive a runtime value of int from the instance’s type MyGenericClass[int], we start with the runtime value of int and let the type checker infer that the instance’s type is MyGenericClass[int].

In more complex cases, you might need to help out the type checker by redundantly specifying the type. It will be your responsibility to make sure the types match.

possible_types = {"int": int, "str": str}

# Error: "Need type annotation".
my_instance = MyGenericClass(possible_types["int"])

# OK.
my_instance = MyGenericClass[int](possible_types["int"])

Method 4

Currently (Python 3.10.3) one cannot access the type parameter during __init__ or __new__.

However, it’s possible to access the type variable in __init_subclass__. It’s a bit different scenario, but I think it’s interesting enough to share.

from typing import Any, Generic, TypeVar, get_args

T = TypeVar("T")


class MyGenericClass(Generic[T]):
    _type_T: Any

    def __init_subclass__(cls) -> None:
        cls._type_T = get_args(cls.__orig_bases__[0])[0]  # type: ignore


class SomeBaseClass(MyGenericClass[int]):
    def __init__(self) -> None:
        print(self._type_T)


SomeBaseClass()  # prints "<class 'int'>"

Method 5

You can use something else entirely.

# Define Wrapper Class
class Class():
    # Define __getitem__ method to be able to use index
    def __getitem__(self, type):
        # Define Main Class
        class Class():
            __doc__ = f"""I am an {type.__name__} class"""

            def __init__(self, value):
                self.value: type = type(value)
        # Return Class
        return Class
# Set Class to an instance of itself to be able to use the indexing
Class = Class()

print(Class[int].__doc__)
print(Class[int](5.3).value)

Use this if you want. You can use the type variable throughout the whole class, even when not using self. Just saying, syntax highlighters might have a hard time understanding this sort of thing because its using a return of something which they don’t really care about, even if its a class. At least for pylance, because that’s what I use.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x