From 7d412b4b196c598bd55f9b3b61673083e78b2182 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Wed, 10 Jan 2024 07:45:41 +0100 Subject: [PATCH] Ruff fixes --- .ruff.toml | 33 ++++++++++++++------- docs/conf.py | 6 ++-- setup.py | 3 +- src/easyconfig/config_objs/app_config.py | 5 ++-- src/easyconfig/config_objs/object_config.py | 1 + src/easyconfig/config_objs/subscription.py | 1 + src/easyconfig/create_app_config.py | 3 +- src/easyconfig/errors/handler.py | 2 +- src/easyconfig/expansion/expand.py | 1 + src/easyconfig/expansion/load_file.py | 1 + src/easyconfig/expansion/location.py | 1 + src/easyconfig/models/app.py | 1 + src/easyconfig/models/config.py | 1 + src/easyconfig/yaml/align.py | 2 +- src/easyconfig/yaml/from_model.py | 2 -- src/easyconfig/yaml/yaml.py | 1 + tests/helper/my_path.py | 3 +- tests/test_implementation/test_load_file.py | 3 +- tests/yaml/test_model_to_yaml.py | 2 -- 19 files changed, 46 insertions(+), 26 deletions(-) diff --git a/.ruff.toml b/.ruff.toml index f65aa5a..038b024 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -36,12 +36,14 @@ select = [ "PERF", # https://docs.astral.sh/ruff/rules/#perflint-perf "RUF", # https://docs.astral.sh/ruff/rules/#ruff-specific-rules-ruf - # "PL", # https://docs.astral.sh/ruff/rules/#pylint-pl - # "FURB", # https://docs.astral.sh/ruff/rules/#refurb-furb + "PL", # https://docs.astral.sh/ruff/rules/#pylint-pl ] ignore = [ - "RET501" # https://docs.astral.sh/ruff/rules/unnecessary-return-none/#unnecessary-return-none-ret501 + "A003", # https://docs.astral.sh/ruff/rules/builtin-attribute-shadowing/ + "RET501", # https://docs.astral.sh/ruff/rules/unnecessary-return-none/#unnecessary-return-none-ret501 + "TRY400", # https://docs.astral.sh/ruff/rules/error-instead-of-exception/ + "PLR1711", # https://docs.astral.sh/ruff/rules/useless-return/ ] @@ -50,15 +52,24 @@ ignore = [ quote-style = "single" -[lint.isort] -known-local-folder = ["easyconfig"] -known-first-party = ["tests", "helper"] +[lint.per-file-ignores] +"docs/*" = [ + "A001", # A001 Variable `copyright` is shadowing a Python builtin + "E402", # E402 Module level import not at top of file + "INP001", # INP001 File `FILE_NAME` is part of an implicit namespace package. Add an `__init__.py`. +] +"tests/*" = [ + "INP001", # INP001 File `FILE_NAME` is part of an implicit namespace package. Add an `__init__.py`. + "ISC002", # ISC002 Implicitly concatenated string literals over multiple lines + "PLR2004", # PLR2004 Magic value used in comparison, consider replacing 5 with a constant variable +] -[lint.flake8-builtins] -builtins-ignorelist = ["id"] +"setup.py" = ["PTH123"] +"src/easyconfig/yaml/from_model.py" = ["PLR0911"] # PLR0911 Too many return statements (7 > 6) -[lint.per-file-ignores] -"docs/conf.py" = ["INP001", "A001"] -"setup.py" = ["PTH123"] + +[lint.isort] +# https://docs.astral.sh/ruff/settings/#isort-lines-after-imports +lines-after-imports = 2 diff --git a/docs/conf.py b/docs/conf.py index d88b94a..ac34557 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -13,17 +13,19 @@ import sys from pathlib import Path + src_folder = Path(__file__).parent.with_name('src') assert src_folder.is_dir() # required for autodoc sys.path.insert(0, str(src_folder)) -import easyconfig # noqa: E402 +import easyconfig + # -- Project information ----------------------------------------------------- project = 'easyconfig' -copyright = '2023, spacemanspiff2007' +copyright = '2024, spacemanspiff2007' author = 'spacemanspiff2007' # The full version, including alpha/beta/rc tags diff --git a/setup.py b/setup.py index 25271ac..d0cccf4 100644 --- a/setup.py +++ b/setup.py @@ -35,7 +35,7 @@ def load_req() -> typing.List[str]: author='spaceman_spiff', # author_email="", description='Easy application configuration with yaml files', - keywords=['yaml', 'configuration', 'pydantic', 'settings', 'config'], + keywords=['yaml', 'configuration', 'pydantic', 'settings', 'config', 'yml'], long_description=long_description, long_description_content_type='text/markdown', url='https://github.com/spacemanspiff2007/easyconfig', @@ -57,6 +57,7 @@ def load_req() -> typing.List[str]: 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', 'Programming Language :: Python :: 3 :: Only', 'Topic :: Software Development :: Libraries', ], diff --git a/src/easyconfig/config_objs/app_config.py b/src/easyconfig/config_objs/app_config.py index 7f26f89..aff39f4 100644 --- a/src/easyconfig/config_objs/app_config.py +++ b/src/easyconfig/config_objs/app_config.py @@ -4,12 +4,13 @@ from pathlib import Path from typing import TYPE_CHECKING -from ..errors import FileDefaultsNotSetError -from .object_config import ConfigObj from easyconfig.__const__ import MISSING, MISSING_TYPE +from easyconfig.config_objs.object_config import ConfigObj +from easyconfig.errors import FileDefaultsNotSetError from easyconfig.expansion import expand_obj from easyconfig.yaml import CommentedMap, cmap_from_model, write_aligned_yaml, yaml_rt + if TYPE_CHECKING: from pydantic import BaseModel from typing_extensions import Self diff --git a/src/easyconfig/config_objs/object_config.py b/src/easyconfig/config_objs/object_config.py index 8411e32..12dffb7 100644 --- a/src/easyconfig/config_objs/object_config.py +++ b/src/easyconfig/config_objs/object_config.py @@ -8,6 +8,7 @@ from easyconfig.config_objs import ConfigObjSubscription, SubscriptionParent from easyconfig.errors import DuplicateSubscriptionError, FunctionCallNotAllowedError + if TYPE_CHECKING: from pydantic.fields import FieldInfo diff --git a/src/easyconfig/config_objs/subscription.py b/src/easyconfig/config_objs/subscription.py index d666736..a4c925b 100644 --- a/src/easyconfig/config_objs/subscription.py +++ b/src/easyconfig/config_objs/subscription.py @@ -2,6 +2,7 @@ from easyconfig.errors import SubscriptionAlreadyCanceledError + if TYPE_CHECKING: import easyconfig diff --git a/src/easyconfig/create_app_config.py b/src/easyconfig/create_app_config.py index f54b999..b820455 100644 --- a/src/easyconfig/create_app_config.py +++ b/src/easyconfig/create_app_config.py @@ -7,6 +7,7 @@ from easyconfig.config_objs.app_config import AppConfig, yaml_rt from easyconfig.errors import ExtraKwArgsNotAllowedError + TYPE_WRAPPED = TypeVar('TYPE_WRAPPED', bound=BaseModel) TYPE_DEFAULTS = Union[BaseModel, Dict[str, Any]] @@ -37,7 +38,7 @@ def check_field_args(model: AppConfig, allowed: FrozenSet[str]): def get_file_values( - model: TYPE_WRAPPED, file_values: Union[MISSING_TYPE, None, TYPE_DEFAULTS, Callable[[], TYPE_DEFAULTS]] = MISSING + model: BaseModel, file_values: Union[MISSING_TYPE, None, TYPE_DEFAULTS, Callable[[], TYPE_DEFAULTS]] = MISSING ) -> Optional[BaseModel]: # Implicit default if file_values is MISSING: diff --git a/src/easyconfig/errors/handler.py b/src/easyconfig/errors/handler.py index 0bcbb5b..5dea4d6 100644 --- a/src/easyconfig/errors/handler.py +++ b/src/easyconfig/errors/handler.py @@ -9,7 +9,7 @@ def default_exception_handler(e: Exception): def set_exception_handler(handler: Callable[[Exception], Any]): - global HANDLER + global HANDLER # noqa: PLW0603 HANDLER = handler diff --git a/src/easyconfig/expansion/expand.py b/src/easyconfig/expansion/expand.py index 99a5afe..8e3e588 100644 --- a/src/easyconfig/expansion/expand.py +++ b/src/easyconfig/expansion/expand.py @@ -6,6 +6,7 @@ from .load_var import read_env_var from .location import ExpansionLocation + RE_REPLACE = re.compile(r''' (?.*?[^$])? diff --git a/src/easyconfig/expansion/load_file.py b/src/easyconfig/expansion/load_file.py index c394546..a18529a 100644 --- a/src/easyconfig/expansion/load_file.py +++ b/src/easyconfig/expansion/load_file.py @@ -6,6 +6,7 @@ from .location import ExpansionLocation, log + # https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file RE_WIN_PATH = re.compile( r''' diff --git a/src/easyconfig/expansion/location.py b/src/easyconfig/expansion/location.py index d12498f..b3148e0 100644 --- a/src/easyconfig/expansion/location.py +++ b/src/easyconfig/expansion/location.py @@ -5,6 +5,7 @@ from easyconfig.errors.errors import CyclicEnvironmentVariableReferenceError + log = logging.getLogger('easyconfig.expansion') diff --git a/src/easyconfig/models/app.py b/src/easyconfig/models/app.py index 4c8a22b..026a623 100644 --- a/src/easyconfig/models/app.py +++ b/src/easyconfig/models/app.py @@ -4,6 +4,7 @@ from easyconfig.models.config import ConfigMixin + if TYPE_CHECKING: from pathlib import Path diff --git a/src/easyconfig/models/config.py b/src/easyconfig/models/config.py index 07b6886..9469b88 100644 --- a/src/easyconfig/models/config.py +++ b/src/easyconfig/models/config.py @@ -2,6 +2,7 @@ from easyconfig.errors import FunctionCallNotAllowedError + if TYPE_CHECKING: import easyconfig import easyconfig.config_objs diff --git a/src/easyconfig/yaml/align.py b/src/easyconfig/yaml/align.py index d7c9be9..fe81f78 100644 --- a/src/easyconfig/yaml/align.py +++ b/src/easyconfig/yaml/align.py @@ -55,7 +55,7 @@ def remove_none(obj: Union[dict]): remove_none(value) if not value: rem.append(index) - else: + else: # noqa: PLR5501 if value is None: rem.append(index) diff --git a/src/easyconfig/yaml/from_model.py b/src/easyconfig/yaml/from_model.py index 5cf9a5a..da6b4ff 100644 --- a/src/easyconfig/yaml/from_model.py +++ b/src/easyconfig/yaml/from_model.py @@ -7,8 +7,6 @@ from easyconfig.__const__ import ARG_NAME_IN_FILE, MISSING from easyconfig.yaml import CommentedMap, CommentedSeq -NoneType = type(None) - def _get_yaml_value(obj, parent_model: BaseModel, *, skip_none=True, obj_name: str | None = None): if obj is None: diff --git a/src/easyconfig/yaml/yaml.py b/src/easyconfig/yaml/yaml.py index 19e66b6..72a705a 100644 --- a/src/easyconfig/yaml/yaml.py +++ b/src/easyconfig/yaml/yaml.py @@ -1,5 +1,6 @@ import ruamel.yaml # type: ignore + yaml_rt = ruamel.yaml.YAML(typ='rt') yaml_safe = ruamel.yaml.YAML(typ='safe') diff --git a/tests/helper/my_path.py b/tests/helper/my_path.py index daeb3bf..89f3baf 100644 --- a/tests/helper/my_path.py +++ b/tests/helper/my_path.py @@ -2,6 +2,7 @@ from pathlib import Path as _Path from typing import Optional + _path_type = type(_Path()) @@ -39,7 +40,7 @@ def is_file(self) -> bool: def get_value(self) -> str: return self.contents.getvalue() - def open(self, *args, mode='r', **kwargs) -> TextIOWrapper: # noqa: A003 + def open(self, *args, mode='r', **kwargs) -> TextIOWrapper: if 'w' in mode and 'a' not in mode: self._create_buffer() return self.contents diff --git a/tests/test_implementation/test_load_file.py b/tests/test_implementation/test_load_file.py index 3ce0c08..7d924a9 100644 --- a/tests/test_implementation/test_load_file.py +++ b/tests/test_implementation/test_load_file.py @@ -1,7 +1,6 @@ import pytest -from pydantic import BaseModel as PydanticBaseModel - from helper import Path +from pydantic import BaseModel as PydanticBaseModel from easyconfig import AppBaseModel, BaseModel from easyconfig.config_objs import AppConfig diff --git a/tests/yaml/test_model_to_yaml.py b/tests/yaml/test_model_to_yaml.py index 308e22a..db63265 100644 --- a/tests/yaml/test_model_to_yaml.py +++ b/tests/yaml/test_model_to_yaml.py @@ -1,10 +1,8 @@ -# ruff: noqa: ISC002 from datetime import datetime from enum import Enum from typing import List, Optional from pydantic import AnyHttpUrl, BaseModel, ByteSize, Field - from tests.helper import dump_yaml from easyconfig.__const__ import ARG_NAME_IN_FILE