From 8d4e624d36f150fec30959b3183cb80008a1e12d Mon Sep 17 00:00:00 2001 From: masklinn Date: Tue, 20 Feb 2024 20:34:00 +0100 Subject: [PATCH] Configure ruff beyond the basics Especially isort, shame it's not part of format but... --- pyproject.toml | 11 +++++++++++ src/ua_parser/__init__.py | 8 ++++---- src/ua_parser/_lazy.pyi | 5 +++-- src/ua_parser/_matchers.pyi | 5 +++-- src/ua_parser/_regexes.pyi | 3 ++- src/ua_parser/bench.py | 12 ++++++------ src/ua_parser/caching.py | 5 ++--- src/ua_parser/core.py | 2 +- src/ua_parser/lazy.py | 2 +- src/ua_parser/loaders.py | 8 ++++---- src/ua_parser/re2.py | 4 ++-- src/ua_parser/threaded.py | 2 +- src/ua_parser/user_agent_parser.py | 2 +- tests/test_caches.py | 14 +++++++------- tests/test_core.py | 15 +++++++++------ tests/test_parsers_basics.py | 5 +++-- tox.ini | 4 ---- 17 files changed, 60 insertions(+), 47 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0f9f61e9..47452ecb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,6 +43,17 @@ classifiers = [ "Programming Language :: Python :: Implementation :: PyPy" ] +[tool.ruff.lint] +select = ["F", "E", "W", "I", "RET", "RUF", "PT"] +ignore = [ + "RET505", # elif after return + "E501", # line too long, formatter should take care of the fixable ones +] + +[tool.ruff.lint.isort] +classes = ["LRU", "OS"] +combine-as-imports = true + [tool.mypy] python_version = "3.8" files = "src,tests" diff --git a/src/ua_parser/__init__.py b/src/ua_parser/__init__.py index 5fee93f0..dcc06b51 100644 --- a/src/ua_parser/__init__.py +++ b/src/ua_parser/__init__.py @@ -48,6 +48,8 @@ import contextlib from typing import Callable, Optional +from .basic import Parser as BasicParser +from .caching import CachingParser, Clearing, Locking, LRU from .core import ( DefaultedParseResult, Device, @@ -56,15 +58,13 @@ Matchers, OS, OSMatcher, - ParseResult, Parser, + ParseResult, PartialParseResult, UserAgent, UserAgentMatcher, ) -from .basic import Parser as BasicParser -from .caching import CachingParser, Clearing, LRU, Locking -from .loaders import load_builtins, load_lazy_builtins, load_data, load_yaml +from .loaders import load_builtins, load_data, load_lazy_builtins, load_yaml Re2Parser: Optional[Callable[[Matchers], Parser]] = None with contextlib.suppress(ImportError): diff --git a/src/ua_parser/_lazy.pyi b/src/ua_parser/_lazy.pyi index aa674789..741db1af 100644 --- a/src/ua_parser/_lazy.pyi +++ b/src/ua_parser/_lazy.pyi @@ -1,7 +1,8 @@ __all__ = ["MATCHERS"] -from typing import Tuple, List -from .lazy import UserAgentMatcher, OSMatcher, DeviceMatcher +from typing import List, Tuple + +from .lazy import DeviceMatcher, OSMatcher, UserAgentMatcher MATCHERS: Tuple[ List[UserAgentMatcher], diff --git a/src/ua_parser/_matchers.pyi b/src/ua_parser/_matchers.pyi index da0b023c..7c4388ad 100644 --- a/src/ua_parser/_matchers.pyi +++ b/src/ua_parser/_matchers.pyi @@ -1,7 +1,8 @@ __all__ = ["MATCHERS"] -from typing import Tuple, List -from .core import UserAgentMatcher, OSMatcher, DeviceMatcher +from typing import List, Tuple + +from .core import DeviceMatcher, OSMatcher, UserAgentMatcher MATCHERS: Tuple[ List[UserAgentMatcher], diff --git a/src/ua_parser/_regexes.pyi b/src/ua_parser/_regexes.pyi index 9050f8b0..10bc2ef4 100644 --- a/src/ua_parser/_regexes.pyi +++ b/src/ua_parser/_regexes.pyi @@ -1,5 +1,6 @@ from typing import List -from .user_agent_parser import UserAgentParser, OSParser, DeviceParser + +from .user_agent_parser import DeviceParser, OSParser, UserAgentParser USER_AGENT_PARSERS: List[UserAgentParser] OS_PARSERS: List[OSParser] diff --git a/src/ua_parser/bench.py b/src/ua_parser/bench.py index 2896004a..b84bba3b 100644 --- a/src/ua_parser/bench.py +++ b/src/ua_parser/bench.py @@ -1,25 +1,25 @@ import argparse import csv -import itertools import io -import time +import itertools import sys +import time from typing import Any, Callable, Iterable, List, Optional from . import ( - load_builtins, - load_yaml, BasicParser, CachingParser, Clearing, - LRU, Locking, + LRU, Matchers, Parser, + load_builtins, + load_yaml, ) from .caching import Cache -from .user_agent_parser import Parse from .re2 import Parser as Re2Parser +from .user_agent_parser import Parse CACHEABLE = { "basic": True, diff --git a/src/ua_parser/caching.py b/src/ua_parser/caching.py index 9128cb26..358b5744 100644 --- a/src/ua_parser/caching.py +++ b/src/ua_parser/caching.py @@ -1,10 +1,9 @@ import abc -from collections import OrderedDict import threading +from collections import OrderedDict from typing import Dict, Optional -from .core import Parser, Domain, PartialParseResult - +from .core import Domain, Parser, PartialParseResult __all__ = [ "CachingParser", diff --git a/src/ua_parser/core.py b/src/ua_parser/core.py index 2cd33e45..54837e44 100644 --- a/src/ua_parser/core.py +++ b/src/ua_parser/core.py @@ -2,7 +2,7 @@ import re from dataclasses import dataclass from enum import Flag, auto -from typing import Generic, Literal, Optional, Tuple, List, TypeVar, Match, Pattern +from typing import Generic, List, Literal, Match, Optional, Pattern, Tuple, TypeVar __all__ = [ "DefaultedParseResult", diff --git a/src/ua_parser/lazy.py b/src/ua_parser/lazy.py index fb247353..d9e0219f 100644 --- a/src/ua_parser/lazy.py +++ b/src/ua_parser/lazy.py @@ -4,7 +4,7 @@ from functools import cached_property from typing import Literal, Optional, Pattern -from .core import Matcher, UserAgent, OS, Device, _replacer, _get +from .core import Device, Matcher, OS, UserAgent, _get, _replacer class UserAgentMatcher(Matcher[UserAgent]): diff --git a/src/ua_parser/loaders.py b/src/ua_parser/loaders.py index 9a382206..66a294c2 100644 --- a/src/ua_parser/loaders.py +++ b/src/ua_parser/loaders.py @@ -15,6 +15,7 @@ import json import os from typing import ( + TYPE_CHECKING, Callable, List, Literal, @@ -24,21 +25,20 @@ Type, TypedDict, Union, - TYPE_CHECKING, cast, ) from . import lazy -from .core import Matchers, UserAgentMatcher, OSMatcher, DeviceMatcher +from .core import DeviceMatcher, Matchers, OSMatcher, UserAgentMatcher if TYPE_CHECKING: PathOrFile = Union[str, os.PathLike[str], io.IOBase] SafeLoader: Optional[Type[object]] try: - from yaml import load, CSafeLoader as SafeLoader + from yaml import CSafeLoader as SafeLoader, load except ImportError: try: - from yaml import load, SafeLoader + from yaml import SafeLoader, load except ImportError: load = SafeLoader = None # type: ignore diff --git a/src/ua_parser/re2.py b/src/ua_parser/re2.py index 5ff67243..559879b2 100644 --- a/src/ua_parser/re2.py +++ b/src/ua_parser/re2.py @@ -6,13 +6,13 @@ import re2 # type: ignore from .core import ( - Parser as AbstractParser, - PartialParseResult, Device, Domain, Matcher, Matchers, OS, + Parser as AbstractParser, + PartialParseResult, UserAgent, ) diff --git a/src/ua_parser/threaded.py b/src/ua_parser/threaded.py index 6be41589..15b23907 100644 --- a/src/ua_parser/threaded.py +++ b/src/ua_parser/threaded.py @@ -6,13 +6,13 @@ from typing import Iterable from . import ( - load_builtins, BasicParser, CachingParser, Clearing, Locking, LRU, Parser, + load_builtins, ) from .re2 import Parser as Re2Parser diff --git a/src/ua_parser/user_agent_parser.py b/src/ua_parser/user_agent_parser.py index 25a8a8c1..5cb1c744 100644 --- a/src/ua_parser/user_agent_parser.py +++ b/src/ua_parser/user_agent_parser.py @@ -521,4 +521,4 @@ def GetFilters( del SafeLoader else: # Just load our pre-compiled versions - from ._regexes import USER_AGENT_PARSERS, DEVICE_PARSERS, OS_PARSERS + from ._regexes import DEVICE_PARSERS, OS_PARSERS, USER_AGENT_PARSERS diff --git a/tests/test_caches.py b/tests/test_caches.py index 3fb078c9..5969e467 100644 --- a/tests/test_caches.py +++ b/tests/test_caches.py @@ -2,17 +2,17 @@ from ua_parser import ( BasicParser, - PartialParseResult, - Domain, - UserAgent, - OS, - Device, CachingParser, Clearing, + Device, + DeviceMatcher, + Domain, LRU, - UserAgentMatcher, + OS, OSMatcher, - DeviceMatcher, + PartialParseResult, + UserAgent, + UserAgentMatcher, ) diff --git a/tests/test_core.py b/tests/test_core.py index f92c1a21..3a73fafa 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -11,27 +11,30 @@ import pytest # type: ignore if platform.python_implementation() == "PyPy": - from yaml import load, SafeLoader + from yaml import SafeLoader, load else: try: - from yaml import load, CSafeLoader as SafeLoader # type: ignore + from yaml import ( # type: ignore + CSafeLoader as SafeLoader, + load, + ) except ImportError: logging.getLogger(__name__).warning( "PyYaml C extension not available to run tests, this will result " "in dramatic tests slowdown." ) - from yaml import load, SafeLoader + from yaml import SafeLoader, load from ua_parser import ( BasicParser, - UserAgent, - OS, Device, + OS, ParseResult, + UserAgent, UserAgentMatcher, + caching, load_builtins, load_lazy_builtins, - caching, ) CORE_DIR = (pathlib.Path(__name__).parent.parent / "uap-core").resolve() diff --git a/tests/test_parsers_basics.py b/tests/test_parsers_basics.py index af38c7e7..92527453 100644 --- a/tests/test_parsers_basics.py +++ b/tests/test_parsers_basics.py @@ -1,11 +1,12 @@ import io + from ua_parser import ( BasicParser, - PartialParseResult, Domain, + PartialParseResult, UserAgent, - load_yaml, UserAgentMatcher, + load_yaml, ) diff --git a/tox.ini b/tox.ini index 3245bc69..4a2cb160 100644 --- a/tox.ini +++ b/tox.ini @@ -44,7 +44,3 @@ deps = mypy types-PyYaml commands = mypy {posargs:} - -[flake8] -max_line_length = 88 -filename = src/ua_parser/