Skip to content

opentelemetry-instrumentation-httpx: support strict typing - #4833

Open
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:httpx-strict-typing
Open

opentelemetry-instrumentation-httpx: support strict typing#4833
henry3260 wants to merge 2 commits into
open-telemetry:mainfrom
henry3260:httpx-strict-typing

Conversation

@henry3260

Copy link
Copy Markdown
Contributor

Description

Add opentelemetry-instrumentation-httpx to the pyright strict typecheck
environment (tox.ini typecheck deps + pyright include in pyproject.toml)
and fix the typing across the instrumentation. Follow-up to the review
discussion in #4730.

Notable changes, none of which are intended to change runtime behavior:

  • Restructure the isinstance dispatch in _extract_response (check tuple
    first) so pyright narrowing works soundly in both branches; this removes the
    previously flagged unnecessary typing.cast calls.
  • Declare the transport handle_request/handle_async_request methods as
    returning httpx.Response to be compatible with the httpx.BaseTransport
    override; the httpx < 0.20 tuple form is still handled at runtime.
  • Dispatch sync/async hooks in instrument_client via TypeIs guards
    (pyright does not narrow callable unions in the negative direction, so a
    pair of positive guards is used).
  • Type span/metric attribute dicts as dict[str, AttributeValue] and make the
    wrapper histogram/hook parameters optional to match call sites.
  • Widen RequestInfo/ResponseInfo url and extensions field annotations
    to match the raw values the transport layer actually passes.

Remaining suppressions: two typing.cast calls at the transport returns
(control-flow invariant pyright cannot see, documented inline) and two
targeted pyright: ignore comments for untyped wrapt and the deprecated
MetricInstruments name required by the old semconv mode — both follow
existing precedent in already-typechecked packages.

Package tests remain excluded from typecheck for now, consistent with the
other packages in the include list.

Fixes #4818

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • tox -e typecheck — 0 errors for the package
  • tox -e lint-instrumentation-httpx — 10.00/10
  • tox -e py312-test-instrumentation-httpx-{0,1,2,3}-wrapt2,
    py312-test-instrumentation-httpx-1-wrapt1, and
    py310-test-instrumentation-httpx-1-wrapt2 — all pass; the -0 factor
    (httpx 0.18) exercises the legacy tuple code path touched by the
    _extract_response restructuring

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added (typing-only change; covered by the existing test suite)
  • Documentation has been updated (not applicable)

@henry3260
henry3260 requested a review from a team as a code owner July 18, 2026 01:23
@henry3260
henry3260 force-pushed the httpx-strict-typing branch from ed0be17 to 2a97a2c Compare July 18, 2026 01:24
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Aug 1, 2026
@henry3260

Copy link
Copy Markdown
Contributor Author

Gentle ping @aabmass
Could you review this PR when you got a chance? Thanks!

@henry3260
henry3260 force-pushed the httpx-strict-typing branch from 8a8ba91 to 83a27a5 Compare August 1, 2026 04:27
@henry3260
henry3260 force-pushed the httpx-strict-typing branch from 83a27a5 to df005d7 Compare August 1, 2026 05:58
@github-actions github-actions Bot removed the Stale label Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentor-httpx: support strict typing

1 participant