Skip to content

Commit

Permalink
Include ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
gmr committed Jan 30, 2024
1 parent dddecb4 commit 22f0399
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 48 deletions.
18 changes: 18 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ testing = [
"isort",
"pre-commit>=3.5.0,<4",
"psycopg[binary,pool]>=3.1.16,<4",
"ruff",
"sentry-sdk>=1.39.1,<2"
]

Expand Down Expand Up @@ -86,6 +87,23 @@ include = ["rejected"]
[tool.hatch.version]
path = "rejected/__init__.py"

[tool.ruff]
line-length = 79
target-version = "py312"

[tool.ruff.format]
quote-style = "single"
skip-magic-trailing-comma = false

[tool.ruff.lint]
select = [
"C4", # flake8-comprehensions
"E", "W", # pycodestyle
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
]

[tool.yapf]
allow_split_before_dict_value = false
indent_dictionary_value = true
5 changes: 2 additions & 3 deletions rejected/amqp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
import os
import ssl
import typing

import pika
import pika.channel
Expand All @@ -26,7 +25,7 @@ def __init__(self, name: str, config: models.Connection,
self.channel = None
self.config = config
self.should_consume = should_consume
self.consumer_tag = '{}-{}'.format(consumer_name, os.getpid())
self.consumer_tag = f'{consumer_name}-{os.getpid()}'
self.name = name
self.publisher_confirm = publisher_confirmations
self.connection = self.connect()
Expand Down Expand Up @@ -80,7 +79,7 @@ def on_open_error(self, *args, **kwargs) -> None:
self.on_failure()

def on_closed(self, _conn: asyncio_connection.AsyncioConnection,
error: typing.Optional[BaseException]) -> None:
error: BaseException | None) -> None:
if self.is_connecting:
LOGGER.error('Connection %s failure while connecting (%s)',
self.name, error)
Expand Down
22 changes: 10 additions & 12 deletions rejected/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pwd
import sys
import types
import typing

LOGGER = logging.getLogger(__name__)

Expand All @@ -14,9 +13,9 @@ class Daemon:
"""Context manager for daemonizing the application using double-forking"""

def __init__(self,
user: typing.Optional[str] = None,
group: typing.Optional[str] = None,
pid_file: typing.Optional[str] = None) -> None:
user: str | None = None,
group: str | None = None,
pid_file: str | None = None) -> None:
"""Create an instance of the Daemon class
If the `user` specified does not match the current user, an attempt
Expand All @@ -39,10 +38,9 @@ def __enter__(self) -> 'Daemon':
sys.exit(1)
return self

def __exit__(self, exc_type: typing.Optional[typing.Type[BaseException]],
exc_val: typing.Optional[BaseException],
exc_tb: typing.Optional[types.TracebackType]) \
-> typing.Optional[bool]:
def __exit__(self, exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: types.TracebackType | None) -> bool | None:
"""Log an error on exit if it was not clean"""
self.pid_file.unlink(True)
return None
Expand Down Expand Up @@ -72,7 +70,7 @@ def fork() -> int:
return pid

@staticmethod
def get_gid(group: typing.Optional[str]) -> int:
def get_gid(group: str | None) -> int:
"""Return group id of the specified group, or current group if None"""
if group:
try:
Expand All @@ -82,7 +80,7 @@ def get_gid(group: typing.Optional[str]) -> int:
return os.getgid()

@staticmethod
def get_pid_file(pid_file: typing.Optional[str]) -> pathlib.Path:
def get_pid_file(pid_file: str | None) -> pathlib.Path:
"""Return a pathlib.Path instance for the specified path, or
find a valid place to write to.
Expand All @@ -101,7 +99,7 @@ def get_pid_file(pid_file: typing.Optional[str]) -> pathlib.Path:
raise RuntimeError('Could not find location for pid_file')

@staticmethod
def get_uid(username: typing.Optional[str]) -> int:
def get_uid(username: str | None) -> int:
"""Return group id of the specified group, or current group if None"""
if username:
try:
Expand Down Expand Up @@ -138,7 +136,7 @@ def redirect_standard_streams() -> None:
"""Ensure the child can read/write to standard streams appropriately"""
sys.stdout.flush()
sys.stderr.flush()
si = open(os.devnull, 'r')
si = open(os.devnull)
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
Expand Down
66 changes: 33 additions & 33 deletions rejected/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,41 @@ class LoggingLevel(str, enum.Enum):


