Skip to content

Make the base instruments generic over their config type and drop the instrument bootstrap_config ignores #285

Closed

Description

@lesnik512

ty 0.0.87 reports invalid-mutable-override wherever a subclass narrows bootstrap_config. #284 silenced the 25 sites with per-line ignores. They fall into two groups.

Framework instruments (20 ignores)

BaseInstrument is already generic over ConfigT, but each base instrument fixes the parameter (CorsInstrument(BaseInstrument[CorsConfig])). A framework instrument then has to re-annotate the field to get its own config type:

class FastAPICorsInstrument(CorsInstrument):
    bootstrap_config: FastAPIConfig  # ty: ignore[invalid-mutable-override]

The fix is to keep the base instruments generic and let the framework instrument bind the parameter:

CorsConfigT = typing.TypeVar("CorsConfigT", bound=CorsConfig)

class CorsInstrument(BaseInstrument[CorsConfigT]): ...

class FastAPICorsInstrument(CorsInstrument[FastAPIConfig]): ...

This applies to the Cors, HealthChecks, Logging, OpenTelemetry, Prometheus and Swagger instruments. It works with slotted dataclasses, passes ty 0.0.87 without the ignore, and changes nothing at runtime. An out-of-tree subclass of CorsInstrument keeps working; ty just sees its config type as unknown until it binds the parameter.

Bootstrappers (5 ignores, out of scope)

FastAPIBootstrapper and the other four bootstrappers narrow BaseBootstrapper.bootstrap_config: BaseConfig the same way. Adding a config parameter to BaseBootstrapper breaks every one-argument subscription: BaseBootstrapper[App] raises TypeError: Too few arguments at import, including in tests/instruments/test_logging_record_filters.py.

A default (typing_extensions.TypeVar("ConfigT", bound=BaseConfig, default=BaseConfig)) keeps BaseBootstrapper[App] working on 3.11 and 3.14 (checked with typing-extensions 4.12.2 and 4.15). It would raise the typing-extensions floor from 4.6 to 4.12, though, and the stdlib only has TypeVar defaults from 3.13, so #272 couldn't drop typing-extensions after 3.11's EOL. These 5 ignores stay until 3.13 is the floor or a major release allows the break.

Activity

  1. changed the title [-]Make instruments generic over their config type and drop the bootstrap_config ignores[/-] [+]Make the base instruments generic over their config type and drop the instrument bootstrap_config ignores[/+] on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions