From 45cdc870dc8c9c3e538f81c9f1763fa662fa37ae Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Mon, 15 May 2023 17:36:26 +0330 Subject: [PATCH] Add ruff --- .gitignore | 1 + Makefile | 6 ++---- docs/index.md | 15 ++++++++----- pydantic_settings/sources.py | 2 +- pyproject.toml | 24 +++++++-------------- requirements/linting.in | 6 +----- requirements/linting.txt | 32 ++++----------------------- requirements/testing.in | 1 + requirements/testing.txt | 42 ++++++++++++++++++++---------------- tests/test_docs.py | 14 +----------- tests/test_settings.py | 2 ++ 11 files changed, 55 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 872a653..2079909 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ _build/ /sandbox/ /.ghtopdep_cache/ /worktrees/ +/.ruff_cache/ diff --git a/Makefile b/Makefile index 5fc1c7a..40e4be7 100644 --- a/Makefile +++ b/Makefile @@ -9,14 +9,12 @@ install: .PHONY: format format: - pyupgrade --py37-plus --exit-zero-even-if-changed `find $(sources) -name "*.py" -type f` - isort $(sources) black $(sources) + ruff --fix $(sources) .PHONY: lint lint: - flake8 $(sources) - isort $(sources) --check-only --df + ruff $(sources) black $(sources) --check --diff .PHONY: mypy diff --git a/docs/index.md b/docs/index.md index 29417bc..bf29c25 100644 --- a/docs/index.md +++ b/docs/index.md @@ -13,19 +13,19 @@ For example: ```py import os -from pprint import pprint from typing import Any, Callable, Set from pydantic import ( AliasChoices, + AmqpDsn, BaseModel, ConfigDict, + Field, ImportString, - RedisDsn, PostgresDsn, - AmqpDsn, - Field, + RedisDsn, ) + from pydantic_settings import BaseSettings @@ -62,7 +62,7 @@ class Settings(BaseSettings): os.environ['my_auth_key'] = 'xxx' os.environ['my_api_key'] = 'xxx' -pprint(Settings().model_dump()) +print(Settings().model_dump()) """ { 'auth_key': '', @@ -225,6 +225,7 @@ import os from typing import Any, List, Tuple, Type from pydantic.fields import FieldInfo + from pydantic_settings import BaseSettings, EnvSettingsSource, PydanticBaseSettingsSource @@ -431,7 +432,9 @@ The order of the returned callables decides the priority of inputs; first item i ```py from typing import Tuple, Type + from pydantic import PostgresDsn + from pydantic_settings import BaseSettings, PydanticBaseSettingsSource @@ -468,6 +471,7 @@ from typing import Any, Dict, Tuple, Type from pydantic import ConfigDict from pydantic.fields import FieldInfo + from pydantic_settings import BaseSettings, PydanticBaseSettingsSource @@ -535,6 +539,7 @@ You might also want to disable a source: from typing import Tuple, Type from pydantic import ValidationError + from pydantic_settings import BaseSettings, PydanticBaseSettingsSource diff --git a/pydantic_settings/sources.py b/pydantic_settings/sources.py index a280b90..0025836 100644 --- a/pydantic_settings/sources.py +++ b/pydantic_settings/sources.py @@ -470,7 +470,7 @@ def _read_env_files(self, case_sensitive: bool) -> Mapping[str, str | None]: def __call__(self) -> dict[str, Any]: data: dict[str, Any] = super().__call__() - data_lower_keys: List[str] = [] + data_lower_keys: list[str] = [] if not self.settings_cls.model_config.get('case_sensitive', False): data_lower_keys = [x.lower() for x in data.keys()] diff --git a/pyproject.toml b/pyproject.toml index 22c95ff..6b3d474 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,13 +59,6 @@ filterwarnings = [ 'ignore:This is a placeholder until pydantic-settings.*:UserWarning', ] -[tool.flake8] -max_line_length = 120 -max_complexity = 14 -inline_quotes = 'single' -multiline_quotes = 'double' -ignore = ['E203', 'W503'] - [tool.coverage.run] source = ['pydantic_settings'] branch = true @@ -86,21 +79,20 @@ source = [ 'pydantic_settings/', ] +[tool.ruff] +line-length = 120 +extend-select = ['Q', 'RUF100', 'C90', 'UP', 'I'] +flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'} +mccabe = { max-complexity = 14 } +isort = { known-first-party = ['pydantic_settings', 'tests'] } +target-version = "py37" + [tool.black] color = true line-length = 120 target-version = ['py310'] skip-string-normalization = true -[tool.isort] -line_length = 120 -known_first_party = 'pydantic_settings' -known_third_party = 'pydantic' -multi_line_output = 3 -include_trailing_comma = true -force_grid_wrap = 0 -combine_as_imports = true - [tool.mypy] python_version = '3.10' show_error_codes = true diff --git a/requirements/linting.in b/requirements/linting.in index 541205e..4701374 100644 --- a/requirements/linting.in +++ b/requirements/linting.in @@ -1,8 +1,4 @@ black -flake8 -flake8-quotes -flake8-pyproject -isort -pyupgrade +ruff mypy pre-commit diff --git a/requirements/linting.txt b/requirements/linting.txt index e4117f6..ae17fb2 100644 --- a/requirements/linting.txt +++ b/requirements/linting.txt @@ -1,6 +1,6 @@ # -# This file is autogenerated by pip-compile with python 3.10 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.11 +# by the following command: # # pip-compile --output-file=requirements/linting.txt requirements/linting.in # @@ -14,21 +14,8 @@ distlib==0.3.6 # via virtualenv filelock==3.12.0 # via virtualenv -flake8==6.0.0 - # via - # -r requirements/linting.in - # flake8-pyproject - # flake8-quotes -flake8-pyproject==1.2.3 - # via -r requirements/linting.in -flake8-quotes==3.3.2 - # via -r requirements/linting.in identify==2.5.24 # via pre-commit -isort==5.12.0 - # via -r requirements/linting.in -mccabe==0.7.0 - # via flake8 mypy==1.3.0 # via -r requirements/linting.in mypy-extensions==1.0.0 @@ -47,21 +34,10 @@ platformdirs==3.5.1 # virtualenv pre-commit==3.3.1 # via -r requirements/linting.in -pycodestyle==2.10.0 - # via flake8 -pyflakes==3.0.1 - # via flake8 -pyupgrade==3.4.0 - # via -r requirements/linting.in pyyaml==6.0 # via pre-commit -tokenize-rt==5.0.0 - # via pyupgrade -tomli==2.0.1 - # via - # black - # flake8-pyproject - # mypy +ruff==0.0.265 + # via -r requirements/linting.in typing-extensions==4.5.0 # via mypy virtualenv==20.23.0 diff --git a/requirements/testing.in b/requirements/testing.in index 26a9004..262b641 100644 --- a/requirements/testing.in +++ b/requirements/testing.in @@ -3,3 +3,4 @@ pytest pytest-mock pytest-sugar pytest-examples +pytest-pretty diff --git a/requirements/testing.txt b/requirements/testing.txt index 6ce69ef..d23d8aa 100644 --- a/requirements/testing.txt +++ b/requirements/testing.txt @@ -1,48 +1,54 @@ # -# This file is autogenerated by pip-compile with python 3.7 -# To update, run: +# This file is autogenerated by pip-compile with Python 3.11 +# by the following command: # # pip-compile --output-file=requirements/testing.txt requirements/testing.in # +black==23.3.0 + # via pytest-examples +click==8.1.3 + # via black coverage[toml]==7.2.5 # via -r requirements/testing.in -exceptiongroup==1.1.1 - # via pytest -importlib-metadata==6.6.0 - # via - # pluggy - # pytest iniconfig==2.0.0 # via pytest +markdown-it-py==2.2.0 + # via rich +mdurl==0.1.2 + # via markdown-it-py +mypy-extensions==1.0.0 + # via black packaging==23.1 # via + # black # pytest # pytest-sugar +pathspec==0.11.1 + # via black +platformdirs==3.5.1 + # via black pluggy==1.0.0 # via pytest +pygments==2.15.1 + # via rich pytest==7.3.1 # via # -r requirements/testing.in # pytest-examples # pytest-mock + # pytest-pretty # pytest-sugar pytest-examples==0.0.9 # via -r requirements/testing.in pytest-mock==3.10.0 # via -r requirements/testing.in +pytest-pretty==1.2.0 + # via -r requirements/testing.in pytest-sugar==0.9.7 # via -r requirements/testing.in +rich==13.3.5 + # via pytest-pretty ruff==0.0.265 # via pytest-examples termcolor==2.3.0 # via pytest-sugar -tomli==2.0.1 - # via - # coverage - # pytest -typed-ast==1.5.4 - # via black -typing-extensions==4.5.0 - # via importlib-metadata -zipp==3.15.0 - # via importlib-metadata diff --git a/tests/test_docs.py b/tests/test_docs.py index a017f03..a5cd327 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -8,7 +8,6 @@ import pytest from pytest_examples import CodeExample, EvalExample, find_examples -INDEX_MAIN = None DOCS_ROOT = Path(__file__).parent.parent / 'docs' @@ -50,17 +49,6 @@ def print_callback(print_statement: str) -> str: @pytest.mark.skipif(bool(skip_reason), reason=skip_reason or 'not skipping') @pytest.mark.parametrize('example', find_examples(str(DOCS_ROOT), skip=sys.platform == 'win32'), ids=str) def test_docs_examples(example: CodeExample, eval_example: EvalExample, tmp_path: Path, mocker): # noqa: C901 - global INDEX_MAIN - if example.path.name == 'index.md': - if INDEX_MAIN is None: - INDEX_MAIN = example.source - else: - (tmp_path / 'index_main.py').write_text(INDEX_MAIN) - sys.path.append(str(tmp_path)) - - if example.path.name == 'devtools.md': - pytest.skip('tested below') - eval_example.print_callback = print_callback prefix_settings = example.prefix_settings() @@ -82,7 +70,7 @@ def test_docs_examples(example: CodeExample, eval_example: EvalExample, tmp_path if group_name: eval_example.set_config(ruff_ignore=['F821']) - eval_example.set_config(line_length=120) + # eval_example.set_config(line_length=120) if lint_settings != 'skip': if eval_example.update_examples: eval_example.format(example) diff --git a/tests/test_settings.py b/tests/test_settings.py index af32a24..b4ea8fe 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -17,6 +17,8 @@ HttpUrl, SecretStr, ValidationError, +) +from pydantic import ( dataclasses as pydantic_dataclasses, ) from pydantic.fields import FieldInfo