Skip to content

Commit

Permalink
feat[leaf.py]: Added checks to work with functools.partial
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vasenin committed Nov 29, 2020
1 parent 291d8fa commit 4b950ed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 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.11.0"
__version__ = "0.11.1"
14 changes: 8 additions & 6 deletions ntc/leaf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from copy import deepcopy
from functools import partial
from typing import Any, Type

from ntc.errors import MissingRequired, SchemaError, TypeMismatch
Expand Down Expand Up @@ -57,17 +58,18 @@ def value(self, val) -> None:
if self._required and val is None:
raise MissingRequired(f"Can't set required value to None for key {self.full_key}")
if val is not None:
if self._subclass and not isinstance(val, type):
check_val = val.func if isinstance(val, partial) else val
if self._subclass and not isinstance(check_val, type):
raise TypeMismatch(
f"Subclass of type {self._type} expected, but {val!r} found for key {self.full_key}!"
f"Subclass of type {self._type} expected, but {check_val!r} found for key {self.full_key}!"
)
if self._subclass and not issubclass(val, self._type):
if self._subclass and not issubclass(check_val, self._type):
raise TypeMismatch(
f"Subclass of type {self._type} expected, but {val!r} found for key {self.full_key}!"
f"Subclass of type {self._type} expected, but {check_val!r} found for key {self.full_key}!"
)
if not self._subclass and not isinstance(val, self._type):
if not self._subclass and not isinstance(check_val, self._type):
raise TypeMismatch(
f"Instance of type {self._type} expected, but {val!r} found for key {self.full_key}!"
f"Instance of type {self._type} expected, but {check_val!r} found for key {self.full_key}!"
)
self._value = val

Expand Down
9 changes: 9 additions & 0 deletions tests/data/good/node_partial_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from functools import partial

from ntc import CN
from tests.data.base_class import SubClass

from .good import cfg

cfg = CN(cfg)
cfg.SUBCLASS = partial(SubClass)
7 changes: 7 additions & 0 deletions tests/test_good.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import partial
from pathlib import Path

import pytest
Expand Down Expand Up @@ -79,6 +80,12 @@ def test_node_nested_subclass():
assert issubclass(cfg.SUBCLASSES.ONE, SubClass)


def test_node_partial_subclass():
cfg = CN.load(DATA_DIR / "node_partial_subclass.py")
assert isinstance(cfg.SUBCLASS, partial)
assert issubclass(cfg.SUBCLASS.func, SubClass)


def test_inheritance_changes():
cfg = CN.load(DATA_DIR / "inheritance_changes.py")
assert cfg.DICT.X == "Y"
Expand Down

0 comments on commit 4b950ed

Please sign in to comment.