From 4ae194f673d7b8456771ca670b219bbba64f58b9 Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Sun, 27 Oct 2024 12:00:06 +0100 Subject: [PATCH] Add pylint checks --- .github/workflows/python-package.yml | 2 ++ onedm/sdf/data.py | 34 ++++++++++++++-------------- onedm/sdf/definitions.py | 2 +- onedm/sdf/document.py | 12 ++++++---- onedm/sdf/loader.py | 2 +- pyproject.toml | 20 ++++++++++++++++ requirements.txt | 1 + 7 files changed, 49 insertions(+), 24 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index c783a8c..33a3add 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -33,6 +33,8 @@ jobs: run: flake8 onedm - name: Lint with mypy run: mypy onedm + - name: Lint with pylint + run: pylint onedm - name: Check formatting run: black --check onedm - name: Test with pytest diff --git a/onedm/sdf/data.py b/onedm/sdf/data.py index 4dcbfd3..1b43795 100644 --- a/onedm/sdf/data.py +++ b/onedm/sdf/data.py @@ -60,9 +60,9 @@ def get_pydantic_schema(self) -> core_schema.CoreSchema: schema = core_schema.nullable_schema(schema) return schema - def validate_input(self, input: Any) -> Any: + def validate_input(self, value: Any) -> Any: """Validate and coerce a value.""" - return SchemaValidator(self.get_pydantic_schema()).validate_python(input) + return SchemaValidator(self.get_pydantic_schema()).validate_python(value) class NumberData(DataQualities): @@ -77,8 +77,8 @@ class NumberData(DataQualities): default: float | None = None @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.CoreSchema: if self.sdf_type == "unix-time": @@ -128,8 +128,8 @@ class IntegerData(DataQualities): _enum = None @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.IntSchema: return core_schema.int_schema( @@ -158,12 +158,12 @@ def to_enum(self) -> EnumMeta | None: ) return self._enum - def validate_input(self, input: Any) -> IntEnum | int: - value = super().validate_input(input) + def validate_input(self, value: Any) -> IntEnum | int: + value = super().validate_input(value) # Convert to enum.IntEnum if possible if enum_cls := self.to_enum(): try: - value = enum_cls(value) + return enum_cls(value) except ValueError: # Value is valid but not a specific enum value pass @@ -179,8 +179,8 @@ class BooleanData(DataQualities): ) @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.BoolSchema: return core_schema.bool_schema() @@ -201,8 +201,8 @@ class StringData(DataQualities): default: str | None = None @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.CoreSchema: if self.enum is not None: @@ -238,8 +238,8 @@ class ArrayData(DataQualities): default: list | None = None @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.ListSchema | core_schema.SetSchema: if self.unique_items: @@ -263,8 +263,8 @@ class ObjectData(DataQualities): default: dict[str, Any] | None = None @field_serializer("type") - def always_include_type(self, type: str, _): - return type + def always_include_type(self, type_: str, _): + return type_ def _get_base_schema(self) -> core_schema.CoreSchema: if self.properties is None: diff --git a/onedm/sdf/definitions.py b/onedm/sdf/definitions.py index dd579fa..dd54fd0 100644 --- a/onedm/sdf/definitions.py +++ b/onedm/sdf/definitions.py @@ -17,7 +17,7 @@ ) -class PropertyBase: +class PropertyBase: # pylint: disable=too-few-public-methods observable: bool = True readable: bool = True writable: bool = True diff --git a/onedm/sdf/document.py b/onedm/sdf/document.py index faba88c..ab69e4b 100644 --- a/onedm/sdf/document.py +++ b/onedm/sdf/document.py @@ -50,7 +50,7 @@ class Document(BaseModel): extra="allow", alias_generator=to_camel, populate_by_name=True ) - info: Information = Field(default_factory=lambda: Information()) + info: Information = Field(default_factory=Information) namespace: dict[str, str] = Field( default_factory=dict, description=( @@ -113,9 +113,11 @@ def populate_sdf_data(self, data: dict, nxt): """ data = data.copy() - def update_from_parent(parent): - for property in parent.properties.values(): - data.update(property.definitions) + def update_from_parent( + parent: Document | definitions.Object | definitions.Thing, + ): + for prop in parent.properties.values(): + data.update(prop.definitions) for action in parent.actions.values(): if action.input_data: data.update(action.input_data.definitions) @@ -132,7 +134,7 @@ def update_from_parent(parent): update_from_parent(obj) update_from_parent(self) - for thing in self.things.values(): + for thing in self.things.values(): # pylint: disable=no-member update_from_parent(thing) return nxt(data) diff --git a/onedm/sdf/loader.py b/onedm/sdf/loader.py index 62203a9..2afe53b 100644 --- a/onedm/sdf/loader.py +++ b/onedm/sdf/loader.py @@ -24,7 +24,7 @@ def __init__(self) -> None: def load_file(self, path): self.url = str(path) - with open(path, "r") as fp: + with open(path, "r", encoding="utf-8") as fp: self.load_from_fp(fp) def load_from_fp(self, fp: io.TextIOBase): diff --git a/pyproject.toml b/pyproject.toml index d826f24..f8bf9d5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,3 +29,23 @@ Issues = "https://github.com/christiansandberg/onedm/issues" [build-system] requires = ["setuptools>=64", "setuptools_scm>=8"] build-backend = "setuptools.build_meta" + +[tool.isort] +profile = "black" + +[tool.pylint."messages control"] +disable = [ + "missing-module-docstring", + "missing-class-docstring", + "missing-function-docstring", + "too-many-instance-attributes", + "too-many-return-statements", + "duplicate-code", + "fixme", +] + +[tool.pylint.format] +max-line-length = 88 + +[tool.mypy] +plugins = "pydantic.mypy" diff --git a/requirements.txt b/requirements.txt index 30f7e5d..329b4f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pytest flake8 mypy +pylint black