Skip to content

Commit

Permalink
Migrate to Ruff (#984)
Browse files Browse the repository at this point in the history
* Replace flake8, black, reorder-python-imports with Ruff

* Apply automated Ruff lint fixes

* Temporarily disable E501

Will come back to this, just want to make sure Ruff isn't breaking
anything first with the existing changes.

* Manual lint fixes

* Revert "Temporarily disable E501"

This reverts commit f593bb0.

* Manual E501 lint fixes

* Set target-version = "py39" and re-run linter
  • Loading branch information
chriskuehl authored Nov 1, 2024
1 parent c389674 commit c5b7ca9
Show file tree
Hide file tree
Showing 134 changed files with 1,071 additions and 1,649 deletions.
12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
REORDER_PYTHON_IMPORTS := reorder-python-imports --py3-plus --separate-from-import --separate-relative
PYTHON_SOURCE = $(shell find baseplate/ tests/ -name '*.py')
PYTHON_EXAMPLES = $(shell find docs/ -name '*.py')

Expand Down Expand Up @@ -53,16 +52,13 @@ test: doctest .venv

.PHONY: fmt
fmt: .venv
.venv/bin/$(REORDER_PYTHON_IMPORTS) --exit-zero-even-if-changed $(PYTHON_SOURCE)
.venv/bin/black baseplate/ tests/
.venv/bin/$(REORDER_PYTHON_IMPORTS) --application-directories /tmp --exit-zero-even-if-changed $(PYTHON_EXAMPLES)
.venv/bin/black docs/ # separate so it uses its own pyproject.toml
.venv/bin/ruff check --fix
.venv/bin/ruff format

.PHONY: lint
lint: .venv
.venv/bin/$(REORDER_PYTHON_IMPORTS) --diff-only $(PYTHON_SOURCE)
.venv/bin/black --diff --check baseplate/ tests/
.venv/bin/flake8 baseplate tests
.venv/bin/ruff check
.venv/bin/ruff format --check
PYTHONPATH=. .venv/bin/pylint baseplate/
.venv/bin/mypy baseplate/

Expand Down
60 changes: 25 additions & 35 deletions baseplate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import logging
import os
import random

from collections.abc import Iterator
from contextlib import contextmanager
from types import TracebackType
from typing import Any
from typing import Callable
from typing import Dict
from typing import Iterator
from typing import List
from typing import NamedTuple
from typing import Optional
from typing import Tuple
from typing import Type
from typing import Any, Callable, NamedTuple, Optional

import gevent.monkey
from pkg_resources import DistributionNotFound, get_distribution

from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution

from baseplate.lib import config
from baseplate.lib import get_calling_module_name
from baseplate.lib import metrics
from baseplate.lib import UnknownCallerError

from baseplate.lib import UnknownCallerError, config, get_calling_module_name, metrics

try:
__version__ = get_distribution(__name__).version
Expand Down Expand Up @@ -51,7 +37,7 @@ def on_server_span_created(self, context: "RequestContext", server_span: "Server
raise NotImplementedError


_ExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
_ExcInfo = tuple[Optional[type[BaseException]], Optional[BaseException], Optional[TracebackType]]


class SpanObserver:
Expand Down Expand Up @@ -157,7 +143,7 @@ def from_upstream(
raise ValueError("invalid sampled value")

if flags is not None:
if not 0 <= flags < 2 ** 64:
if not 0 <= flags < 2**64:
raise ValueError("invalid flags value")

return cls(trace_id, parent_id, span_id, sampled, flags)
Expand All @@ -182,7 +168,7 @@ class RequestContext:

def __init__(
self,
context_config: Dict[str, Any],
context_config: dict[str, Any],
prefix: Optional[str] = None,
span: Optional["Span"] = None,
wrapped: Optional["RequestContext"] = None,
Expand All @@ -197,7 +183,7 @@ def __init__(
# reference. so we fake it here and say "trust us".
#
# this would be much cleaner with a different API but this is where we are.
self.span: "Span" = span # type: ignore
self.span: Span = span # type: ignore

def __getattr__(self, name: str) -> Any:
try:
Expand Down Expand Up @@ -279,9 +265,9 @@ def __init__(self, app_config: Optional[config.RawConfig] = None) -> None:
...
"""
self.observers: List[BaseplateObserver] = []
self.observers: list[BaseplateObserver] = []
self._metrics_client: Optional[metrics.Client] = None
self._context_config: Dict[str, Any] = {}
self._context_config: dict[str, Any] = {}
self._app_config = app_config or {}

self.service_name = self._app_config.get("baseplate.service_name")
Expand Down Expand Up @@ -353,18 +339,22 @@ def configure_observers(self) -> None:
skipped.append("metrics")

if "tracing.service_name" in self._app_config:
from baseplate.observers.tracing import tracing_client_from_config
from baseplate.observers.tracing import TraceBaseplateObserver
from baseplate.observers.tracing import (
TraceBaseplateObserver,
tracing_client_from_config,
)

tracing_client = tracing_client_from_config(self._app_config)
self.register(TraceBaseplateObserver(tracing_client))
else:
skipped.append("tracing")

if "sentry.dsn" in self._app_config or "SENTRY_DSN" in os.environ:
from baseplate.observers.sentry import init_sentry_client_from_config
from baseplate.observers.sentry import SentryBaseplateObserver
from baseplate.observers.sentry import _SentryUnhandledErrorReporter
from baseplate.observers.sentry import (
SentryBaseplateObserver,
_SentryUnhandledErrorReporter,
init_sentry_client_from_config,
)

init_sentry_client_from_config(self._app_config)
_SentryUnhandledErrorReporter.install()
Expand All @@ -377,7 +367,7 @@ def configure_observers(self) -> None:
"The following observers are unconfigured and won't run: %s", ", ".join(skipped)
)

def configure_context(self, context_spec: Dict[str, Any]) -> None:
def configure_context(self, context_spec: dict[str, Any]) -> None:
"""Add a number of objects to each request's context object.
Configure and attach multiple clients to the
Expand Down Expand Up @@ -509,8 +499,8 @@ def server_context(self, name: str) -> Iterator[RequestContext]:
with self.make_server_span(context, name):
yield context

def get_runtime_metric_reporters(self) -> Dict[str, Callable[[Any], None]]:
specs: List[Tuple[Optional[str], Dict[str, Any]]] = [(None, self._context_config)]
def get_runtime_metric_reporters(self) -> dict[str, Callable[[Any], None]]:
specs: list[tuple[Optional[str], dict[str, Any]]] = [(None, self._context_config)]
result = {}
while specs:
prefix, spec = specs.pop(0)
Expand Down Expand Up @@ -550,7 +540,7 @@ def __init__(
self.context = context
self.baseplate = baseplate
self.component_name: Optional[str] = None
self.observers: List[SpanObserver] = []
self.observers: list[SpanObserver] = []

def register(self, observer: SpanObserver) -> None:
"""Register an observer to receive events from this span."""
Expand Down Expand Up @@ -640,7 +630,7 @@ def __enter__(self) -> "Span":

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_type: Optional[type[BaseException]],
value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
Expand All @@ -655,7 +645,7 @@ def make_child(
"""Return a child Span whose parent is this Span."""
raise NotImplementedError

def with_tags(self, tags: Dict[str, Any]) -> "Span":
def with_tags(self, tags: dict[str, Any]) -> "Span":
"""Declare a set of tags to be added to a span before starting it in the context manager.
Can be used as follow:
Expand Down
3 changes: 1 addition & 2 deletions baseplate/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
trace information is passed on and metrics are collected automatically.
"""
from typing import Any
from typing import TYPE_CHECKING

from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
import baseplate.lib.metrics
Expand Down
53 changes: 26 additions & 27 deletions baseplate/clients/cassandra.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import logging
import time

from collections.abc import Mapping, Sequence
from threading import Event
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Mapping
from typing import NamedTuple
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
NamedTuple,
Optional,
Union,
)

from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import _NOT_SET # pylint: disable=no-name-in-module
from cassandra.cluster import Cluster # pylint: disable=no-name-in-module
from cassandra.cluster import ExecutionProfile # pylint: disable=no-name-in-module
from cassandra.cluster import ResponseFuture # pylint: disable=no-name-in-module
from cassandra.cluster import Session # pylint: disable=no-name-in-module
from cassandra.query import BoundStatement # pylint: disable=no-name-in-module
from cassandra.query import PreparedStatement # pylint: disable=no-name-in-module
from cassandra.query import SimpleStatement # pylint: disable=no-name-in-module
from prometheus_client import Counter
from prometheus_client import Gauge
from prometheus_client import Histogram
from cassandra.cluster import ( # pylint: disable=no-name-in-module
_NOT_SET,
Cluster,
ExecutionProfile,
ResponseFuture,
Session,
)
from cassandra.query import ( # pylint: disable=no-name-in-module
BoundStatement,
PreparedStatement,
SimpleStatement,
)
from prometheus_client import Counter, Gauge, Histogram

from baseplate import Span
from baseplate.clients import ContextFactory
Expand Down Expand Up @@ -70,7 +69,7 @@ def cluster_from_config(
app_config: config.RawConfig,
secrets: Optional[SecretsStore] = None,
prefix: str = "cassandra.",
execution_profiles: Optional[Dict[str, ExecutionProfile]] = None,
execution_profiles: Optional[dict[str, ExecutionProfile]] = None,
**kwargs: Any,
) -> Cluster:
"""Make a Cluster from a configuration dictionary.
Expand Down Expand Up @@ -171,7 +170,7 @@ def __init__(
prometheus_cluster_name: Optional[str] = None,
):
self.session = session
self.prepared_statements: Dict[str, PreparedStatement] = {}
self.prepared_statements: dict[str, PreparedStatement] = {}
self.prometheus_client_name = prometheus_client_name
self.prometheus_cluster_name = prometheus_cluster_name

Expand Down Expand Up @@ -318,7 +317,7 @@ def _on_execute_failed(exc: BaseException, args: CassandraCallbackArgs, event: E
event.set()


RowFactory = Callable[[List[str], List[Tuple]], Any]
RowFactory = Callable[[list[str], list[tuple]], Any]
Query = Union[str, SimpleStatement, PreparedStatement, BoundStatement]
Parameters = Union[Sequence[Any], Mapping[str, Any]]

Expand All @@ -329,7 +328,7 @@ def __init__(
context_name: str,
server_span: Span,
session: Session,
prepared_statements: Dict[str, PreparedStatement],
prepared_statements: dict[str, PreparedStatement],
prometheus_client_name: Optional[str] = None,
prometheus_cluster_name: Optional[str] = None,
):
Expand Down
17 changes: 4 additions & 13 deletions baseplate/clients/kombu.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import abc
import time

from typing import Any
from typing import Generic
from typing import Optional
from typing import Type
from typing import TypeVar
from typing import Any, Generic, Optional, TypeVar

import kombu.serialization

from kombu import Connection
from kombu import Exchange
from kombu import Connection, Exchange
from kombu.pools import Producers
from prometheus_client import Counter
from prometheus_client import Histogram
from prometheus_client import Counter, Histogram
from thrift import TSerialization
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from thrift.protocol.TProtocol import TProtocolFactory
Expand All @@ -25,7 +17,6 @@
from baseplate.lib.prometheus_metrics import default_latency_buckets
from baseplate.lib.secrets import SecretsStore


T = TypeVar("T")

amqp_producer_labels = [
Expand Down Expand Up @@ -140,7 +131,7 @@ class KombuThriftSerializer(KombuSerializer[T]): # pylint: disable=unsubscripta

def __init__(
self,
thrift_class: Type[T],
thrift_class: type[T],
protocol_factory: TProtocolFactory = TBinaryProtocolAcceleratedFactory(),
):
self.thrift_class = thrift_class
Expand Down
Loading

0 comments on commit c5b7ca9

Please sign in to comment.