Skip to content

Commit 6c51321

Browse files
authored
fix: 🐛 Now ignore NoneType when analyzing types
1 parent e1016ee commit 6c51321

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

injection/_core/common/type.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Iterator,
1111
)
1212
from inspect import isclass, isfunction
13-
from types import GenericAlias, UnionType
13+
from types import GenericAlias, NoneType, UnionType
1414
from typing import (
1515
Annotated,
1616
Any,
@@ -74,7 +74,7 @@ def standardize_types(
7474
with_origin: bool = False,
7575
) -> Iterator[TypeDef[Any]]:
7676
for tp in types:
77-
if tp is None:
77+
if tp in (None, NoneType):
7878
continue
7979

8080
origin = get_origin(tp)

tests/core/test_descriptors.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ class _RawData: ...
1010

1111
class TestMappedScope:
1212
def test_set_name_with_multiple_owner_raise_type_error(self):
13-
class ContextA:
13+
class BindingsA:
1414
scope = MappedScope("some_scope")
1515

1616
with pytest.raises(TypeError):
1717

18-
class ContextB:
19-
scope = ContextA.scope
18+
class BindingsB:
19+
scope = BindingsA.scope
2020

2121
async def test_aopen_with_success(self, module):
2222
@dataclass
23-
class ScopeContext:
23+
class Bindings:
2424
data: Scoped[_RawData]
2525

2626
scope = MappedScope("some_scope", module=module)
2727

2828
data = _RawData()
29-
context = ScopeContext(data)
29+
context = Bindings(data)
3030

3131
assert module.get_instance(_RawData) is NotImplemented
3232

@@ -37,14 +37,14 @@ class ScopeContext:
3737

3838
def test_open_with_success(self, module):
3939
@dataclass
40-
class ScopeContext:
40+
class Bindings:
4141
data: Scoped[_RawData]
4242
unscoped_data: int
4343

4444
scope = MappedScope("some_scope", module=module)
4545

4646
data = _RawData()
47-
context = ScopeContext(data, 2)
47+
context = Bindings(data, 2)
4848

4949
assert module.get_instance(_RawData) is NotImplemented
5050

@@ -54,6 +54,21 @@ class ScopeContext:
5454

5555
assert module.get_instance(_RawData) is NotImplemented
5656

57+
def test_open_with_optional_types(self, module):
58+
@dataclass
59+
class Bindings:
60+
data: Scoped[_RawData | None] = None
61+
name: Scoped[str | None] = None
62+
63+
scope = MappedScope("some_scope", module=module)
64+
65+
data = _RawData()
66+
context = Bindings(data)
67+
68+
with context.scope.define():
69+
assert module.get_instance(_RawData) is data
70+
assert module.get_instance(str) is NotImplemented
71+
5772

5873
class TestLazyInstance:
5974
def test_lazy_instance_with_instance_return_t(self):

0 commit comments

Comments
 (0)