Skip to content

Commit 673b161

Browse files
victorywysYansen Wang
andauthored
Add the NoneType hook in type_def (#8)
* add support for expanding signiture list to its base classes when **kwargs is contained in a construction method * remove fixed FIXME comment * add a switch to turn off the inherent signature function * disable the inherent function by default and fix a bug in register.get(), where an unexpected exception may be raised * add more tests for better code coverage * fix typos * add typedef hook for NoneType * enable full test in test_config_type_def * remove redundant test call --------- Co-authored-by: Yansen Wang <[email protected]>
1 parent bc4697c commit 673b161

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/test_config_type_def.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def test_any():
2222
assert TypeDef.dump(typing.Any, '456') == '456'
2323

2424

25+
def test_none():
26+
assert TypeDef.load(type(None), None) == None
27+
assert TypeDef.dump(type(None), None) == None
28+
29+
with pytest.raises(ValidationError, match='must be NoneType'):
30+
TypeDef.load(type(None), 1)
31+
with pytest.raises(ValidationError, match='Expected None'):
32+
TypeDef.dump(type(None), "123")
33+
34+
2535
def test_unsupported_type():
2636
with pytest.raises(TypeError, match=r'.*Callable\[\[\], str\].*'):
2737
TypeDef.load(typing.Callable[[], str], lambda x: x)
@@ -197,6 +207,29 @@ class Foo:
197207
assert TypeDef.dump(typing.Union[pathlib.Path, None], None) == None
198208

199209

210+
def test_complex_optional_union():
211+
@dataclass
212+
class Foo:
213+
bar: int = 1
214+
215+
assert TypeDef.load(typing.Optional[typing.Union[pathlib.Path, Foo]], '/bin') == pathlib.Path('/bin')
216+
assert TypeDef.load(typing.Optional[typing.Union[pathlib.Path, Foo]], {'bar': 2}).bar == 2
217+
assert TypeDef.load(typing.Optional[typing.Union[pathlib.Path, Foo]], None) == None
218+
219+
assert TypeDef.load(typing.Union[pathlib.Path, Foo, type(None)], None) == None
220+
with pytest.raises(ValidationError, match='are exhausted'):
221+
TypeDef.load(typing.Union[pathlib.Path, Foo, type(None)], [1, 2.5, '3'])
222+
223+
with pytest.raises(ValidationError, match='are exhausted'):
224+
TypeDef.dump(typing.Union[pathlib.Path, Foo, type(None)], "/bin")
225+
226+
assert TypeDef.dump(typing.Optional[typing.Union[pathlib.Path, str]], '/bin') == '/bin'
227+
assert TypeDef.dump(typing.Optional[typing.Union[pathlib.Path, Foo]], Foo(bar=2))['bar'] == 2
228+
assert TypeDef.dump(typing.Optional[typing.Union[pathlib.Path, Foo]], None) == None
229+
230+
assert TypeDef.dump(typing.Union[pathlib.Path, Foo, type(None)], None) == None
231+
232+
200233
def test_class_config():
201234
class module:
202235
def __init__(self, a, b, c=1):
@@ -259,3 +292,4 @@ def __init__(self, a: typing.Union[submodule, ClassConfig[submodule]],
259292
{'a': {'a': 1, 'b': 2}, 'b': {'a': 3, 'b': 4, 'c': 5}}).b.c == 5
260293
assert TypeDef.load(ClassConfig[module],
261294
{'a': {'a': 1, 'b': 2}, 'b': {'a': 3, 'b': 4, 'c': 5}}).build().b._c == 5
295+

utilsd/config/type_def.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ def to_plain(self, obj, ctx):
209209
return obj
210210

211211

212+
class NoneTypeDef(TypeDef):
213+
@classmethod
214+
def new(cls, type_):
215+
if type_ is type(None):
216+
return cls(type_)
217+
return None
218+
219+
def from_plain(self, plain, ctx):
220+
ctx.mark_cli_anchor_point(type(None))
221+
return plain
222+
223+
def to_plain(self, obj, ctx):
224+
if obj is None:
225+
return None
226+
else:
227+
raise TypeError(f'Expected None, got {obj}')
228+
229+
212230
class OptionalDef(TypeDef):
213231
@classmethod
214232
def new(cls, type_):
@@ -660,6 +678,7 @@ def to_plain(self, obj, ctx):
660678

661679
# register all the modules in this file
662680
TypeDefRegistry.register_module(module=AnyDef)
681+
TypeDefRegistry.register_module(module=NoneTypeDef)
663682
TypeDefRegistry.register_module(module=OptionalDef)
664683
TypeDefRegistry.register_module(module=PathDef)
665684
TypeDefRegistry.register_module(module=ListDef)

0 commit comments

Comments
 (0)