Skip to content

Conversation

@ibraheemdev
Copy link
Member

Summary

We currently fail to account for the type context when inferring generic classes constructed with __new__, or synthesized __init__ for dataclasses.

@ibraheemdev ibraheemdev added the ty Multi-file analysis & type inference label Nov 14, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

mypy_primer results

Changes were detected when running on open source projects
bidict (https://github.com/jab/bidict)
- bidict/_base.py:200:9: error[invalid-assignment] Object of type `ReferenceType[Self@inverse]` is not assignable to attribute `_invweak` of type `ReferenceType[BidictBase[KT@BidictBase, VT@BidictBase]] | None`
- Found 15 diagnostics
+ Found 14 diagnostics

pyinstrument (https://github.com/joerick/pyinstrument)
- pyinstrument/stack_sampler.py:49:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[object]`
- Found 42 diagnostics
+ Found 41 diagnostics

dedupe (https://github.com/dedupeio/dedupe)
- dedupe/predicates.py:116:20: error[invalid-return-type] Return type does not match returned value: expected `frozenset[Literal["0", "1"]]`, found `frozenset[str]`
- dedupe/predicates.py:118:20: error[invalid-return-type] Return type does not match returned value: expected `frozenset[Literal["0", "1"]]`, found `frozenset[str]`
- Found 52 diagnostics
+ Found 50 diagnostics

schemathesis (https://github.com/schemathesis/schemathesis)
- src/schemathesis/specs/openapi/schemas.py:430:9: error[invalid-assignment] Object of type `APIOperation[Unknown, OpenApiResponses, OpenApiSecurityParameters]` is not assignable to `APIOperation[OperationParameter, ResponsesContainer[Unknown], OpenApiSecurityParameters]`
- Found 268 diagnostics
+ Found 267 diagnostics

antidote (https://github.com/Finistere/antidote)
- src/antidote/core/_catalog.py:214:65: error[invalid-argument-type] Argument is incorrect: Expected `ReferenceType[CatalogImpl]`, found `ReferenceType[Self@__init__]`
- Found 315 diagnostics
+ Found 314 diagnostics

freqtrade (https://github.com/freqtrade/freqtrade)
- freqtrade/persistence/models.py:29:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[str | None]`
- Found 615 diagnostics
+ Found 614 diagnostics

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
- pymongo/_csot.py:30:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[int | float | None]`
- pymongo/_csot.py:31:1: error[invalid-assignment] Object of type `ContextVar[float]` is not assignable to `ContextVar[int | float]`
- pymongo/_csot.py:32:1: error[invalid-assignment] Object of type `ContextVar[float]` is not assignable to `ContextVar[int | float]`
- Found 475 diagnostics
+ Found 472 diagnostics

trio (https://github.com/python-trio/trio)
- src/trio/_core/_run.py:1452:21: error[invalid-assignment] Object of type `_TaskStatus[None]` is not assignable to `_TaskStatus[object]`
- Found 524 diagnostics
+ Found 523 diagnostics

meson (https://github.com/mesonbuild/meson)
- mesonbuild/cmake/executor.py:29:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to `PerMachine[ExternalProgram | None]`
- mesonbuild/cmake/executor.py:30:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to `PerMachine[str | None]`
+ mesonbuild/cmake/interpreter.py:1004:70: error[invalid-argument-type] Argument is incorrect: Expected `int`, found `str | int | Path | BaseNode`
- mesonbuild/dependencies/cmake.py:35:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to `PerMachine[CMakeInfo | None]`
- mesonbuild/dependencies/pkgconfig.py:36:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to `PerMachine[ExternalProgram | None]`
- mesonbuild/modules/rust.py:90:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to `PerMachine[ExternalProgram | None]`
+ mesonbuild/mparser.py:702:65: error[invalid-argument-type] Argument is incorrect: Expected `int`, found `None`
- mesonbuild/utils/universal.py:589:16: error[invalid-return-type] Return type does not match returned value: expected `PerMachine[_T@PerMachineDefaultable]`, found `PerMachine[_T@PerMachineDefaultable & ~None]`
- mesonbuild/utils/universal.py:623:16: error[invalid-return-type] Return type does not match returned value: expected `PerThreeMachine[_T@PerThreeMachineDefaultable]`, found `PerThreeMachine[_T@PerThreeMachineDefaultable & ~None]`
- run_project_tests.py:557:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to attribute `class_cmakeinfo` of type `PerMachine[CMakeInfo | None]`
- run_project_tests.py:560:5: error[invalid-assignment] Object of type `PerMachine[None]` is not assignable to attribute `pkg_bin_per_machine` of type `PerMachine[ExternalProgram | None]`
- Found 1734 diagnostics
+ Found 1727 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/context.py:762:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[UUID | None]`
- src/prefect/context.py:763:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[dict[str, Any] | None]`
- src/prefect/server/database/configurations.py:40:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[str | None]`
- Found 3237 diagnostics
+ Found 3234 diagnostics

setuptools (https://github.com/pypa/setuptools)
- setuptools/_vendor/typing_extensions.py:3546:30: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `Iterable[Unknown]`, found `object`
+ setuptools/_vendor/typing_extensions.py:3546:30: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `Iterable[str]`, found `object`

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:98:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 44 diagnostics
+ Found 43 diagnostics

openlibrary (https://github.com/internetarchive/openlibrary)
+ openlibrary/plugins/upstream/mybooks.py:468:13: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `SupportsKeysAndGetItem[Literal["want-to-read", "already-read", "currently-reading"], Literal["Want to Read", "Already Read", "Currently Reading"]]`, found `dict[Unknown | str, Unknown | str]`
- Found 942 diagnostics
+ Found 943 diagnostics

altair (https://github.com/vega/altair)
- altair/utils/core.py:219:1: error[invalid-assignment] Object of type `frozenset[str]` is not assignable to `frozenset[Literal["field", "aggregate", "type", "timeUnit"]]`
- Found 991 diagnostics
+ Found 990 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
- ddtrace/_trace/provider.py:17:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[Span | Context | None]`
- ddtrace/appsec/_iast/_iast_request_context_base.py:22:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[int | None]`
- ddtrace/internal/ci_visibility/context.py:12:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[Context | Span | None]`
- ddtrace/llmobs/_context.py:14:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[Context | Span | None]`
- Found 7978 diagnostics
+ Found 7974 diagnostics

bokeh (https://github.com/bokeh/bokeh)
- src/bokeh/plotting/contour.py:249:9: error[invalid-argument-type] Argument is incorrect: Expected `GlyphRenderer[Glyph]`, found `GlyphRenderer[MultiPolygons]`
- src/bokeh/plotting/contour.py:250:9: error[invalid-argument-type] Argument is incorrect: Expected `GlyphRenderer[Glyph]`, found `GlyphRenderer[MultiLine]`
- src/bokeh/server/contexts.py:233:13: error[invalid-assignment] Object of type `ReferenceType[BokehSessionContext]` is not assignable to attribute `_session_context` of type `ReferenceType[SessionContext] | None`
- Found 637 diagnostics
+ Found 634 diagnostics

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- tests/test_natype.py:80:11: error[type-assertion-failure] Argument does not have asserted type `Index[int]`
- tests/test_natype.py:160:11: error[type-assertion-failure] Argument does not have asserted type `Index[int]`
- tests/test_pandas.py:1036:11: error[type-assertion-failure] Argument does not have asserted type `Series[Any]`
+ tests/test_pandas.py:1037:11: error[type-assertion-failure] Argument does not have asserted type `@Todo`
- tests/test_pandas.py:1040:11: error[type-assertion-failure] Argument does not have asserted type `Series[Any]`
+ tests/test_pandas.py:1041:11: error[type-assertion-failure] Argument does not have asserted type `@Todo`
- Found 5968 diagnostics
+ Found 5966 diagnostics

zulip (https://github.com/zulip/zulip)
- zerver/lib/typed_endpoint.py:271:12: error[invalid-return-type] Return type does not match returned value: expected `FuncParam[T@parse_single_parameter]`, found `FuncParam[Any & ~<class '_empty'>]`
- Found 3283 diagnostics
+ Found 3282 diagnostics

pydantic (https://github.com/pydantic/pydantic)
- pydantic/_internal/_generics.py:96:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[WeakValueDictionary[tuple[Any, Any, tuple[Any, ...]], type[BaseModel]] | None]`
- pydantic/_internal/_generics.py:395:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[set[str] | None]`
- Found 4816 diagnostics
+ Found 4814 diagnostics

core (https://github.com/home-assistant/core)
- homeassistant/components/conversation/chat_log.py:29:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[ChatLog | None]`
- homeassistant/components/conversation/trace.py:78:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[ConversationTrace | None]`
- homeassistant/config_entries.py:1344:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[ConfigEntry[Any] | None]`
- homeassistant/core.py:1683:9: error[invalid-assignment] Object of type `_OneTimeListener[Mapping[str, Any]]` is not assignable to `_OneTimeListener[_DataT@async_listen_once]`
- homeassistant/helpers/chat_session.py:33:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[ChatSession | None]`
- homeassistant/helpers/entity_platform.py:1248:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[EntityPlatform | None]`
- homeassistant/helpers/script.py:159:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[list[str] | None]`
- homeassistant/helpers/template/context.py:13:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[tuple[str, str] | None]`
- homeassistant/helpers/template/render_info.py:22:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[RenderInfo | None]`
- homeassistant/helpers/trace.py:102:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[dict[str, deque[TraceElement]] | None]`
- homeassistant/helpers/trace.py:106:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[list[TraceElement] | None]`
- homeassistant/helpers/trace.py:110:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[list[str] | None]`
- homeassistant/helpers/trace.py:116:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[tuple[str, str] | None]`
- homeassistant/helpers/trace.py:120:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[StopReason | None]`
- homeassistant/setup.py:38:1: error[invalid-assignment] Object of type `ContextVar[None]` is not assignable to `ContextVar[tuple[str, str | None] | None]`
- Found 14489 diagnostics
+ Found 14474 diagnostics

No memory usage changes detected ✅

@ibraheemdev ibraheemdev force-pushed the ibraheem/generic-constructor-inference branch from 8934296 to b1a9cb6 Compare November 14, 2025 02:14
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-assignment 0 41 0
invalid-argument-type 3 3 1
type-assertion-failure 2 4 0
invalid-return-type 0 5 0
Total 5 53 1

Full report with detailed diff (timing results)

@ibraheemdev ibraheemdev changed the title [ty] Improve generic constructor inference [ty] Improve generic class constructor inference Nov 14, 2025
Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

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

This is great, thank you!

@ibraheemdev ibraheemdev force-pushed the ibraheem/generic-constructor-inference branch from b1a9cb6 to 816631a Compare November 14, 2025 20:21
@ibraheemdev ibraheemdev enabled auto-merge (squash) November 14, 2025 20:25
@ibraheemdev ibraheemdev merged commit 2a2b719 into main Nov 14, 2025
40 checks passed
@ibraheemdev ibraheemdev deleted the ibraheem/generic-constructor-inference branch November 14, 2025 20:25
dcreager added a commit that referenced this pull request Nov 14, 2025
* origin/main: (59 commits)
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  [ty] Support legacy `typing` special forms in implicit type aliases (#21433)
  Bump 0.14.5 (#21435)
  [ty] Support `type[…]` and `Type[…]` in implicit type aliases (#21421)
  [ty] Respect notebook cell boundaries when adding an auto import (#21322)
  Update PyCharm setup instructions (#21409)
  ...
dcreager added a commit that referenced this pull request Nov 14, 2025
* dcreager/deep-comparison: (64 commits)
  assuming
  SubtypingAssuming
  implies_subtype_of
  name tweak
  Apply suggestions from code review
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  ...
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