From 5e0d81e36feccc4d99e9fc9201d689f20f564b98 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Wed, 11 Mar 2026 20:20:27 +0300 Subject: [PATCH] migrate to ty --- .gitignore | 1 - Justfile | 4 ++-- README.md | 1 - lite_bootstrap/bootstrappers/fastapi_bootstrapper.py | 2 +- lite_bootstrap/bootstrappers/faststream_bootstrapper.py | 8 ++++---- lite_bootstrap/bootstrappers/litestar_bootstrapper.py | 2 +- lite_bootstrap/instruments/opentelemetry_instrument.py | 2 +- lite_bootstrap/instruments/sentry_instrument.py | 2 +- pyproject.toml | 6 +----- tests/conftest.py | 9 +++++---- 10 files changed, 16 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 068012f..8cf31db 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ __pycache__/* .idea .DS_Store .env -.mypy_cache .pytest_cache .ruff_cache .coverage diff --git a/Justfile b/Justfile index fabf8c0..0853917 100644 --- a/Justfile +++ b/Justfile @@ -8,13 +8,13 @@ lint: uv run eof-fixer . uv run ruff format uv run ruff check --fix - uv run mypy . + uv run ty check lint-ci: uv run eof-fixer . --check uv run ruff format --check uv run ruff check --no-fix - uv run mypy . + uv run ty check test *args: uv run --no-sync pytest {{ args }} diff --git a/README.md b/README.md index 9b7345a..cb4df70 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ Lite-Bootstrap == [![Test Coverage](https://codecov.io/gh/modern-python/lite-bootstrap/branch/main/graph/badge.svg)](https://codecov.io/gh/modern-python/lite-bootstrap) -[![MyPy Strict](https://img.shields.io/badge/mypy-strict-blue)](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration) [![Supported versions](https://img.shields.io/pypi/pyversions/lite-bootstrap.svg)](https://pypi.python.org/pypi/lite-bootstrap) [![downloads](https://img.shields.io/pypi/dm/lite-bootstrap.svg)](https://pypistats.org/packages/lite-bootstrap) [![GitHub stars](https://img.shields.io/github/stars/modern-python/lite-bootstrap)](https://github.com/modern-python/lite-bootstrap/stargazers) diff --git a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py index 55f64a5..d8c98fc 100644 --- a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py @@ -64,7 +64,7 @@ class FastApiCorsInstrument(CorsInstrument): def bootstrap(self) -> None: self.bootstrap_config.application.add_middleware( - CORSMiddleware, + CORSMiddleware, # ty: ignore[invalid-argument-type] allow_origins=self.bootstrap_config.cors_allowed_origins, allow_methods=self.bootstrap_config.cors_allowed_methods, allow_headers=self.bootstrap_config.cors_allowed_headers, diff --git a/lite_bootstrap/bootstrappers/faststream_bootstrapper.py b/lite_bootstrap/bootstrappers/faststream_bootstrapper.py index f45ae30..bc5b2ed 100644 --- a/lite_bootstrap/bootstrappers/faststream_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/faststream_bootstrapper.py @@ -52,7 +52,7 @@ def __init__( @dataclasses.dataclass(kw_only=True, slots=True, frozen=True) class FastStreamConfig(HealthChecksConfig, LoggingConfig, OpentelemetryConfig, PrometheusConfig, SentryConfig): - application: "AsgiFastStream" = dataclasses.field(default_factory=lambda: AsgiFastStream()) + application: "AsgiFastStream" = dataclasses.field(default_factory=AsgiFastStream) opentelemetry_middleware_cls: type[FastStreamTelemetryMiddlewareProtocol] | None = None prometheus_middleware_cls: type[FastStreamPrometheusMiddlewareProtocol] | None = None @@ -103,7 +103,7 @@ def bootstrap(self) -> None: if self.bootstrap_config.opentelemetry_middleware_cls and self.bootstrap_config.application.broker: self.bootstrap_config.opentelemetry_middleware_cls(tracer_provider=get_tracer_provider()) self.bootstrap_config.application.broker.add_middleware( - self.bootstrap_config.opentelemetry_middleware_cls(tracer_provider=get_tracer_provider()) # type: ignore[arg-type] + self.bootstrap_config.opentelemetry_middleware_cls(tracer_provider=get_tracer_provider()) ) @@ -116,7 +116,7 @@ class FastStreamSentryInstrument(SentryInstrument): class FastStreamPrometheusInstrument(PrometheusInstrument): bootstrap_config: FastStreamConfig collector_registry: "prometheus_client.CollectorRegistry" = dataclasses.field( - default_factory=lambda: prometheus_client.CollectorRegistry(), init=False + default_factory=prometheus_client.CollectorRegistry, init=False ) not_ready_message = PrometheusInstrument.not_ready_message + " or prometheus_middleware_cls is missing" missing_dependency_message = "prometheus_client is not installed" @@ -138,7 +138,7 @@ def bootstrap(self) -> None: ) if self.bootstrap_config.prometheus_middleware_cls and self.bootstrap_config.application.broker: self.bootstrap_config.application.broker.add_middleware( - self.bootstrap_config.prometheus_middleware_cls(registry=self.collector_registry) # type: ignore[arg-type] + self.bootstrap_config.prometheus_middleware_cls(registry=self.collector_registry) ) diff --git a/lite_bootstrap/bootstrappers/litestar_bootstrapper.py b/lite_bootstrap/bootstrappers/litestar_bootstrapper.py index 0698f7d..c6b226b 100644 --- a/lite_bootstrap/bootstrappers/litestar_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/litestar_bootstrapper.py @@ -49,7 +49,7 @@ class LitestarConfig( SentryConfig, SwaggerConfig, ): - application_config: "AppConfig" = dataclasses.field(default_factory=lambda: AppConfig()) + application_config: "AppConfig" = dataclasses.field(default_factory=AppConfig) opentelemetry_excluded_urls: list[str] = dataclasses.field(default_factory=list) prometheus_additional_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict) swagger_extra_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict) diff --git a/lite_bootstrap/instruments/opentelemetry_instrument.py b/lite_bootstrap/instruments/opentelemetry_instrument.py index 5cb854d..24398e4 100644 --- a/lite_bootstrap/instruments/opentelemetry_instrument.py +++ b/lite_bootstrap/instruments/opentelemetry_instrument.py @@ -7,7 +7,7 @@ if typing.TYPE_CHECKING: - from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined] + from opentelemetry.instrumentation.instrumentor import BaseInstrumentor if import_checker.is_opentelemetry_installed: from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter diff --git a/lite_bootstrap/instruments/sentry_instrument.py b/lite_bootstrap/instruments/sentry_instrument.py index b7d2122..b40f860 100644 --- a/lite_bootstrap/instruments/sentry_instrument.py +++ b/lite_bootstrap/instruments/sentry_instrument.py @@ -58,7 +58,7 @@ def enrich_sentry_event_from_structlog_log( return None if event_name := loaded_formatted_log.get("event"): - event["logentry"]["formatted"] = event_name # type: ignore[index] + event["logentry"]["formatted"] = event_name else: return event diff --git a/pyproject.toml b/pyproject.toml index 91e4be6..f6d7456 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -123,7 +123,7 @@ dev = [ "redis>=5.2.1", ] lint = [ - "mypy", + "ty", "ruff", "eof-fixer", ] @@ -136,10 +136,6 @@ build-backend = "uv_build" module-name = "lite_bootstrap" module-root = "" -[tool.mypy] -python_version = "3.10" -strict = true - [tool.ruff] fix = false unsafe-fixes = true diff --git a/tests/conftest.py b/tests/conftest.py index cf03a8c..7a4c390 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,13 +5,14 @@ import pytest import sentry_sdk -from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore[attr-defined] +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor +from sentry_sdk.envelope import Envelope from structlog.typing import EventDict, WrappedLogger from lite_bootstrap import import_checker -class CustomInstrumentor(BaseInstrumentor): # type: ignore[misc] +class CustomInstrumentor(BaseInstrumentor): def instrumentation_dependencies(self) -> typing.Collection[str]: return [] @@ -22,9 +23,9 @@ def _uninstrument(self, **kwargs: typing.Mapping[str, typing.Any]) -> None: class SentryTestTransport(sentry_sdk.Transport): def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None: # noqa: ANN401 super().__init__(*args, **kwargs) - self.mock_envelopes: list[sentry_sdk.envelope.Envelope] = [] + self.mock_envelopes: list[Envelope] = [] - def capture_envelope(self, envelope: sentry_sdk.envelope.Envelope) -> None: + def capture_envelope(self, envelope: Envelope) -> None: self.mock_envelopes.append(envelope)