Skip to content

Conversation

@ibraheemdev
Copy link
Member

Summary

Extends #20927 to generic call inference, preferring the declared type of generic classes:

def f[T](x: T) -> list[T]:
    return [x]

x: list[Any] = f(1)
reveal_type(x)  # before: list[Literal[1]], after: list[Any]

Part of astral-sh/ty#136.

@ibraheemdev ibraheemdev added the ty Multi-file analysis & type inference label Oct 25, 2025
@ibraheemdev ibraheemdev force-pushed the ibraheem/declared-generic-type branch from cf8b71b to c563650 Compare October 25, 2025 02:52
@github-actions
Copy link
Contributor

github-actions bot commented Oct 25, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@ibraheemdev ibraheemdev force-pushed the ibraheem/declared-generic-type branch from c563650 to fdbb426 Compare October 25, 2025 02:57
@github-actions
Copy link
Contributor

github-actions bot commented Oct 25, 2025

mypy_primer results

Changes were detected when running on open source projects
dulwich (https://github.com/dulwich/dulwich)
- dulwich/pack.py:1703:83: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 172 diagnostics
+ Found 171 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- pydantic/_internal/_validators.py:161:16: error[invalid-return-type] Return type does not match returned value: expected `Pattern[bytes]`, found `Pattern[str | bytes]`
+ pydantic/_internal/_validators.py:161:16: error[invalid-return-type] Return type does not match returned value: expected `Pattern[bytes]`, found `Pattern[bytes | str]`

Expression (https://github.com/cognitedata/Expression)
- tests/test_result.py:491:21: error[invalid-argument-type] Argument to bound method `or_else` is incorrect: Expected `Result[Literal[42], Any]`, found `Result[Literal[0], Any]`
- tests/test_result.py:509:21: error[invalid-argument-type] Argument to bound method `or_else` is incorrect: Expected `Result[Any, Literal["original error"]]`, found `Result[Any, Literal["new error"]]`
- Found 201 diagnostics
+ Found 199 diagnostics

sphinx (https://github.com/sphinx-doc/sphinx)
- sphinx/domains/python/__init__.py:672:42: error[index-out-of-bounds] Index 0 is out of bounds for string `Literal[""]` with length 0
- Found 530 diagnostics
+ Found 529 diagnostics

jax (https://github.com/google/jax)
- jax/_src/interpreters/mlir.py:1484:5: error[invalid-assignment] Object of type `OrderedDict[str, Unknown]` is not assignable to attribute `_tokens` of type `OrderedDict[Effect, Unknown]`
- Found 2544 diagnostics
+ Found 2543 diagnostics

rotki (https://github.com/rotki/rotki)
+ rotkehlchen/exchanges/coinbase.py:524:20: error[invalid-assignment] Object of type `dict[Unknown, Unknown]` is not assignable to `defaultdict[str, list[dict[Unknown, Unknown]]]`
- Found 1684 diagnostics
+ Found 1685 diagnostics

core (https://github.com/home-assistant/core)
+ homeassistant/helpers/service.py:913:9: error[invalid-assignment] Object of type `object` is not assignable to `ServiceResponse`
- Found 14272 diagnostics
+ Found 14273 diagnostics
Memory usage changes were detected when running on open source projects
prefect (https://github.com/PrefectHQ/prefect)
-     struct metadata = ~33MB
+     struct metadata = ~35MB

@github-actions
Copy link
Contributor

github-actions bot commented Oct 25, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-assignment 2 1 0
invalid-argument-type 0 2 0
index-out-of-bounds 0 1 0
invalid-return-type 0 0 1
unused-ignore-comment 0 1 0
Total 2 5 1

Full report with detailed diff (timing results)

@ibraheemdev
Copy link
Member Author

ibraheemdev commented Oct 25, 2025

I'm not sure if we should restrict this change to invariant generics. It seems reasonable to use frozendict[str, Any] as an implicit typed dict, so preferring the Any there might still be meaningful (even thought it feels strictly worse).

@ibraheemdev ibraheemdev marked this pull request as draft October 25, 2025 03:49
@carljm
Copy link
Contributor

carljm commented Oct 30, 2025

@ibraheemdev Is this blocked? What's keeping it in draft?

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 30, 2025

CodSpeed Performance Report

Merging #21070 will not alter performance

Comparing ibraheem/declared-generic-type (2f4dcbf) with main (1d111c8)

Summary

✅ 22 untouched
⏩ 30 skipped1

Footnotes

  1. 30 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ibraheemdev ibraheemdev force-pushed the ibraheem/declared-generic-type branch from 2f4a925 to 0ee0e3f Compare October 30, 2025 21:43
@ibraheemdev ibraheemdev marked this pull request as ready for review October 30, 2025 21:44
ibraheemdev added a commit that referenced this pull request Oct 31, 2025
## Summary

We currently perform a subtyping check instead of the intended subclass
check (and the subtyping check is confusingly named `is_subclass_of`).
This showed up in #21070.
ibraheemdev added a commit that referenced this pull request Oct 31, 2025
…1165)

## Summary

The solver is currently order-dependent, and will choose a supertype
over the exact type if it appears earlier in the list of constraints. We
could be smarter and try to choose the most precise subtype, but I
imagine this is something the new constraint solver will fix anyways,
and this fixes the issue showing up on
#21070.
@ibraheemdev
Copy link
Member Author

The ecosystem changes all seem correct.

@ibraheemdev
Copy link
Member Author

Superceded by #21210.

@ibraheemdev ibraheemdev closed this Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants