Skip to content

Commit

Permalink
feat: remove type set on assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Mikhaylov committed Oct 8, 2020
1 parent bd4c66d commit d3ed0d7
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ntc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .leaf import *
from .node import *

__version__ = "0.4.1"
__version__ = "0.5.0"
15 changes: 8 additions & 7 deletions ntc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,15 @@ def _set_new(self, key: str, value: Any) -> None:
value_to_set = value
elif isinstance(value, type):
if leaf_spec:
value_to_set = CfgLeaf(
value if leaf_spec.subclass else None,
value, # Need to pass value here instead of copying, in case new value is more restrictive
subclass=leaf_spec.subclass,
required=leaf_spec.required,
)
required = leaf_spec.required
else:
value_to_set = CfgLeaf(None, value)
required = True
value_to_set = CfgLeaf(
value,
value, # Need to pass value here instead of copying from spec, in case new value is more restrictive
subclass=True,
required=required,
)
elif leaf_spec:
value_to_set = leaf_spec.clone()
value_to_set.value = value
Expand Down
11 changes: 11 additions & 0 deletions tests/data/bad/bad_node_nested_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ntc import CN

from ..good.good import cfg


class BadSubClass:
pass


cfg = CN(cfg)
cfg.SUBCLASSES.ONE = BadSubClass
11 changes: 11 additions & 0 deletions tests/data/bad/bad_node_required_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ntc import CN

from ..good.good import cfg


class BadSubClass:
pass


cfg = CN(cfg)
cfg.SUBCLASS = None
2 changes: 1 addition & 1 deletion tests/data/bad/bad_node_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class BadSubClass:


cfg = CN(cfg)
cfg.SUBCLASSES.ONE = BadSubClass
cfg.SUBCLASS = BadSubClass
1 change: 1 addition & 0 deletions tests/data/base_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
cfg.LIST = [1, 2, 3, 4]
cfg.CLASS = BaseClass()
cfg.CLASSES = CN(BaseClass)
cfg.SUBCLASS = BaseClass
cfg.SUBCLASSES = CN(CL(None, BaseClass, subclass=True))


Expand Down
7 changes: 7 additions & 0 deletions tests/data/good/node_nested_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ntc import CN
from tests.data.base_class import SubClass

from .good import cfg

cfg = CN(cfg)
cfg.SUBCLASSES.ONE = SubClass
2 changes: 1 addition & 1 deletion tests/data/good/node_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .good import cfg

cfg = CN(cfg)
cfg.SUBCLASSES.ONE = SubClass
cfg.SUBCLASS = SubClass
14 changes: 12 additions & 2 deletions tests/test_bad.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ntc import CN, ModuleError, SchemaError, SchemaFrozenError, TypeMismatch
from ntc import CN, MissingRequired, ModuleError, SchemaError, SchemaFrozenError, TypeMismatch

DATA_DIR = Path(__file__).parent / "data" / "bad"

Expand All @@ -28,10 +28,20 @@ def test_bad_node():


def test_bad_node_subclass():
with pytest.raises(SchemaError):
with pytest.raises(TypeMismatch):
CN.load(DATA_DIR / "bad_node_subclass.py")


def test_bad_node_required_subclass():
with pytest.raises(MissingRequired):
CN.load(DATA_DIR / "bad_node_required_subclass.py")


def test_bad_node_nested_subclass():
with pytest.raises(SchemaError):
CN.load(DATA_DIR / "bad_node_nested_subclass.py")


def test_bad_node_instance():
with pytest.raises((TypeMismatch, SchemaError)):
CN.load(DATA_DIR / "bad_node_instance.py")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_good.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_freeze_loaded():

def test_node_subclass():
cfg = CN.load(DATA_DIR / "node_subclass.py")
assert issubclass(cfg.SUBCLASS, SubClass)


def test_node_nested_subclass():
cfg = CN.load(DATA_DIR / "node_nested_subclass.py")
assert issubclass(cfg.SUBCLASSES.ONE, SubClass)


Expand Down
27 changes: 23 additions & 4 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def __str__(self):
return "quux"


class Foo:
pass


class FooSubclass(Foo):
pass


@pytest.fixture
def basic_cfg():
cfg = CfgNode()
Expand All @@ -29,13 +37,24 @@ def test_define_with_value(basic_cfg):
def test_define_with_type():
cfg = CfgNode()

cfg.FOO = str
cfg.FOO = Foo

with pytest.raises(TypeMismatch):
cfg.FOO = Quux

cfg.FOO = FooSubclass
assert cfg.FOO == FooSubclass


def test_define_with_type_and_leaf():
cfg = CfgNode(CfgLeaf(Foo, Foo, subclass=True))

cfg.FOO = FooSubclass

with pytest.raises(TypeMismatch):
cfg.FOO = 42
cfg.FOO = Foo

cfg.FOO = "foo"
assert cfg.FOO == "foo"
assert cfg.FOO == FooSubclass


def test_define_with_leaf():
Expand Down

0 comments on commit d3ed0d7

Please sign in to comment.