class Binding(pydantic.BaseModel):
arguments: typing.Optional[dict] = None
arguments: dict | None = None
destination: str
destination_type: BindingType
routing_key: str
source: str


class Exchange(pydantic.BaseModel):
arguments: typing.Optional[dict] = None
arguments: dict | None = None
auto_delete: bool = False
durable: bool = True
name: str
type: str


class Queue(pydantic.BaseModel):
arguments: typing.Optional[dict] = None
arguments: dict | None = None
auto_delete: bool = False
durable: bool = True
name: str


class Definitions(pydantic.BaseModel):
bindings: typing.Optional[typing.List[Binding]] = None
exchanges: typing.Optional[typing.List[Exchange]] = None
queues: typing.Optional[typing.List[Queue]] = None
bindings: list[Binding] | None = None
exchanges: list[Exchange] | None = None
queues: list[Queue] | None = None

class Config:
arbitrary_types_allowed = True


class ComplexConnection(pydantic.BaseModel):
name: str
consume: typing.Optional[bool] = True
confirm: typing.Optional[bool] = False
consume: bool | None = True
confirm: bool | None = False


class Connection(pydantic.BaseModel):
Expand All @@ -64,7 +64,7 @@ class Connection(pydantic.BaseModel):
virtual_host: str = '/'
heartbeat_interval: int = 300
qos_prefetch: int = 1
definitions: typing.Optional[Definitions] = None
definitions: Definitions | None = None

class Config:
arbitrary_types_allowed = True
Expand All @@ -73,18 +73,18 @@ class Config:
class Consumer(pydantic.BaseModel):
class_path: str = pydantic.Field(
validation_alias=pydantic.AliasPath('class'))
connections: typing.List[typing.Union[str, ComplexConnection]]
connections: list[str | ComplexConnection]
queue: str
qty: int = 1
ack: bool = True
max_errors: int = 3
sentry_dsn: typing.Optional[str] = None
drop_exchange: typing.Optional[str] = None
sentry_dsn: str | None = None
drop_exchange: str | None = None
drop_invalid_messages: bool = True
error_exchange: typing.Optional[str] = None
message_type: typing.Optional[str] = None
error_max_retry: typing.Optional[int] = None
config: typing.Optional[dict] = None
error_exchange: str | None = None
message_type: str | None = None
error_max_retry: int | None = None
config: dict | None = None

class Config:
arbitrary_types_allowed = True
Expand All @@ -99,38 +99,38 @@ class Statsd(pydantic.BaseModel):

class Stats(pydantic.BaseModel):
log: bool = True
statsd: typing.Optional[Statsd] = None
statsd: Statsd | None = None

class Config:
arbitrary_types_allowed = True


class Application(pydantic.BaseModel):
connections: typing.Dict[str, Connection]
consumers: typing.Dict[str, Consumer]
poll_interval: typing.Union[float, int, None] = 60
sentry_dsn: typing.Optional[str] = None
stats: typing.Optional[Stats] = None
connections: dict[str, Connection]
consumers: dict[str, Consumer]
poll_interval: float | int | None = 60
sentry_dsn: str | None = None
stats: Stats | None = None

class Config:
arbitrary_types_allowed = True


class Daemon(pydantic.BaseModel):
user: typing.Optional[str] = None
group: typing.Optional[str] = None
pidfile: typing.Optional[str] = None
user: str | None = None
group: str | None = None
pidfile: str | None = None


class Logging(pydantic.BaseModel):
version: typing.Optional[int] = 1
filters: typing.Optional[typing.Dict[str, dict]]
formatters: typing.Optional[typing.Dict[str, dict]]
handlers: typing.Optional[typing.Dict[str, dict]]
loggers: typing.Optional[typing.Dict[str, dict]]
root: typing.Optional[typing.Dict[str, dict]] = None
incremental: typing.Optional[bool] = False
disable_existing_loggers: typing.Optional[bool] = True
version: int | None = 1
filters: dict[str, dict] | None
formatters: dict[str, dict] | None
handlers: dict[str, dict] | None
loggers: dict[str, dict] | None
root: dict[str, dict] | None = None
incremental: bool | None = False
disable_existing_loggers: bool | None = True


class Configuration(pydantic.BaseModel):
Expand Down

0 comments on commit 22f0399

Please sign in to comment.