Skip to content

Commit

Permalink
chore: Switch to ruff (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam authored Dec 17, 2023
1 parent f1b098d commit f678959
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 52 deletions.
29 changes: 4 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,10 @@ repos:
alias: ec
args:
- -disable-indent-size
- repo: https://github.com/asottile/pyupgrade
rev: "v3.15.0"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.1.8"
hooks:
- id: pyupgrade
args:
- --py310-plus
- repo: https://github.com/pycqa/autoflake
rev: "v2.2.1"
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --ignore-init-module-imports
- repo: https://github.com/pycqa/isort
rev: "5.13.2"
hooks:
- id: isort
- id: ruff
- repo: https://github.com/psf/black
rev: "23.12.0"
hooks:
Expand All @@ -43,14 +29,7 @@ repos:
rev: "1.16.0"
hooks:
- id: blacken-docs
additional_dependencies: ["black==22.8.0"]
- repo: https://github.com/pycqa/flake8
rev: "6.1.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==22.9.11
- flake8-comprehensions==3.10.0
additional_dependencies: ["black==23.12.0"]
- repo: https://github.com/sirosen/check-jsonschema
rev: "0.27.3"
hooks:
Expand Down
70 changes: 70 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,79 @@
requires = ["setuptools>=69.0.0", "wheel"]
build-backend = "setuptools.build_meta"


[tool.black]
target-version = ["py310"]


[tool.pytest.ini_options]
testpaths = ["tests", "src"]
addopts = "--doctest-modules"


[tool.ruff]
fix = true
target-version = "py310"
extend-select = [
# bugbear
"B",
# comprehensions
"C4",
# mccabe
"C90",
# bandit
"S",
# blind exception
# Bare excepts are caught without this, but this also catches `except Exception: ...`.
"BLE",
# builtins
"A",
# Enforce valid noqa comments.
"RUF100",
# isort
"I",
# pycodestyle
"W",
# pyupgrade
"UP",
# debugger
"T10",
# print
"T20",
# quotes
"Q",
# simplify
"SIM",
# tidy imports
# We use this to only outlaw relative _parent_ imports, other relative imports are OK.
"TID",
]
extend-ignore = [
# There's no reason to outlaw asserts.
# https://stackoverflow.com/a/68429294/1220706
"S101",
# Ignore line-length. This is enforced by black, but all cases cannot be handled.
# Ideally we'd only suppress this in generated files.
"E501",
# Allow function calls in argument defaults.
"B008",
]

[tool.ruff.isort]
force-single-line = true
known-first-party = ["immoney", "tests"]

[tool.ruff.mccabe]
max-complexity = 10

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "parents"

[tool.ruff.extend-per-file-ignores]
"tests/*" = [
# Cryptographically safe usage of PRNGs is not relevant in tests.
"S311",
# "Yoda conditions are discouraged", this is dangerous to apply in tests, as it
# destroys some cases that are designed to tests bi-directional equality.
"SIM300",
]
15 changes: 0 additions & 15 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ test =
coverage
hypothesis

[flake8]
exclude = appveyor,.idea,.git,.venv,.tox,__pycache__,*.egg-info,build
max-complexity = 8
max-line-length = 88
# B008: It's ok to instantiate instances as defaults.
# E203: Black does the right thing, flake8 doesn't.
# B019: This is the point of caching ...
# B024: Abstract base classes should not be expected to introduce abstract methods.
extend-ignore = E203 B008 B019 B024

[isort]
profile = black
src_paths = src, tests
force_single_line = True

[mypy]
python_version = 3.10
show_error_codes = True
Expand Down
14 changes: 7 additions & 7 deletions src/immoney/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def __truediv__(self, other: Fraction) -> SubunitFraction[C_co]:
...

def __truediv__(self, other: object) -> SubunitFraction[C_co]:
if not isinstance(other, (int, Fraction)):
if not isinstance(other, int | Fraction):
return NotImplemented
if other == 0:
raise DivisionByZero
Expand All @@ -413,7 +413,7 @@ def __rtruediv__(self, other: Fraction) -> SubunitFraction[C_co]:
...

def __rtruediv__(self, other: object) -> SubunitFraction[C_co]:
if not isinstance(other, (int, Fraction)):
if not isinstance(other, int | Fraction):
return NotImplemented
if self.subunits == 0:
raise DivisionByZero
Expand Down Expand Up @@ -576,7 +576,7 @@ def __mul__(self, other: Fraction) -> Self:
...

def __mul__(self, other: object) -> Self:
if isinstance(other, (int, Fraction)):
if isinstance(other, int | Fraction):
return SubunitFraction(self.value * other, self.currency)
return NotImplemented

Expand All @@ -592,7 +592,7 @@ def __truediv__(self, other: Fraction) -> Self:
...

def __truediv__(self, other: object) -> Self:
if isinstance(other, (int, Fraction)):
if isinstance(other, int | Fraction):
return SubunitFraction(self.value / other, self.currency)
return NotImplemented

Expand All @@ -605,7 +605,7 @@ def __rtruediv__(self, other: Fraction) -> Self:
...

def __rtruediv__(self, other: object) -> Self:
if isinstance(other, (int, Fraction)):
if isinstance(other, int | Fraction):
return SubunitFraction(other / self.value, self.currency)
return NotImplemented

Expand Down Expand Up @@ -884,7 +884,7 @@ def __truediv__(self, other: Fraction) -> SubunitFraction[C_co]:
...

def __truediv__(self, other: object) -> SubunitFraction[C_co]:
if not isinstance(other, (int, Fraction)):
if not isinstance(other, int | Fraction):
return NotImplemented
if other == 0:
raise DivisionByZero
Expand All @@ -899,7 +899,7 @@ def __rtruediv__(self, other: Fraction) -> SubunitFraction[C_co]:
...

def __rtruediv__(self, other: object) -> SubunitFraction[C_co]:
if not isinstance(other, (int, Fraction)):
if not isinstance(other, int | Fraction):
return NotImplemented
if other == 0:
return SubunitFraction(0, self.currency)
Expand Down
4 changes: 3 additions & 1 deletion src/immoney/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class InstanceCache(type):

_normalize: Callable[..., tuple[object, ...]]

@lru_cache
# lru_cache has a default bound, so while this does consume memory, it's a trivial
# amount, and worth it.
@lru_cache # noqa: B019
def __instantiate(cls, *args: object) -> object:
return super().__call__(*args)

Expand Down
8 changes: 4 additions & 4 deletions tests/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def truediv(
b: Numeric,
/,
) -> Fraction | SubunitFraction[Currency]:
if isinstance(a, (int, Fraction)):
if isinstance(a, int | Fraction):
return Fraction(a, b)
elif isinstance(a, (Money, Overdraft, SubunitFraction)):
elif isinstance(a, Money | Overdraft | SubunitFraction):
return a / b
raise NotImplementedError

Expand All @@ -80,9 +80,9 @@ def rtruediv(
b: Numeric,
/,
) -> Fraction | SubunitFraction[Currency]:
if isinstance(a, (int, Fraction)):
if isinstance(a, int | Fraction):
return Fraction(b, a)
elif isinstance(a, (Money, Overdraft, SubunitFraction)):
elif isinstance(a, Money | Overdraft | SubunitFraction):
return b / a
raise NotImplementedError

Expand Down

0 comments on commit f678959

Please sign in to comment.