Skip to content

Commit

Permalink
Merge branch 'refactor_style_fixes' into 'master'
Browse files Browse the repository at this point in the history
refactor: flake8-guided style fixes

See merge request utilities/ntc!46
  • Loading branch information
Artem Vasenin committed Apr 12, 2022
2 parents f21f0a9 + c0bae7e commit 2a331b7
Show file tree
Hide file tree
Showing 100 changed files with 260 additions and 61 deletions.
2 changes: 2 additions & 0 deletions ntc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" NeuroTrade Config Library """
from __future__ import annotations

from .errors import *
from .interfaces import *
from .leaf import *
Expand Down
39 changes: 15 additions & 24 deletions ntc/errors.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
from __future__ import annotations


class ConfigError(Exception):
pass


class ConfigUseError(ConfigError, ValueError):
pass


class TypeMismatch(ConfigError):
"""
Wrong type is used
"""
"""Wrong type is used"""


class NodeReassignment(ConfigError):
"""
Assigning value to node item
"""
"""Assigning value to node item"""


class ModuleError(ConfigError):
"""
Incorrectly specified config, can't import
"""
"""Incorrectly specified config, can't import"""


class SchemaError(ConfigError):
"""
Incorrectly specified schema
"""
"""Incorrectly specified schema"""


class SchemaFrozenError(SchemaError):
"""
Trying to add new items after schema has been frozen
"""
"""Trying to add new items after schema has been frozen"""


class SpecError(ConfigError):
"""
Value which is assigned does not match leaf_spec
"""
"""Value which is assigned does not match leaf_spec"""


class SaveError(ConfigError):
"""
Can't save
"""
"""Can't save"""


class ValidationError(ConfigError):
"""
Config restrictions are not respected
"""
"""Config restrictions are not respected"""


class MissingRequired(ValidationError):
Expand Down
3 changes: 3 additions & 0 deletions ntc/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations


