Skip to content

Commit

Permalink
ci: Switch to ruff from flake8, flake8-comprehensions, isort, pydocstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 12, 2023
1 parent b09ec54 commit aae5c1e
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 51 deletions.
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: chartboost/ruff-action@v1
- uses: actions/setup-python@v4
with:
python-version: '3.11'
Expand Down
18 changes: 4 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,8 @@ repos:
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.278
hooks:
- id: flake8
additional_dependencies: [flake8-comprehensions]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/pydocstyle
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: [tomli]
files: ^(?!tests)
- id: ruff
# args: [--fix, --exit-non-zero-on-fix]
7 changes: 2 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
import os
import sys
from pathlib import Path

sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, Path.resolve(".."))


# -- Project information -----------------------------------------------------
Expand Down
24 changes: 15 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,33 @@ build-backend = "setuptools.build_meta"
[tool.black]
line-length = 119

[tool.isort]
profile = 'black'
line_length = 119

[tool.pydocstyle]
match_dir = '(?!tests).*'
ignore = 'D100,D104,D200,D203,D205,D212,D400,D415'

[tool.ruff]
line-length = 119
select = ["ALL"]
ignore = ["ANN", "COM", "EM", "D100", "D104", "D200", "D203", "D205", "D212", "D400", "D415"]
ignore = [
"ANN", "COM", "CPY", "EM",
"D100", "D104", "D200", "D203", "D205", "D212", "D400", "D415",
"ARG001", "ARG002", # unused arguments
"PLR0913", # many arguments (decorators)
]
target-version = "py310"

[tool.ruff.per-file-ignores]
"docs/conf.py" = ["INP001"] # no __init__.py file
"tests/*" = [
"D", # docstring
"ARG001", # unused pytest fixture
"FBT003", # boolean positional value
"PLR2004", # unnamed constant value
"PLW0602", # globals
"PLW0603", # globals
"PT004", # fixture naming convention
"S101", # assert
"TRY002", # raise
]
"tests/fixtures/*" = [
"INP001", # no __init__.py file
"T201", # print
]

[tool.ruff.flake8-builtins]
Expand Down
4 changes: 0 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
[flake8]
max-line-length = 119
extend-ignore = E203

[metadata]
name = yapw
version = 0.1.3
Expand Down
25 changes: 14 additions & 11 deletions yapw/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import signal
import threading
from collections import namedtuple
from collections.abc import Callable
from concurrent.futures import ThreadPoolExecutor
from types import FrameType
from typing import Any, Generic, TypeVar
from typing import TYPE_CHECKING, Any, Generic, TypeVar

import pika
from pika.adapters.asyncio_connection import AsyncioConnection
Expand All @@ -25,9 +23,14 @@

from yapw.decorators import halt
from yapw.ossignal import signal_names
from yapw.types import ConsumerCallback, Decode, Decorator, Encode, State
from yapw.util import basic_publish_debug_args, basic_publish_kwargs, default_decode, default_encode

if TYPE_CHECKING:
from collections.abc import Callable
from types import FrameType

from yapw.types import ConsumerCallback, Decode, Decorator, Encode, State

T = TypeVar("T")
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,7 +73,9 @@ class Base(Generic[T]):
# `connection` and `interrupt` aren't "safe to use" but can be "used safely" like in:
# https://github.com/pika/pika/blob/master/examples/basic_consumer_threaded.py
#: Attributes that can - and are expected to - be used safely in consumer callbacks.
__safe__ = {"connection", "interrupt", "exchange", "encode", "content_type", "delivery_mode", "format_routing_key"}
__safe__ = frozenset(
["connection", "interrupt", "exchange", "encode", "content_type", "delivery_mode", "format_routing_key"]
)

def __init__(
self,
Expand Down Expand Up @@ -180,15 +185,14 @@ def interrupt(self) -> None:
"""
Override this method in subclasses to shut down gracefully (e.g. wait for threads to terminate).
"""
pass

@property
def state(self): # type: ignore # anonymous class
def state(self): # type: ignore[no-untyped-def] # anonymous class
"""
A named tuple of attributes that can be used within threads.
"""
# Don't pass `self` to the callback, to prevent use of unsafe attributes and mutation of safe attributes.
klass = namedtuple("State", self.__safe__) # type: ignore # python/mypy#848 "This just never will happen"
klass = namedtuple("State", self.__safe__) # type: ignore[misc] # python/mypy#848 "just never will happen"
return klass(**{attr: getattr(self, attr) for attr in self.__safe__})


Expand Down Expand Up @@ -234,8 +238,8 @@ def declare_queue(
self.channel.queue_declare(queue=queue_name, durable=self.durable, arguments=arguments)

for routing_key in routing_keys:
routing_key = self.format_routing_key(routing_key)
self.channel.queue_bind(queue=queue_name, exchange=self.exchange, routing_key=routing_key)
formatted_routing_key = self.format_routing_key(routing_key)
self.channel.queue_bind(queue=queue_name, exchange=self.exchange, routing_key=formatted_routing_key)

# https://github.com/pika/pika/blob/master/examples/basic_consumer_threaded.py
def consume(
Expand Down Expand Up @@ -650,4 +654,3 @@ def channel_consumeok_callback(self, method: pika.frame.Method[pika.spec.Basic.C
"""
Override this method in subclasses to perform any other work, once the consumer is started.
"""
pass
15 changes: 9 additions & 6 deletions yapw/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ def errback(exception):
from __future__ import annotations

import logging
from collections.abc import Callable
from typing import Any

import pika
from typing import TYPE_CHECKING, Any

from yapw.methods import add_callback_threadsafe, nack
from yapw.types import ConsumerCallback, Decode, State

if TYPE_CHECKING:
from collections.abc import Callable

import pika

from yapw.types import ConsumerCallback, Decode, State

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +74,7 @@ def decorate(
message = decode(body, properties.content_type)
try:
callback(state, channel, method, properties, message)
except Exception as exception:
except Exception as exception: # noqa: BLE001
errback(exception)
finally:
if finalback:
Expand Down
2 changes: 1 addition & 1 deletion yapw/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def publish(
:param routing_key: the routing key
"""
keywords = basic_publish_kwargs(state, message, routing_key)
keywords.update(kwargs) # type: ignore # PEP 692 disallows updating TypedDict (PublishKeywords) with Dict.
keywords.update(kwargs) # type: ignore[typeddict-item] # PEP 692 disallows updating TypedDict with Dict

_channel_method_from_thread(state.connection, channel, "publish", *args, **keywords)
logger.debug(*basic_publish_debug_args(channel, message, keywords))
Expand Down
2 changes: 1 addition & 1 deletion yapw/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

jsonlib = orjson
except ImportError:
jsonlib = json # type: ignore
jsonlib = json # type: ignore[misc]

from yapw.types import PublishKeywords, State

Expand Down

0 comments on commit aae5c1e

Please sign in to comment.