Skip to content

[ty] Specialize bare generic class name with type[...] type context - #29248

Merged
sharkdp merged 1 commit into
mainfrom
ibraheem/bare-class-tcx
Oct 12, 2026
Merged

sharkdp merged 1 commit into
mainfrom
ibraheem/bare-class-tcx

Conversation

@ibraheemdev

Copy link
Copy Markdown
Member

A type[...] annotation should inform the specialization of a bare generic class:

class X[T = int]: ...

x: type[X[str]] = X
reveal_type(x())  # revealed: X[str]

Resolves astral-sh/ty#1624.

@ibraheemdev
ibraheemdev requested a review from a team as a code owner October 9, 2026 06:05
@ibraheemdev ibraheemdev added the ty The ty type checker label Oct 9, 2026
@astral-sh-bot
astral-sh-bot Bot requested a review from sharkdp October 9, 2026 06:05
@astral-sh-bot

astral-sh-bot Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Typing conformance results regressed ❌

The percentage of diagnostics emitted that were expected errors decreased from 98.24% to 98.24%. The percentage of expected errors that received a diagnostic decreased from 98.24% to 98.24%. The number of fully passing files held steady at 134/146.

Summary

How are test cases classified?

Each test case represents one expected error annotation or a group of annotations sharing a tag. Counts are per test case, not per diagnostic — multiple diagnostics on the same line count as one. Required annotations (E) are true positives when ty flags the expected location and false negatives when it does not. Optional annotations (E?) are true positives when flagged but true negatives (not false negatives) when not. Tagged annotations (E[tag]) require ty to flag exactly one of the tagged lines; tagged multi-annotations (E[tag+]) allow any number up to the tag count. Flagging unexpected locations counts as a false positive.

Metric Old New Diff Outcome
True Positives 1117 1116 -1 ⏬ (❌)
False Positives 20 20 +0
False Negatives 20 20 +0
Total Diagnostics 1181 1180 -1 ⏬
Precision 98.24% 98.24% -0.00% ⏬ (❌)
Recall 98.24% 98.24% -0.00% ⏬ (❌)
Passing Files 134/146 134/146 +0

Test file breakdown

1 file altered
File True Positives False Positives False Negatives Status
generics_defaults_specialization.py 2 (-1) ❌ 0 0 ✅ Still Passing
Total (all files) 1116 (-1) ❌ 20 20 134/146

Optional Diagnostics Removed (1)

1 diagnostic
Test case Diff

generics_defaults_specialization.py:48

-error[invalid-assignment] Object of type `<class 'Bar'>` is not assignable to `type[Bar[int]]`


# OK, the default specialization of `P` is assignable to `type[P[Unknown]]`
expects_type_p(P)
def _(bare: type[P]): # error: [missing-type-argument]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were restructured so that the bare class name is default specialized instead of being affected by type context, to preserve the original intent of the tests. There are no semantic changes in this file.

@ibraheemdev
ibraheemdev force-pushed the ibraheem/bare-class-tcx branch from cc15d79 to 02f069b Compare October 9, 2026 06:08
@astral-sh-bot

astral-sh-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

Lint rule Added Removed Changed
unsound-assignment 0 6 0
invalid-assignment 0 4 0
Total 0 10 0

Large timing changes:

Project Old Time New Time Change
pyppeteer 0.07s 0.18s +161%
schema_salad 0.20s 0.45s +121%

Raw diff:

beartype (https://github.com/beartype/beartype)
- beartype/_check/code/codescope.py:398:33 warning[unsound-assignment] Unsound assignment: `list[Unknown]` is not a subtype of `list[type]`
- beartype/_check/code/codescope.py:402:36 warning[unsound-assignment] Unsound assignment: `list[Unknown]` is not a subtype of `list[type]`
- beartype/_check/code/_pep/pep484/codepep484604union.py:458:48 warning[unsound-assignment] Unsound assignment: `list[Unknown]` is not a subtype of `list[HintSane]`

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/input/run_input.py:482:49 warning[unsound-assignment] Unsound assignment: `type[AutomaticRunInput[Unknown]]` is not a subtype of `type[AutomaticRunInput[T@AutomaticRunInput]]`
- src/prefect/server/utilities/database.py:405:34 warning[unsound-assignment] Unsound assignment: `<class 'REAL'>` is not a subtype of `type[REAL[float]]`

psycopg (https://github.com/psycopg/psycopg)
- psycopg/psycopg/connection.py:78:9 error[invalid-assignment] Object of type `<class 'Cursor'>` is not assignable to attribute `cursor_factory` of type `type[Cursor[Row@Connection]]`
- psycopg/psycopg/connection.py:79:9 error[invalid-assignment] Object of type `<class 'ServerCursor'>` is not assignable to attribute `server_cursor_factory` of type `type[ServerCursor[Row@Connection]]`
- psycopg/psycopg/connection_async.py:82:9 error[invalid-assignment] Object of type `<class 'AsyncCursor'>` is not assignable to attribute `cursor_factory` of type `type[AsyncCursor[Row@AsyncConnection]]`
- psycopg/psycopg/connection_async.py:83:9 error[invalid-assignment] Object of type `<class 'AsyncServerCursor'>` is not assignable to attribute `server_cursor_factory` of type `type[AsyncServerCursor[Row@AsyncConnection]]`

sympy (https://github.com/sympy/sympy)
- sympy/polys/domains/algebraicfield.py:265:24 warning[unsound-assignment] Unsound assignment: `<class 'ANP'>` is not a subtype of `type[ANP[MPQ]]`

Full report with detailed diff (timing results)

@ibraheemdev

ibraheemdev commented Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

Hmm.. it looks like the conformance suite expects the motivating assignment to be disallowed? Pyright seems to have also changed its behavior since astral-sh/ty#1624 was filed.

@carljm

carljm commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

The conformance suite issue is resolved by python/typing#2366.

Pyright's behavior change appears to have been motivated entirely by the conformance suite, not by a real-world behavior preference: microsoft/pyright#11596

@carljm

carljm commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

We'll also need to land #29269 and rebase this on it before the conformance CI will clear up.

@sharkdp
sharkdp force-pushed the ibraheem/bare-class-tcx branch from a41d831 to 70281a9 Compare October 12, 2026 10:09

@sharkdp sharkdp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@sharkdp

sharkdp commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

I rebased the PR on main. The conformance tests still say that "Typing conformance results regressed", but the optional E? assertion means that this is not actually considered a regression in score anymore: "The number of fully passing files held steady at 134/146.".

@AlexWaygood

Copy link
Copy Markdown
Member

We need to fix the script so that we no longer get a big angry ❌ if an optional diagnostic is removed...

@sharkdp
sharkdp merged commit 691b38c into main Oct 12, 2026
75 checks passed
@sharkdp
sharkdp deleted the ibraheem/bare-class-tcx branch October 12, 2026 10:30

This branch was previously deployed

1 inactive deployment
automations — 70281a9b Deployed Oct 12, 2026 by sharkdp via security-review / security review #91378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

use type context to inform default specialization of bare generic class name

4 participants