From 9950b00b436164f3bd39a3bf076bd9c702db2fca Mon Sep 17 00:00:00 2001 From: Lukas <76838159+wolflu05@users.noreply.github.com> Date: Thu, 27 Jul 2023 19:34:46 +0200 Subject: [PATCH] Adapt code to pydantic-v2 (#27) * Adapt code to pydantic-v2 * Added more strict typing --- .github/workflows/ci.yml | 2 +- .../BulkGenerator/BulkGenerator.py | 18 ++++++++++-------- .../BulkGenerator/dimensions.py | 4 ++-- .../BulkGenerator/generators/gen_alpha.py | 2 +- .../BulkGenerator/generators/generator.py | 6 +++--- inventree_bulk_plugin/BulkGenerator/utils.py | 2 +- .../BulkGenerator/validations.py | 17 +++++++++-------- setup.py | 2 +- 8 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 198838a..133cf9a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,7 +45,7 @@ jobs: - name: Install test dependencies run: | - pip install pydantic==1.9.1 + pip install pydantic==2.* pip install coverage==7.0.1 - name: Run tests diff --git a/inventree_bulk_plugin/BulkGenerator/BulkGenerator.py b/inventree_bulk_plugin/BulkGenerator/BulkGenerator.py index 566f1d0..567d727 100644 --- a/inventree_bulk_plugin/BulkGenerator/BulkGenerator.py +++ b/inventree_bulk_plugin/BulkGenerator/BulkGenerator.py @@ -2,7 +2,7 @@ import re from ..version import BULK_PLUGIN_VERSION -from .validations import BulkDefinitionSchema +from .validations import BulkDefinitionChild, BulkDefinitionChildCount, BulkDefinitionChildDimensions, BulkDefinitionChildTemplate, BulkDefinitionSchema from .dimensions import get_dimension_values from .utils import version_tuple @@ -16,8 +16,8 @@ def __getattr__(*args): __delattr__ = dict.__delitem__ -def apply_template(obj, template): - for k in template.__fields__.keys(): +def apply_template(obj: BulkDefinitionChild, template: BulkDefinitionChildTemplate): + for k in template.model_fields.keys(): # only templates have names if k == "name": continue @@ -55,7 +55,7 @@ def apply_template(obj, template): class BulkGenerator: def __init__(self, inp): self.inp = inp - self.schema = None + self.schema: BulkDefinitionSchema = None def generate(self): self.validate() @@ -71,7 +71,9 @@ def validate(self): raise ValueError( f"The server runs on v{BULK_PLUGIN_VERSION} which is incompatible to v{self.schema.version}.") - def parse_child(self, child): + ParseChildReturnType = list[tuple[dict[str, str], list["ParseChildReturnType"]]] + + def parse_child(self, child: BulkDefinitionChild) -> ParseChildReturnType: res = [] # merge extend template @@ -128,17 +130,17 @@ def parse_child(self, child): return res - def generate_product(self, dimensions, count, child): + def generate_product(self, dimensions: BulkDefinitionChildDimensions, count: BulkDefinitionChildCount, child: BulkDefinitionChild): seq = [] for d, c in itertools.zip_longest(dimensions, count, fillvalue=None): seq.append(get_dimension_values(d, c, self.schema.settings)) return itertools.product(*seq, repeat=1) - def get_generated(self, child, ctx={}): + def get_generated(self, child: BulkDefinitionChild, ctx: dict = {}): return dict([a, self.parse_str(x, ctx)] for a, x in child.generate.items()) - def parse_str(self, string, ctx={}): + def parse_str(self, string: str, ctx: dict = {}): ctx = {'inp': DotDict(self.schema.input), **ctx} try: diff --git a/inventree_bulk_plugin/BulkGenerator/dimensions.py b/inventree_bulk_plugin/BulkGenerator/dimensions.py index 805ab03..55fd2cf 100644 --- a/inventree_bulk_plugin/BulkGenerator/dimensions.py +++ b/inventree_bulk_plugin/BulkGenerator/dimensions.py @@ -6,7 +6,7 @@ from .generators.generator import Generator, GeneratorTypes -def parse_dimension(dimension): +def parse_dimension(dimension: str) -> list[tuple[GeneratorTypes, Union[str, tuple[str, str]], dict, str]]: res = [] for gen_match in re.finditer(r"(?:(?:(\w+)-(\w+))|(\*?\w+))(?:{(.*?)})?(?:,|$)", dimension): settings = {} @@ -27,7 +27,7 @@ def parse_dimension(dimension): return res -def match_generator(generators, gen_type: GeneratorTypes, gen: Union[str, Tuple[str, str]]) -> Union[Generator, None]: +def match_generator(generators: list[Generator], gen_type: GeneratorTypes, gen: Union[str, Tuple[str, str]]) -> Union[Generator, None]: for generator in generators: if gen_type == GeneratorTypes.INFINITY and generator.NAME == gen: return generator diff --git a/inventree_bulk_plugin/BulkGenerator/generators/gen_alpha.py b/inventree_bulk_plugin/BulkGenerator/generators/gen_alpha.py index 5346724..35c919d 100644 --- a/inventree_bulk_plugin/BulkGenerator/generators/gen_alpha.py +++ b/inventree_bulk_plugin/BulkGenerator/generators/gen_alpha.py @@ -42,7 +42,7 @@ def generator(self): yield ''.join(p) @classmethod - def _get_alpha_index(cls, x) -> int: + def _get_alpha_index(cls, x: str) -> int: """Return the zero-based index of alphanumeric values.""" if x == "": return 0 diff --git a/inventree_bulk_plugin/BulkGenerator/generators/generator.py b/inventree_bulk_plugin/BulkGenerator/generators/generator.py index 1eec0f0..8c9c5ee 100644 --- a/inventree_bulk_plugin/BulkGenerator/generators/generator.py +++ b/inventree_bulk_plugin/BulkGenerator/generators/generator.py @@ -12,9 +12,9 @@ class GeneratorTypes(IntEnum): class BaseSettingsSchema(BaseModel): - start: Optional[str] - end: Optional[str] - count: Optional[int] + start: Optional[str] = None + end: Optional[str] = None + count: Optional[int] = None step: Optional[int] = 1 diff --git a/inventree_bulk_plugin/BulkGenerator/utils.py b/inventree_bulk_plugin/BulkGenerator/utils.py index 3008978..352cd22 100755 --- a/inventree_bulk_plugin/BulkGenerator/utils.py +++ b/inventree_bulk_plugin/BulkGenerator/utils.py @@ -1,2 +1,2 @@ -def version_tuple(v): +def version_tuple(v: str): return tuple(map(int, v.split("."))) diff --git a/inventree_bulk_plugin/BulkGenerator/validations.py b/inventree_bulk_plugin/BulkGenerator/validations.py index 18e8932..ffd44b3 100644 --- a/inventree_bulk_plugin/BulkGenerator/validations.py +++ b/inventree_bulk_plugin/BulkGenerator/validations.py @@ -2,13 +2,17 @@ from pydantic import BaseModel, PrivateAttr +BulkDefinitionChildDimensions = Optional[List[str]] +BulkDefinitionChildCount = Optional[List[Union[int, None]]] + + class BulkDefinitionChild(BaseModel): parent_name_match: Optional[str] = ".*" - extends: Optional[str] - dimensions: Optional[List[str]] = [] + extends: Optional[str] = None + dimensions: BulkDefinitionChildDimensions = [] generate: Optional[Dict[str, str]] = {} - count: Optional[List[Union[int, None]]] = [] - child: Optional["BulkDefinitionChild"] + count: BulkDefinitionChildCount = [] + child: Optional["BulkDefinitionChild"] = None childs: Optional[List["BulkDefinitionChild"]] = [] _generated: List[Tuple[Dict[str, str], "_generated"]] = PrivateAttr([]) @@ -24,12 +28,9 @@ class BulkDefinitionSettings(BaseModel): leading_zeros: Optional[bool] = True -BulkDefinitionChildTemplate - - class BulkDefinitionSchema(BaseModel): version: str - input: Dict[str, Union[int, str]] + input: Dict[str, str] settings: Optional[BulkDefinitionSettings] = BulkDefinitionSettings() templates: List["BulkDefinitionChildTemplate"] output: "BulkDefinitionChild" diff --git a/setup.py b/setup.py index 56dedb7..122bf52 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ packages=setuptools.find_packages(), install_requires=[ - "pydantic==1.*" + "pydantic==2.*" ], setup_requires=[