Skip to content

[CB-0] [sdk-superfan] Fix mypy and pyright on Python 3.12 TypeAliasType branch - #217

Open
spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/mypy-py312-typealiastype
Open

spicysouvlaki wants to merge 1 commit into
mainfrom
wschmitt/mypy-py312-typealiastype

Conversation

@spicysouvlaki

@spicysouvlaki spicysouvlaki commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

What happened

#178: mypy src on Python 3.12 fails with
src/browserbase/_utils/_typing.py:56: error: Argument 2 to <tuple> has incompatible type "type[typing.TypeAliasType]"; expected "type[typing_extensions.TypeAliasType]" [arg-type]

Why

src/browserbase/_utils/_typing.py:54-56 annotates the tuple with the typing_extensions class, then appends the distinct stdlib class on 3.12+. Pyright strict also fails there (reportConstantRedefinition). CI lints only on 3.9, where that branch is unreachable.

Fix

Build the tuple in an if/else on sys.version_info with no shared annotation. TypeIs on is_type_alias_type still drives narrowing.

Tests

  • scripts/lint now also runs pyright and mypy with a 3.12 target.
  • Before: ./scripts/lint fails (pyright 3.12: 2 errors; mypy 3.12: 1 error). After: passes, 0 errors on 3.9 / 3.12 / 3.13 targets for both.
  • ./scripts/test (steady mock): 3.9 and 3.14, pydantic v1 + v2, all pass.

Risk and rollout

Touches the generated _utils/_typing.py and scripts/lint. Stainless seals both, so expect a possible merge conflict if the template changes those lines. No runtime behavior change.

Fixes #178

🤖 Generated with Claude Code

`_TYPE_ALIAS_TYPES` was annotated `tuple[type[typing_extensions.TypeAliasType], ...]`
and then extended with the stdlib `typing.TypeAliasType` under
`sys.version_info >= (3, 12)`. On 3.12+ these are distinct classes, so:

- mypy 1.17: `Argument 2 to <tuple> has incompatible type
  "type[typing.TypeAliasType]"; expected "type[typing_extensions.TypeAliasType]"`
- pyright 1.1.399 strict: reportConstantRedefinition plus the same
  assignability error.

CI never saw it because lint runs on `.python-version` (3.9), where both
checkers treat the 3.12 branch as unreachable. Reproduced from the 3.9 env
with `mypy --python-version 3.12 .` and `pyright --pythonversion 3.12`.

Fix: pick the tuple per version branch with no shared annotation, so each
checker infers the concrete tuple for the target it evaluates. Narrowing is
unchanged: it comes from `is_type_alias_type`'s `TypeIs` return type, not
the tuple's declared type.

Guard: scripts/lint now also runs pyright and mypy with a 3.12 target. Both
fail on origin/main and pass with this change; 3.9 and 3.13 targets pass too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@spicysouvlaki
spicysouvlaki marked this pull request as ready for review November 16, 2026 21:46
@spicysouvlaki
spicysouvlaki requested a review from a team as a code owner November 16, 2026 21:46

This branch has not been deployed

No deployments
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.

Mypy fails on Python 3.12 TypeAliasType compatibility branch

1 participant