S
S
Sergey Pankov2020-06-10 09:14:04
Python
Sergey Pankov, 2020-06-10 09:14:04

How to properly clean the generic type from parameters: List[str] -> List?

In python3.7, backwards compatibility was broken in terms of generic type checking:

$ python3.6 -c "import typing; print(issubclass(typing.List[str], typing.List))"
True
$ python3.8 -c "import typing; print(issubclass(typing.List[str], typing.List))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.8/typing.py", line 774, in __subclasscheck__
    raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks

Let's now leave out of brackets all my disgust at how it was decided to implement type hinting and generics in python. Let's not spit on the faecal code in the python standard library. Let's just bombard the topic:
And how correctly elegantly and briefly clean the type description from the parameters.

Somewhere I met this:
getattr(typing, List[str]._name)
But, damn it, are they serious ?! Isn't there a better way?

Honestly, for a long time I have been trying to look less towards the details of the implementation of some "dirty" places of my favorite programming language. One disorder.
Here I came across when migrating my small project from 3.6 to 3.8 to another cake.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question