28

I am having some issues working with the typing types in Python for any more than type hinting:

>>> from typing import List
>>> string_list = ['nobody', 'expects', 'the', 'spanish', 'inqusition']
>>> string_list_class = List[str]

Now I would like to

  1. Check if string_list conforms to string_list_class.
  2. Check if string_list_class is a list.
  3. If so, check the class, that string_list_class is a list of.

I find myself unable to achieve any of those:

>>> isinstance(string_list, string_list_class)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 708, in __instancecheck__
    return self.__subclasscheck__(type(obj))
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 716, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

>>> issubclass(string_list_class, List)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/typing.py", line 716, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

The documentation also is not really helpful with that. Also the API does not seem to be intended to be used that way, however, I need to use that functionality.

Bodging around

A way I found to answer 2. is, that

>>> type(string_list_class)
<class 'typing._GenericAlias'>

Tough I have no access to _GenericAlias I can build it myself:

>>> _GenericAlias = type(List[str])
>>> isinstance(string_list_class, _GenericAlias)
True

However that does not seem like a good solution at all and it also yields True for other classes like Collection.

For 1. and 3. I could imagine hacking something together with repr(type(string_list)) and repr(string_list_class) and somehow comparing that string to something, but that also is not a good solution.

But there must be a better way to do this

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hans
  • 2,354
  • 3
  • 25
  • 35
  • I get point 1, but what's the purpose of point 2? If `string_list` conforms to `string_list_class`, then you already know that it's a list. And I have no idea what point 3 is supposed to mean. – Aran-Fey Jul 04 '18 at 10:59
  • I want to check if `string_list_class` is a List type in general. It might be a list of `str`, `int`, `MyObject` or something else. This is unrelated to 1. With 3 I want to extract `MyObject` from `List[MyObject]`. – Hans Jul 04 '18 at 11:01
  • 1
    Ok, I see. But... I'm not sure if it's a good idea to ask 3 mostly unrelated questions at the same time... – Aran-Fey Jul 04 '18 at 11:12
  • I agree, but they are not entirely unrelated IMHO, and answers to one or two of those questions could answer the others. I was unable to find a solution, but if there is one, the same source probably also could answer the other questions. – Hans Jul 04 '18 at 11:15
  • 1
    For point 3, see [How to access the type arguments of typing.Generic?](//stackoverflow.com/q/48572831). And [What's the correct way to check if an object is a typing.Generic?](//stackoverflow.com/q/49171189) might help with point 2. – Aran-Fey Jul 04 '18 at 11:20
  • Does this answer your question? [How to access the type arguments of typing.Generic?](https://stackoverflow.com/questions/48572831/how-to-access-the-type-arguments-of-typing-generic) – KenHBS Nov 29 '22 at 16:45
  • yes, however a very similar answer is has also been posted in response to this question so I accepted that. – Hans Nov 30 '22 at 09:15

1 Answers1

25

Checking if a variable conforms to a typing object

To check if string_list conforms to string_list_class, you can use the typeguard type checking library.

from typeguard import check_type

try:
    check_type('string_list', string_list, string_list_class)
    print("string_list conforms to string_list_class")
except TypeError:
    print("string_list does not conform to string_list_class")

Checking the generic type of a typing object

To check if string_list_class is a list type, you can use the typing_inspect library:

from typing_inspect import get_origin
from typing import List

get_origin(List[str]) # -> List

You could also use the private __origin__ field, but there is no stability guarantee for it.

List[str].__origin__ # -> list

Checking the type argument of a typing object

To check the class, that string_list_class is a list of, you can use the typing_inspect library again.

from typing_inspect import get_parameters
from typing import List

assert get_parameters(List[str])[0] == str

As before, there is also a private field you can use if you like to take risks

List[str].__args__[0] # -> str
lovasoa
  • 6,419
  • 1
  • 35
  • 45
  • 4
    Worth noting that a lot of the functionality in typing_inspect doesn't work in python 3.9. Unfortunately it seems that doing runtime checks for generic types can be a bit unreliable – Steve Vermeulen Dec 05 '20 at 10:30
  • See https://github.com/ilevkivskyi/typing_inspect/issues/65 – lovasoa Dec 05 '20 at 14:08