From 17812dffb5eb65f900accedfb8cb9712f33d4f9c Mon Sep 17 00:00:00 2001 From: remimd Date: Fri, 7 Nov 2025 18:34:09 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Now=20ignore=20NoneType?= =?UTF-8?q?=20when=20analyzing=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- injection/_core/common/type.py | 4 ++-- tests/core/test_descriptors.py | 29 ++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/injection/_core/common/type.py b/injection/_core/common/type.py index d5c81b2..d46d6e2 100644 --- a/injection/_core/common/type.py +++ b/injection/_core/common/type.py @@ -10,7 +10,7 @@ Iterator, ) from inspect import isclass, isfunction -from types import GenericAlias, UnionType +from types import GenericAlias, NoneType, UnionType from typing import ( Annotated, Any, @@ -74,7 +74,7 @@ def standardize_types( with_origin: bool = False, ) -> Iterator[TypeDef[Any]]: for tp in types: - if tp is None: + if tp in (None, NoneType): continue origin = get_origin(tp) diff --git a/tests/core/test_descriptors.py b/tests/core/test_descriptors.py index b129809..e49dc7d 100644 --- a/tests/core/test_descriptors.py +++ b/tests/core/test_descriptors.py @@ -10,23 +10,23 @@ class _RawData: ... class TestMappedScope: def test_set_name_with_multiple_owner_raise_type_error(self): - class ContextA: + class BindingsA: scope = MappedScope("some_scope") with pytest.raises(TypeError): - class ContextB: - scope = ContextA.scope + class BindingsB: + scope = BindingsA.scope async def test_aopen_with_success(self, module): @dataclass - class ScopeContext: + class Bindings: data: Scoped[_RawData] scope = MappedScope("some_scope", module=module) data = _RawData() - context = ScopeContext(data) + context = Bindings(data) assert module.get_instance(_RawData) is NotImplemented @@ -37,14 +37,14 @@ class ScopeContext: def test_open_with_success(self, module): @dataclass - class ScopeContext: + class Bindings: data: Scoped[_RawData] unscoped_data: int scope = MappedScope("some_scope", module=module) data = _RawData() - context = ScopeContext(data, 2) + context = Bindings(data, 2) assert module.get_instance(_RawData) is NotImplemented @@ -54,6 +54,21 @@ class ScopeContext: assert module.get_instance(_RawData) is NotImplemented + def test_open_with_optional_types(self, module): + @dataclass + class Bindings: + data: Scoped[_RawData | None] = None + name: Scoped[str | None] = None + + scope = MappedScope("some_scope", module=module) + + data = _RawData() + context = Bindings(data) + + with context.scope.define(): + assert module.get_instance(_RawData) is data + assert module.get_instance(str) is NotImplemented + class TestLazyInstance: def test_lazy_instance_with_instance_return_t(self):