Skip to content

Commit d7025b5

Browse files
authored
Introduce linting with ruff (#259)
1 parent 5e3d364 commit d7025b5

8 files changed

Lines changed: 74 additions & 40 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@ repos:
2323
- id: pyupgrade
2424
args:
2525
- --py37-plus
26-
- repo: https://github.com/pycqa/autoflake
27-
rev: "v1.7.7"
28-
hooks:
29-
- id: autoflake
30-
args:
31-
- --in-place
32-
- --remove-all-unused-imports
33-
- --ignore-init-module-imports
3426
- repo: https://github.com/pycqa/isort
3527
rev: "5.10.1"
3628
hooks:
3729
- id: isort
30+
- repo: https://github.com/charliermarsh/ruff-pre-commit
31+
rev: "v0.0.178"
32+
hooks:
33+
- id: ruff
3834
- repo: https://github.com/psf/black
3935
rev: "22.10.0"
4036
hooks:
@@ -44,13 +40,6 @@ repos:
4440
hooks:
4541
- id: blacken-docs
4642
additional_dependencies: ["black==22.10.0"]
47-
- repo: https://github.com/pycqa/flake8
48-
rev: "5.0.4"
49-
hooks:
50-
- id: flake8
51-
additional_dependencies:
52-
- flake8-bugbear
53-
- flake8-comprehensions
5443
- repo: https://github.com/sirosen/check-jsonschema
5544
rev: "0.19.2"
5645
hooks:

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717

1818
def get_copyright_from_license() -> str:
19-
license = current_dir.parent / "LICENSE"
19+
license_path = current_dir.parent / "LICENSE"
2020
prefix = "Copyright (c) "
21-
for line in license.read_text().split("\n"):
21+
for line in license_path.read_text().split("\n"):
2222
if line.startswith(prefix):
2323
return line[len(prefix) :]
2424
raise RuntimeError("Couldn't parse copyright from LICENSE")
2525

2626

2727
# Project information
2828
project = "phantom-types"
29-
copyright = get_copyright_from_license()
29+
copyright = get_copyright_from_license() # noqa: A001
3030
author = "Anton Agestam"
3131
version = phantom.__version__
3232
release = version

pyproject.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,53 @@ markers = [
1212
"external: mark tests that require extra dependencies",
1313
"no_external: mark tests that will fail if run with extra dependencies",
1414
]
15+
16+
[tool.ruff]
17+
fix = true
18+
target-version = "py37"
19+
extend-select = [
20+
# bugbear
21+
"B",
22+
# comprehensions
23+
"C4",
24+
# mccabe
25+
"C90",
26+
# bandit
27+
"S",
28+
# blind exception
29+
# Bare excepts are caught without this, but this also catches `except Exception: ...`.
30+
"BLE",
31+
# builtins
32+
"A",
33+
# Enforce valid noqa comments.
34+
"RUF100",
35+
# pycodestyle
36+
"W",
37+
# pyupgrade
38+
"UP",
39+
# debugger
40+
"T10",
41+
# print
42+
"T20",
43+
# quotes
44+
"Q",
45+
# return
46+
# This gives 3 false positives, would be nice otherwise probably.
47+
# "RET",
48+
# simplify
49+
"SIM",
50+
# tidy imports
51+
# We use this to only outlaw relative parent imports.
52+
"TID",
53+
]
54+
extend-ignore = [
55+
# There's no reason to outlaw asserts.
56+
# https://stackoverflow.com/a/68429294/1220706
57+
"S101",
58+
]
59+
60+
[tool.ruff.mccabe]
61+
max-complexity = 8
62+
63+
[tool.ruff.flake8-tidy-imports]
64+
ban-relative-imports = "parents"

setup.cfg

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ test =
6060
pytest-mypy-plugins>=1.9.3
6161
coverage
6262

63-
[flake8]
64-
exclude = appveyor,.idea,.git,.venv,.tox,__pycache__,*.egg-info,build
65-
max-complexity = 8
66-
max-line-length = 88
67-
# B008: It's ok to instantiate instances as defaults.
68-
# E203: Black does the right thing, flake8 doesn't.
69-
# B024: Abstract base classes should not be expected to introduce abstract methods.
70-
extend-ignore = E203 B008 B024
71-
7263
[isort]
7364
profile = black
7465
src_paths = src, tests

src/phantom/ext/phonenumbers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _deconstruct_phone_number(
4242
try:
4343
parsed_number = phonenumbers.parse(phone_number, region=country_code)
4444
except phonenumbers.NumberParseException as e:
45-
raise InvalidPhoneNumber(e.error_type, e._msg)
45+
raise InvalidPhoneNumber(e.error_type, e._msg) from e
4646
if not phonenumbers.is_valid_number(parsed_number):
4747
raise InvalidPhoneNumber
4848
return parsed_number

src/phantom/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class Schema(TypedDict, total=False):
1010
title: str
1111
description: str
12-
type: Literal["array", "string", "float", "number"]
13-
format: str
12+
type: Literal["array", "string", "float", "number"] # noqa: A003
13+
format: str # noqa: A003
1414
examples: Sequence[object]
1515
minimum: Optional[float]
1616
maximum: Optional[float]

src/phantom/sized.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ class PhantomSized(
9191
schema generation.
9292
"""
9393

94-
def __init_subclass__(cls, len: Predicate[int], **kwargs: Any) -> None:
94+
def __init_subclass__(
95+
cls,
96+
len: Predicate[int], # noqa: A002
97+
**kwargs: Any,
98+
) -> None:
9599
super().__init_subclass__(
96100
predicate=boolean.both(
97101
is_not_known_mutable_instance,
@@ -129,10 +133,10 @@ class PhantomBound(
129133
__min__: int | None
130134
__max__: int | None
131135

132-
def __init_subclass__( # noqa
136+
def __init_subclass__(
133137
cls,
134-
min: int | None = None,
135-
max: int | None = None,
138+
min: int | None = None, # noqa: A002
139+
max: int | None = None, # noqa: A002
136140
abstract: bool = False,
137141
**kwargs: Any,
138142
) -> None:

tests/pydantic/test_datetime.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class HasTZAware(pydantic.BaseModel):
1919
class TestPydanticTZAware:
2020
@parametrize_aware_str
2121
def test_can_parse_tz_aware(self, value: str, expected: datetime.datetime):
22-
object = HasTZAware.parse_obj({"created_at": value})
23-
assert type(object.created_at) is datetime.datetime
24-
assert object.created_at == expected
22+
obj = HasTZAware.parse_obj({"created_at": value})
23+
assert type(obj.created_at) is datetime.datetime
24+
assert obj.created_at == expected
2525

2626
def test_tz_aware_rejects_naive_datetime(self):
2727
with pytest.raises(ValidationError):
@@ -35,9 +35,9 @@ class HasTZNaive(pydantic.BaseModel):
3535
class TestPydanticTZNaive:
3636
@parametrize_naive_str
3737
def test_can_parse_tz_naive(self, value: str, expected: datetime.datetime):
38-
object = HasTZNaive.parse_obj({"time_of_day": value})
39-
assert type(object.time_of_day) is datetime.datetime
40-
assert object.time_of_day == expected
38+
obj = HasTZNaive.parse_obj({"time_of_day": value})
39+
assert type(obj.time_of_day) is datetime.datetime
40+
assert obj.time_of_day == expected
4141

4242
def test_tz_naive_rejects_aware_datetime(self):
4343
with pytest.raises(ValidationError):

0 commit comments

Comments
 (0)