Skip to content

Commit

Permalink
Adapt code to pydantic-v2 (#27)
Browse files Browse the repository at this point in the history
* Adapt code to pydantic-v2

* Added more strict typing
  • Loading branch information
wolflu05 authored Jul 27, 2023
1 parent 69358e8 commit 9950b00
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions inventree_bulk_plugin/BulkGenerator/BulkGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions inventree_bulk_plugin/BulkGenerator/dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions inventree_bulk_plugin/BulkGenerator/generators/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion inventree_bulk_plugin/BulkGenerator/utils.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def version_tuple(v):
def version_tuple(v: str):
return tuple(map(int, v.split(".")))
17 changes: 9 additions & 8 deletions inventree_bulk_plugin/BulkGenerator/validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand All @@ -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"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
packages=setuptools.find_packages(),

install_requires=[
"pydantic==1.*"
"pydantic==2.*"
],

setup_requires=[
Expand Down

0 comments on commit 9950b00

Please sign in to comment.