Skip to content

Commit

Permalink
Configure ruff beyond the basics
Browse files Browse the repository at this point in the history
Especially isort, shame it's not part of format but...
  • Loading branch information
masklinn committed Feb 20, 2024
1 parent 7da56fd commit 8d4e624
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 47 deletions.
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions src/ua_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions src/ua_parser/_lazy.pyi
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
5 changes: 3 additions & 2 deletions src/ua_parser/_matchers.pyi
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
3 changes: 2 additions & 1 deletion src/ua_parser/_regexes.pyi
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
12 changes: 6 additions & 6 deletions src/ua_parser/bench.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/ua_parser/caching.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/ua_parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/ua_parser/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
8 changes: 4 additions & 4 deletions src/ua_parser/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json
import os
from typing import (
TYPE_CHECKING,
Callable,
List,
Literal,
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/ua_parser/re2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
2 changes: 1 addition & 1 deletion src/ua_parser/threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ua_parser/user_agent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions tests/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
15 changes: 9 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions tests/test_parsers_basics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import io

from ua_parser import (
BasicParser,
PartialParseResult,
Domain,
PartialParseResult,
UserAgent,
load_yaml,
UserAgentMatcher,
load_yaml,
)


Expand Down
4 changes: 0 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,3 @@ deps =
mypy
types-PyYaml
commands = mypy {posargs:}

[flake8]
max_line_length = 88
filename = src/ua_parser/

0 comments on commit 8d4e624

Please sign in to comment.