Skip to content

Commit

Permalink
Temporarily restore Python 3.8 support (#1024)
Browse files Browse the repository at this point in the history
* Revert "Migrate to Ruff (#984)"

This reverts commit c5b7ca9.

* poetry lock --no-update

* Revert "Drop python3.8 support (#1003)"

This reverts commit fe59ab2.

* poetry lock --no-update

* Fix mypy errors

* Add future annotations import to test
  • Loading branch information
chriskuehl authored Nov 13, 2024
1 parent 60c859d commit f1f2f08
Show file tree
Hide file tree
Showing 137 changed files with 2,070 additions and 1,368 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

container:
image: python:${{ matrix.python-version }}
Expand Down
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 @@ -52,13 +53,16 @@ test: doctest .venv

.PHONY: fmt
fmt: .venv
.venv/bin/ruff check --fix
.venv/bin/ruff format
.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

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

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

from contextlib import contextmanager
from types import TracebackType
from typing import Any, Callable, NamedTuple, Optional
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

import gevent.monkey
from pkg_resources import DistributionNotFound, get_distribution

from baseplate.lib import UnknownCallerError, config, get_calling_module_name, metrics
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


try:
__version__ = get_distribution(__name__).version
Expand Down Expand Up @@ -37,7 +51,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 @@ -143,7 +157,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 @@ -168,7 +182,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 @@ -183,7 +197,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 @@ -265,9 +279,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 @@ -339,22 +353,18 @@ def configure_observers(self) -> None:
skipped.append("metrics")

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

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 (
SentryBaseplateObserver,
_SentryUnhandledErrorReporter,
init_sentry_client_from_config,
)
from baseplate.observers.sentry import init_sentry_client_from_config
from baseplate.observers.sentry import SentryBaseplateObserver
from baseplate.observers.sentry import _SentryUnhandledErrorReporter

init_sentry_client_from_config(self._app_config)
_SentryUnhandledErrorReporter.install()
Expand All @@ -367,7 +377,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 @@ -499,8 +509,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 @@ -540,7 +550,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 @@ -630,7 +640,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 @@ -645,7 +655,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: 2 additions & 1 deletion baseplate/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
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: 27 additions & 26 deletions baseplate/clients/cassandra.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import logging
import time
from collections.abc import Mapping, Sequence

from threading import Event
from typing import (
TYPE_CHECKING,
Any,
Callable,
NamedTuple,
Optional,
Union,
)
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 cassandra.auth import PlainTextAuthProvider
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 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 baseplate import Span
from baseplate.clients import ContextFactory
Expand Down Expand Up @@ -69,7 +70,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 @@ -170,7 +171,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 @@ -317,7 +318,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 @@ -328,7 +329,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: 13 additions & 4 deletions baseplate/clients/kombu.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import abc
import time
from typing import Any, Generic, Optional, TypeVar

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

import kombu.serialization
from kombu import Connection, Exchange

from kombu import Connection
from kombu import Exchange
from kombu.pools import Producers
from prometheus_client import Counter, Histogram
from prometheus_client import Counter
from prometheus_client import Histogram
from thrift import TSerialization
from thrift.protocol.TBinaryProtocol import TBinaryProtocolAcceleratedFactory
from thrift.protocol.TProtocol import TProtocolFactory
Expand All @@ -17,6 +25,7 @@
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 @@ -131,7 +140,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 f1f2f08

Please sign in to comment.