class CfgSavable:
"""
Class to indicate that an instance will be saveable by Cfg
Expand Down
29 changes: 15 additions & 14 deletions ntc/leaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from typing import Any

from ntc.errors import MissingRequired, SchemaError, TypeMismatch
from ntc.errors import ConfigUseError, MissingRequired, SchemaError, TypeMismatch

from .utils import full_type_name

Expand Down Expand Up @@ -57,7 +57,7 @@ def __str__(self):
return result

@property
def type(self) -> type:
def type(self) -> type: # noqa: A003 # "type" at class-level isn't so bad.
return self._type

@property
Expand All @@ -73,20 +73,21 @@ def value(self) -> Any:
return self._value

@value.setter
def value(self, val) -> None:
if self._required and val is None:
raise MissingRequired(f"Can't set required value to None for {self}")
if val is not None:
check_val = val.func if isinstance(val, partial) else val
def value(self, new_value) -> None:
if new_value is None:
if self._required:
raise MissingRequired(f"Can't set required value to None for {self}")
else:
check_val = new_value.func if isinstance(new_value, partial) else new_value
expected_type = full_type_name(self._type)
if self._subclass and (not isinstance(check_val, type) or not issubclass(check_val, self._type)):
raise TypeMismatch(f"Subclass of type <{expected_type}> expected, but {check_val!r} found for {self}!")
if not self._subclass and not isinstance(check_val, self._type):
raise TypeMismatch(f"Instance of type <{expected_type}> expected, but {check_val!r} found for {self}!")
self._value = val
self._value = new_value

if self._parent:
self._parent._update_module(self._full_key, val)
self._parent._update_module(self._full_key, new_value)

@property
def full_key(self):
Expand All @@ -95,7 +96,7 @@ def full_key(self):
@full_key.setter
def full_key(self, value: str):
if self._full_key and value != self._full_key:
raise ValueError(f"full_key cannot be reassigned for leaf {self}")
raise ConfigUseError(f"full_key cannot be reassigned for leaf {self}")
self._full_key = value

@property
Expand Down Expand Up @@ -132,10 +133,10 @@ def check(self, leaf_spec: CfgLeaf) -> None:
raise SchemaError(f"Value of {self} must be an instance of {leaf_spec.type}")

def __eq__(self, other: CfgLeaf):
for attr_name in ["_type", "_required", "_subclass", "_value", "_desc"]:
if getattr(self, attr_name) != getattr(other, attr_name):
return False
return True
return all(
getattr(self, attr_name) == getattr(other, attr_name)
for attr_name in ("_type", "_required", "_subclass", "_value", "_desc")
)


CL = CfgLeaf
Expand Down
18 changes: 13 additions & 5 deletions ntc/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

import yaml

from ntc.errors import MissingRequired, NodeReassignment, SaveError, SchemaError, SchemaFrozenError, ValidationError
from ntc.errors import (
ConfigUseError,
MissingRequired,
NodeReassignment,
SaveError,
SchemaError,
SchemaFrozenError,
ValidationError,
)
from ntc.interfaces import CfgSavable
from ntc.utils import add_yaml_str_representer, import_module, merge_cfg_module

Expand Down Expand Up @@ -260,14 +268,14 @@ def full_key(self):
@full_key.setter
def full_key(self, value: str):
if self._full_key and value != self._full_key:
raise ValueError(f"full_key cannot be reassigned for node at {self._full_key}")
raise ConfigUseError(f"full_key cannot be reassigned for node at {self._full_key}")
self._full_key = value

def describe(self, key: str = None) -> str:
if key is None:
return self._desc
if key not in self:
raise ValueError(f"{key!r} key does not exist")
raise ConfigUseError(f"{key!r} key does not exist")

attr = super().__getitem__(key)
if isinstance(attr, CfgNode):
Expand Down Expand Up @@ -335,14 +343,14 @@ def _init_with_base(self, base: Union[dict, CfgNode]) -> None:
value = CfgNode(value, full_key=self._build_child_key(key))
self[key] = value
else:
raise ValueError("This should not happen!")
raise Exception("This should not happen!")

def _build_child_key(self, key: str) -> str:
return f"{self.full_key}.{key}"

def __reduce__(self):
if not self.schema_frozen:
raise ValueError(f"Can't pickle unfrozen CfgNode: {self.full_key}")
raise ConfigUseError(f"Can't pickle unfrozen CfgNode: {self.full_key}")
state = {}
for attr_name in self._BUILT_IN_ATTRS:
state[attr_name] = getattr(self, attr_name)
Expand Down
2 changes: 2 additions & 0 deletions ntc/transforms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path
Expand Down
2 changes: 2 additions & 0 deletions ntc/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import importlib.util
import sys
from pathlib import Path
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ dependencies = [

[project.optional-dependencies]
tests = [
"nt-dev>=0.32",
"nt-dev>=0.34",
"types-pyyaml",
]
dev = [
"nt-dev>=0.32",
"nt-dev>=0.34",
]

[tool.flit.sdist]
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from __future__ import annotations
1 change: 1 addition & 0 deletions tests/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from __future__ import annotations
1 change: 1 addition & 0 deletions tests/data/bad/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from __future__ import annotations
2 changes: 2 additions & 0 deletions tests/data/bad/bad_attr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_cfg_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..base_cfg import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_class.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..base_class import BaseClass
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..base_cfg import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_changes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.bad.bad_inherit import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CL

from ..base_cfg import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_instance_changes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.bad.bad_inherit_instance import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CL

from ..base_cfg import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass_changes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.bad.bad_inherit_subclass import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass_class.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ..base_cfg import cfg
from ..base_class import BaseClass

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass_class_changes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.bad.bad_inherit_subclass import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass_instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CL

from ..base_cfg import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_inherit_subclass_instance_changes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.bad.bad_inherit_subclass_instance import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_init.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

cfg = CN("Bad")
2 changes: 2 additions & 0 deletions tests/data/bad/bad_init_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ..good.good import cfg

cfg = cfg.inherit()
2 changes: 2 additions & 0 deletions tests/data/bad/bad_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_node_instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

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

Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_node_nested_subclass.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_node_required_subclass.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_node_subclass.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/bad_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN

from ..good.good import cfg
Expand Down
2 changes: 2 additions & 0 deletions tests/data/bad/inheritance_changes_bad.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CN
from tests.data.good.inheritance import cfg

Expand Down
2 changes: 2 additions & 0 deletions tests/data/base_cfg.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CL, CN

from .base_class import BaseClass
Expand Down
1 change: 1 addition & 0 deletions tests/data/description/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from __future__ import annotations
2 changes: 2 additions & 0 deletions tests/data/description/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from ntc import CL, CN
from tests.data.base_cfg import cfg

Expand Down
Loading

0 comments on commit 2a331b7

Please sign in to comment.