|
15 | 15 | """ |
16 | 16 |
|
17 | 17 | import collections |
| 18 | +import sys |
18 | 19 | import typing |
19 | 20 |
|
20 | 21 | try: |
@@ -93,7 +94,7 @@ def isoftype(value, T): |
93 | 94 | # there's not much we can do. |
94 | 95 |
|
95 | 96 | # TODO: Right now we're accessing internal fields to get what we need. |
96 | | - # TODO: Would be nice to update this if Python, at some point, adds an |
| 97 | + # TODO: Would be nice to rewrite this if Python, at some point, adds an |
97 | 98 | # TODO: official API to access the static type information at run time. |
98 | 99 |
|
99 | 100 | if T is typing.Any: |
@@ -185,8 +186,14 @@ def get_origin(tp): |
185 | 186 | if T is typing.Union: # isinstance(T, typing._SpecialForm) and T._name == "Union": |
186 | 187 | return False # pragma: no cover, Python 3.7+ only. |
187 | 188 |
|
188 | | - # TODO: in Python 3.7+, what is the mysterious callable that doesn't have `__qualname__`? |
189 | | - if callable(T) and hasattr(T, "__qualname__") and T.__qualname__ == "NewType.<locals>.new_type": |
| 189 | + def isNewType(T): |
| 190 | + # In Python 3.10, an instance of `typing.NewType` is now actually such and not just a function. Nice! |
| 191 | + if sys.version_info >= (3, 10, 0): |
| 192 | + return isinstance(T, typing.NewType) |
| 193 | + # Python 3.6 through Python 3.9 |
| 194 | + # TODO: in Python 3.7+, what is the mysterious callable that doesn't have a `__qualname__`? |
| 195 | + return callable(T) and hasattr(T, "__qualname__") and T.__qualname__ == "NewType.<locals>.new_type" |
| 196 | + if isNewType(T): |
190 | 197 | # This is the best we can do, because the static types created by `typing.NewType` |
191 | 198 | # have a constructor that discards the type information at runtime: |
192 | 199 | # UserId = typing.NewType("UserId", int) |
|
0 commit comments