Fix accidentally broken Python pre-3.10 compatibility

Tests started failing like

________________________ test_newtype_integration_works ________________________
Traceback (most recent call last):
  File "/home/runner/work/injector/injector/injector_test.py", line 1398, in test_newtype_integration_works
    injector = Injector([configure])
  File "/home/runner/work/injector/injector/injector/__init__.py", line 904, in __init__
    self.binder.install(module)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 573, in install
    instance(self)
  File "/home/runner/work/injector/injector/injector_test.py", line 1396, in configure
    binder.bind(UserID, to=123)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 474, in bind
    self._bindings[interface] = self.create_binding(interface, to, scope)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 578, in create_binding
    provider = self.provider_for(interface, to)
  File "/home/runner/work/injector/injector/injector/__init__.py", line 640, in provider_for
    raise UnknownProvider('couldn\'t determine provider for %r to %r' % (interface, to))
injector.UnknownProvider: couldn't determine provider for <function NewType.<locals>.new_type at 0x7f64edc69d90> to 123

when I merged d7f6f396eb.
This commit is contained in:
Jakub Stasiak 2021-12-20 22:59:34 +01:00
parent 3a63a32e1e
commit ba0409d40b
1 changed files with 7 additions and 1 deletions

View File

@ -700,7 +700,13 @@ def _is_specialization(cls: type, generic_class: Any) -> bool:
def _punch_through_alias(type_: Any) -> type:
if type(type_).__module__ == 'typing' and type(type_).__name__ == 'NewType':
if (
sys.version_info < (3, 10)
and getattr(type_, '__qualname__', '') == 'NewType.<locals>.new_type'
or sys.version_info >= (3, 10)
and type(type_).__module__ == 'typing'
and type(type_).__name__ == 'NewType'
):
return type_.__supertype__
else:
return type_