Skip to content

Commit

Permalink
style: use ruff instead of flake8, isort, pyupgrade, autoflake, etc... (
Browse files Browse the repository at this point in the history
#146)

* style: update pre-commit for ruff

* ci: add pydantic to mypy env

* style: apply ruff changes

* chore: add ruff cache to manifest-ignore
  • Loading branch information
tlambert03 authored Nov 23, 2022
1 parent 4e9fd21 commit fb09cfd
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 118 deletions.
45 changes: 12 additions & 33 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ci:
autoupdate_schedule: monthly
autofix_commit_msg: "style: [pre-commit.ci] auto fixes [...]"
autoupdate_commit_msg: "ci: [pre-commit.ci] autoupdate"
autofix_commit_msg: "style(pre-commit.ci): auto fixes [...]"
autoupdate_commit_msg: "ci(pre-commit.ci): autoupdate"

default_install_hook_types: [pre-commit, commit-msg]

Expand All @@ -21,48 +21,27 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.134
hooks:
- id: setup-cfg-fmt

- repo: https://github.com/PyCQA/autoflake
rev: v1.7.7
hooks:
- id: autoflake
args: ["--in-place", "--remove-all-unused-imports"]

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/asottile/pyupgrade
rev: v3.2.0
hooks:
- id: pyupgrade
args: [--py37-plus, --keep-runtime-typing]
- id: ruff
args: [--fix]

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.10.1
hooks:
- id: flake8
additional_dependencies:
- flake8-typing-imports
- flake8-type-checking
- git+https://github.com/tlambert03/flake8-pyprojecttoml.git@main
- flake8-docstrings
- flake8-bugbear
- id: validate-pyproject

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
rev: v0.991
hooks:
- id: mypy
exclude: tests
additional_dependencies:
- "types-attrs"
- types-attrs
- pydantic
78 changes: 34 additions & 44 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description = "Pure python callback/event system modeled after Qt Signals"
readme = "README.md"
requires-python = ">=3.7"
license = { text = "BSD 3-Clause License" }
authors = [{ email = "talley.lambert@gmail.com" }, { name = "Talley Lambert" }]
authors = [{ name = "Talley Lambert", email = "talley.lambert@gmail.com" }]
classifiers = [
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: BSD License",
Expand All @@ -42,19 +42,12 @@ dev = [
"black",
"cruft",
"dask",
"flake8-bugbear",
"flake8-docstrings",
"flake8-type-checking",
"flake8-typing-imports",
"flake8-pyprojecttoml",
"flake8",
"ruff",
"ipython",
"isort",
"mypy",
"numpy",
"pre-commit",
"pydantic",
"pydocstyle",
"PyQt5",
"pytest-cov",
"pytest-mypy-plugins",
Expand Down Expand Up @@ -110,41 +103,39 @@ zip-safe = false
# https://github.com/pypa/setuptools_scm/#pyprojecttoml-usage
[tool.setuptools_scm]

# https://pycqa.github.io/isort/docs/configuration/options.html
[tool.isort]
profile = "black"
src_paths = ["src/psygnal", "tests"]

# https://flake8.pycqa.org/en/latest/user/options.html
# https://gitlab.com/durko/flake8-pyprojecttoml
[tool.flake8]
exclude = "docs,.eggs,examples,_version.py"
max-line-length = 88
ignore = "E203"
min-python-version = "3.7.0"
docstring-convention = "all" # use numpy convention, while allowing D417
extend-ignore = """
E203 # whitespace before ':'
D107,D203,D212,D213,D402,D413,D415,D416 # numpy
D100 # missing docstring in public module
D105 # missing docstring in magic method
D401 # imperative mood
W503 # line break before binary operator
TC002,B010
"""
per-file-ignores = [
"tests/*:D",
"tests/test_psygnal.py:E704,E701,E302,D",
"benchmarks/**:D",
"setup.py:D",
# https://github.com/charliermarsh/ruff
[tool.ruff]
line-length = 88
target-version = "py38"
extend-select = [
"E", # style errors
"F", # flakes
"D", # pydocstyle
"I001", # isort
"U", # pyupgrade
# "N", # pep8-naming
# "S", # bandit
"C", # flake8-comprehensions
"B", # flake8-bugbear
"A001", # flake8-builtins
"RUF", # ruff-specific rules
"M001", # Unused noqa directive
]
extend-ignore = [
"D100", # Missing docstring in public module
"D107", # Missing docstring in __init__
"D203", # 1 blank line required before class docstring
"D212", # Multi-line docstring summary should start at the first line
"D213", # Multi-line docstring summary should start at the second line
"D413", # Missing blank line after last section
"D416", # Section name should end with a colon
]

[tool.ruff.per-file-ignores]
"tests/*.py" = ["D"]
"benchmarks/*.py" = ["D"]
"setup.py" = ["D"]

# http://www.pydocstyle.org/en/stable/usage.html
[tool.pydocstyle]
match_dir = "src/psygnal"
convention = "numpy"
add_select = "D402,D415,D417"
ignore = "D100,D213,D401,D413,D107"

# https://docs.pytest.org/en/6.2.x/customize.html
[tool.pytest.ini_options]
Expand Down Expand Up @@ -190,12 +181,11 @@ skip = ["tests"]
[tool.check-manifest]
ignore = [
".cruft.json",
".flake8",
".ruff_cache/**/*",
".github_changelog_generator",
".pre-commit-config.yaml",
"tests/**/*",
"typesafety/*",
"tox.ini",
".devcontainer/*",
".readthedocs.yaml",
"Makefile",
Expand Down
10 changes: 5 additions & 5 deletions src/psygnal/_evented_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def no_class_attributes() -> Iterator[None]: # pragma: no cover
def _return2(x: str, y: inspect.Signature) -> inspect.Signature:
return y

setattr(pydantic.main, "ClassAttribute", _return2)
pydantic.main.ClassAttribute = _return2 # type: ignore [attr-defined]
try:
yield
finally:
# undo our monkey patch
setattr(pydantic.main, "ClassAttribute", utils.ClassAttribute)
pydantic.main.ClassAttribute = utils.ClassAttribute # type: ignore


class EventedMetaclass(pydantic.main.ModelMetaclass):
Expand All @@ -99,7 +99,7 @@ class EventedMetaclass(pydantic.main.ModelMetaclass):
"""

@no_type_check
def __new__(
def __new__( # noqa: C901
mcs: type, name: str, bases: tuple, namespace: dict, **kwargs: Any
) -> EventedMetaclass:
"""Create new EventedModel class."""
Expand Down Expand Up @@ -153,7 +153,7 @@ def __new__(
return cls


def _get_field_dependents(cls: EventedModel) -> Dict[str, Set[str]]:
def _get_field_dependents(cls: EventedModel) -> Dict[str, Set[str]]: # noqa: C901
"""Return mapping of field name -> dependent set of property names.
Dependencies may be declared in the Model Config to emit an event
Expand Down Expand Up @@ -301,7 +301,7 @@ class Config:
# pydantic BaseModel configuration. see:
# https://pydantic-docs.helpmanual.io/usage/model_config/

class Config: # noqa
class Config:
# this seems to be necessary for the _json_encoders trick to work
json_encoders = {"____": None}

Expand Down
13 changes: 6 additions & 7 deletions src/psygnal/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,11 @@ def __getattr__(self, name: str) -> Any:
return self.__getattribute__(name)

@overload
def __get__(self, instance: None, owner: Type[Any] | None = None) -> Signal: # noqa
def __get__(self, instance: None, owner: Type[Any] | None = None) -> Signal:
... # pragma: no cover

@overload
def __get__( # noqa
self, instance: Any, owner: Type[Any] | None = None
) -> SignalInstance:
def __get__(self, instance: Any, owner: Type[Any] | None = None) -> SignalInstance:
... # pragma: no cover

def __get__(
Expand Down Expand Up @@ -806,7 +804,7 @@ def emit(
# will return `None` if emitter is blocked
... # pragma: no cover

def emit(
def emit( # noqa: D417
self,
*args: Any,
check_nargs: bool = False,
Expand All @@ -815,8 +813,9 @@ def emit(
) -> EmitThread | None:
"""Emit this signal with arguments `args`.
NOTE:
`check_args` and `check_types` both add overhead when calling emit.
!!! note
`check_args` and `check_types` both add overhead when calling emit.
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion src/psygnal/containers/_evented_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _move_plan(
else:
raise TypeError(
"Can only move integer or slice indices"
) # pragma: no cover # noqa
) # pragma: no cover

to_move = list(dict.fromkeys(to_move))

Expand Down
3 changes: 1 addition & 2 deletions src/psygnal/containers/_evented_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from itertools import chain
from typing import Any, Dict, Iterable, Iterator, MutableSet, Set, Tuple, TypeVar, Union

from typing_extensions import Final, Literal

from psygnal import Signal, SignalGroup
from typing_extensions import Final, Literal

_T = TypeVar("_T")
_Cls = TypeVar("_Cls", bound="_BaseMutableSet")
Expand Down
1 change: 0 additions & 1 deletion tests/containers/test_evented_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest.mock import Mock

import pytest

from psygnal.containers._evented_dict import EventedDict


Expand Down
1 change: 0 additions & 1 deletion tests/containers/test_evented_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import numpy as np
import pytest

from psygnal import EmissionInfo, Signal, SignalGroup
from psygnal.containers import EventedList

Expand Down
29 changes: 14 additions & 15 deletions tests/containers/test_evented_proxy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest.mock import Mock, call

import numpy as np

from psygnal import SignalGroup
from psygnal.containers import (
EventedCallableObjectProxy,
Expand Down Expand Up @@ -65,23 +64,23 @@ def __init__(self) -> None:
assert not _evented_proxy._OBJ_CACHE


def test_in_place_proxies():
def test_in_place_proxies(): # noqa: C901
# fmt: off
class T:
x = 0
def __iadd__(self, other): return self # noqa
def __isub__(self, other): return self # noqa
def __imul__(self, other): return self # noqa
def __imatmul__(self, other): return self # noqa
def __itruediv__(self, other): return self # noqa
def __ifloordiv__(self, other): return self # noqa
def __imod__(self, other): return self # noqa
def __ipow__(self, other): return self # noqa
def __ilshift__(self, other): return self # noqa
def __irshift__(self, other): return self # noqa
def __iand__(self, other): return self # noqa
def __ixor__(self, other): return self # noqa
def __ior__(self, other): return self # noqa
def __iadd__(self, other): return self
def __isub__(self, other): return self
def __imul__(self, other): return self
def __imatmul__(self, other): return self
def __itruediv__(self, other): return self
def __ifloordiv__(self, other): return self
def __imod__(self, other): return self
def __ipow__(self, other): return self
def __ilshift__(self, other): return self
def __irshift__(self, other): return self
def __iand__(self, other): return self
def __ixor__(self, other): return self
def __ior__(self, other): return self
# fmt: on

t = EventedObjectProxy(T())
Expand Down
1 change: 0 additions & 1 deletion tests/containers/test_evented_set.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest.mock import Mock, call

import pytest

from psygnal.containers import EventedOrderedSet, EventedSet, OrderedSet


Expand Down
1 change: 0 additions & 1 deletion tests/containers/test_selectable_evented_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest.mock import Mock

import pytest

from psygnal.containers import SelectableEventedList


Expand Down
1 change: 0 additions & 1 deletion tests/test_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import time

import pytest

from psygnal import Signal

pytest.importorskip("pytest_benchmark")
Expand Down
1 change: 0 additions & 1 deletion tests/test_evented_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import numpy as np
import pytest

from psygnal import SignalGroup, evented


Expand Down
3 changes: 1 addition & 2 deletions tests/test_evented_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import numpy as np
import pytest
from psygnal import EventedModel, SignalGroup
from pydantic import PrivateAttr
from typing_extensions import Protocol, runtime_checkable

from psygnal import EventedModel, SignalGroup


def test_creating_empty_evented_model():
"""Test creating an empty evented pydantic model."""
Expand Down
1 change: 0 additions & 1 deletion tests/test_group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest.mock import Mock, call

import pytest

from psygnal import EmissionInfo, Signal, SignalGroup


Expand Down
Loading

0 comments on commit fb09cfd

Please sign in to comment.