From ba0409d40b7a3be2bb9d4221aeea09d772df208a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 20 Dec 2021 22:59:34 +0100 Subject: [PATCH] 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 .new_type at 0x7f64edc69d90> to 123 when I merged d7f6f396ebb8. --- injector/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/injector/__init__.py b/injector/__init__.py index 7fcfcba..d74fe2a 100644 --- a/injector/__init__.py +++ b/injector/__init__.py @@ -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..new_type' + or sys.version_info >= (3, 10) + and type(type_).__module__ == 'typing' + and type(type_).__name__ == 'NewType' + ): return type_.__supertype__ else: return type_