Skip to content

refactor: mechanical readability pass (#199) - #206

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/199-readability-pass
Sep 12, 2026
Merged

refactor: mechanical readability pass (#199)#206
lesnik512 merged 1 commit into
mainfrom
refactor/199-readability-pass

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Closes #199.

Four independent cleanups from the same read-through, no behaviour change in any of them.

1. self.bootstrap_config. as line noise

A config = self.bootstrap_config local in every method that uses the prefix more than
twice. The issue named four files; this went repo-wide once the same shape turned up in
fastmcp_bootstrapper.py and in the Sentry, Pyroscope and logging instruments — the
Sentry bootstrap() alone spent the prefix 12 times. An AST sweep now reports no method
over the threshold. Methods at exactly two uses are left alone, which is where the issue
drew the line.

Plain config =, never config: typing.Final =: CorsInstrument.bootstrap() already
spelled it that way before this change, so the codebase had a convention to match.

2. The teardown-error idiom is transcribed three times

One collect_teardown_errors() context manager in exceptions.py, next to the error it
raises. It yields a collector whose capture(name) records one step's failure and lets
teardown continue; the optional logger argument carries base.py's per-failure warning,
which the two instruments deliberately do not emit.

Both nested finally shapes in LoggingInstrument.teardown() are preserved verbatim
rather than folded into capture(). capture() swallows Exception, so the finallys
are what still guarantee _detach_record_filters() and the factory reset run when a
BaseException — a KeyboardInterrupt mid-teardown — passes through.

raise ... from collector.errors[0][1] still fires outside any except, so __cause__
is the first failure and __context__ stays None, as in all three originals.

One observable drift worth naming: the teardown warning's LogRecord.pathname and
funcName now point at exceptions.py. Same logger name, same message; nothing asserts
it.

3. BaseBootstrapper reads out of order

__init__ moved directly under the class attributes, and its instrument-selection loop
extracted to _select_instruments().

That extraction adds a stack frame, so the InstrumentDependencyMissingWarning
stacklevel had to go 3 -> 4 to keep landing on the line that constructed the
bootstrapper. The literal now depends on every bootstrapper defining its own __init__,
including FreeBootstrapper's pure super() pass-through — delete that one-line
delegation and the warning silently re-points at lite-bootstrap's own source. Pinned as an
invariant in test_free_bootstrap.py, which fails if that __init__ goes away.

4. types.BootstrapObjectT is dead

Deleted. No references in the package, tests, benchmarks or docs. ApplicationT in the
same file is live and stays.

Tests

tests/test_exceptions.py is new: the collector's contract is not fully reachable through
the three call sites, and the repo gates on 100% coverage. It covers aggregation and
chaining, the silent path, the logging argument, and an invariant that capture() lets
every non-Exception BaseException through — broadening that except would make
teardown uninterruptible.

ruff format, ruff check, ty check clean; 275 tests pass at 100% statement coverage.
Branch coverage is unchanged from main at 99.10%, the same 32 partial branches, all of
them module-level if import_checker.is_X_installed: guards.

- alias `config = self.bootstrap_config` in every method that uses it more
  than twice, across the bootstrappers and the OpenTelemetry, Sentry,
  Pyroscope and logging instruments
- replace the three transcriptions of the teardown-error idiom with one
  `collect_teardown_errors()` context manager next to `TeardownError`
- move `BaseBootstrapper.__init__` under the class attributes and extract
  its instrument-selection loop into `_select_instruments()`
- delete the unreferenced `types.BootstrapObjectT`
@lesnik512
lesnik512 merged commit c210381 into main Sep 12, 2026
12 checks passed
@lesnik512
lesnik512 deleted the refactor/199-readability-pass branch September 12, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mechanical readability pass: repeated config prefix, duplicated teardown idiom, BaseBootstrapper ordering, dead TypeVar

1 participant