Skip to content

Comments

[ty] Fix subtyping with type[T] and unions#21740

Merged
ibraheemdev merged 1 commit intomainfrom
ibraheem/type-of-subtyping
Dec 1, 2025
Merged

[ty] Fix subtyping with type[T] and unions#21740
ibraheemdev merged 1 commit intomainfrom
ibraheem/type-of-subtyping

Conversation

@ibraheemdev
Copy link
Member

Summary

Resolves #21685 (comment).

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

astral-sh-bot bot commented Dec 1, 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 Dec 1, 2025

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
- beartype/claw/_package/clawpkgtrie.py:66:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieBlacklist]'> | <class 'dict[str, Divergent]'>`
- beartype/claw/_package/clawpkgtrie.py:247:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieWhitelist]'> | <class 'dict[str, Divergent]'>`
- Found 498 diagnostics
+ Found 496 diagnostics

spack (https://github.com/spack/spack)
- lib/spack/spack/vendor/jsonschema/exceptions.py:240:26: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 7952 diagnostics
+ Found 7951 diagnostics

trio (https://github.com/python-trio/trio)
- src/trio/testing/_raises_group.py:811:32: error[not-iterable] Object of type `enumerate[type[BaseExcT_co@RaisesGroup] | Matcher[BaseExcT_co@RaisesGroup] | RaisesGroup[BaseException]]` is not iterable
- src/trio/testing/_raises_group.py:834:32: error[not-iterable] Object of type `enumerate[type[BaseExcT_co@RaisesGroup] | Matcher[BaseExcT_co@RaisesGroup] | RaisesGroup[BaseException]]` is not iterable
- src/trio/testing/_raises_group.py:895:36: error[not-iterable] Object of type `enumerate[type[BaseExcT_co@RaisesGroup] | Matcher[BaseExcT_co@RaisesGroup] | RaisesGroup[BaseException]]` is not iterable
- Found 493 diagnostics
+ Found 490 diagnostics

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/flow_runs.py:291:19: error[unresolved-attribute] Object of type `type[T@_in_process_pause]` has no attribute `load`
+ src/prefect/flow_runs.py:291:19: error[unresolved-attribute] Object of type `(type[T@_in_process_pause] & ~AlwaysTruthy & ~AlwaysFalsy) | (type[T@_in_process_pause] & ~AlwaysFalsy)` has no attribute `load`
- src/prefect/flow_runs.py:306:15: error[unresolved-attribute] Object of type `type[T@_in_process_pause]` has no attribute `save`
+ src/prefect/flow_runs.py:306:15: error[unresolved-attribute] Object of type `(type[T@_in_process_pause] & ~AlwaysTruthy & ~AlwaysFalsy) | (type[T@_in_process_pause] & ~AlwaysFalsy)` has no attribute `save`
- src/prefect/flow_runs.py:318:34: error[unresolved-attribute] Object of type `type[T@_in_process_pause]` has no attribute `load`
+ src/prefect/flow_runs.py:318:34: error[unresolved-attribute] Object of type `(type[T@_in_process_pause] & ~AlwaysTruthy & ~AlwaysFalsy) | (type[T@_in_process_pause] & ~AlwaysFalsy)` has no attribute `load`
- src/prefect/flow_runs.py:445:26: error[unresolved-attribute] Object of type `type[T@suspend_flow_run]` has no attribute `load`
+ src/prefect/flow_runs.py:445:26: error[unresolved-attribute] Object of type `(type[T@suspend_flow_run] & ~AlwaysTruthy & ~AlwaysFalsy) | (type[T@suspend_flow_run] & ~AlwaysFalsy)` has no attribute `load`
- src/prefect/flow_runs.py:455:15: error[unresolved-attribute] Object of type `type[T@suspend_flow_run]` has no attribute `save`
+ src/prefect/flow_runs.py:455:15: error[unresolved-attribute] Object of type `(type[T@suspend_flow_run] & ~AlwaysTruthy & ~AlwaysFalsy) | (type[T@suspend_flow_run] & ~AlwaysFalsy)` has no attribute `save`
- src/prefect/utilities/collections.py:197:40: error[invalid-argument-type] Argument to function `ensure_iterable` is incorrect: Expected `tuple[type[T@extract_instances], ...] | Iterable[tuple[type[T@extract_instances], ...]]`, found `type[T@extract_instances] | tuple[type[T@extract_instances], ...]`
- src/prefect/utilities/collections.py:210:16: error[invalid-argument-type] Method `__getitem__` of type `bound method dict[type[T@extract_instances], list[Any]].__getitem__(key: type[T@extract_instances], /) -> list[Any]` cannot be called with key of type `tuple[type[T@extract_instances], ...]` on object of type `dict[type[T@extract_instances], list[Any]]`
+ src/prefect/utilities/collections.py:210:16: error[invalid-argument-type] Method `__getitem__` of type `bound method dict[type[T@extract_instances], list[Any]].__getitem__(key: type[T@extract_instances], /) -> list[Any]` cannot be called with key of type `Unknown` on object of type `dict[type[T@extract_instances], list[Any]]`
- src/prefect/utilities/pydantic.py:85:9: error[invalid-assignment] Implicit shadowing of function `__reduce__`
+ src/prefect/utilities/pydantic.py:85:9: error[invalid-assignment] Object of type `def _reduce_model(self: BaseModel) -> tuple[Any, ...]` is not assignable to attribute `__reduce__` on type `type[M@add_cloudpickle_reduction] & ~AlwaysFalsy`
- Found 3400 diagnostics
+ Found 3399 diagnostics

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ src/scikit_build_core/_logging.py:153:13: warning[unsupported-base] Unsupported class base with type `<class 'Mapping[str, Style]'> | <class 'Mapping[str, Divergent]'>`
- Found 41 diagnostics
+ Found 42 diagnostics

ibis (https://github.com/ibis-project/ibis)
- ibis/backends/__init__.py:1019:9: error[invalid-argument-type] Argument to bound method `extend` is incorrect: Argument type `list[Unknown | type[Self@db_identity]]` does not satisfy upper bound `list[_T@list]` of type variable `Self`
- ibis/backends/__init__.py:1020:9: error[invalid-argument-type] Argument to bound method `extend` is incorrect: Argument type `list[Unknown | type[Self@db_identity]]` does not satisfy upper bound `list[_T@list]` of type variable `Self`
- Found 4489 diagnostics
+ Found 4487 diagnostics

hydpy (https://github.com/hydpy-dev/hydpy)
- hydpy/core/exceptiontools.py:202:68: error[not-iterable] Object of type `(Unknown & ~AlwaysFalsy) | (tuple[type[T@getattr_], ...] & ~AlwaysFalsy) | (tuple[type[@Todo], ...] & ~AlwaysFalsy)` may not be iterable
- hydpy/core/variabletools.py:2345:18: error[not-iterable] Object of type `Iterable[type[TypeVariable@sort_variables] | tuple[type[TypeVariable@sort_variables], T@sort_variables]]` is not iterable
- Found 654 diagnostics
+ Found 652 diagnostics

sympy (https://github.com/sympy/sympy)
- sympy/core/basic.py:670:76: error[not-iterable] Object of type `tuple[Tbasic@atoms | type[Tbasic@atoms], ...] & ~AlwaysFalsy` is not iterable
+ sympy/matrices/matrixbase.py:1592:20: error[invalid-return-type] Return type does not match returned value: expected `set[Basic] | set[Tbasic@atoms]`, found `set[type[Tbasic@atoms]]`

pydantic (https://github.com/pydantic/pydantic)
- pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:943:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:983:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:1026:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:1066:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:1109:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:1148:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`
+ pydantic/fields.py:1188:5: error[invalid-parameter-default] Default value of type `PydanticUndefinedType` is not assignable to annotated parameter type `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`
- pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, Divergent] | ((dict[str, Divergent], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, Divergent], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`
+ pydantic/fields.py:1567:13: error[invalid-argument-type] Argument is incorrect: Expected `dict[str, int | float | str | ... omitted 3 union elements] | ((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) | None`, found `Top[dict[Unknown, Unknown]] | (((dict[str, int | float | str | ... omitted 3 union elements], /) -> None) & ~Top[dict[Unknown, Unknown]]) | None`

No memory usage changes detected ✅

@ibraheemdev ibraheemdev merged commit ec854c7 into main Dec 1, 2025
42 checks passed
@ibraheemdev ibraheemdev deleted the ibraheem/type-of-subtyping branch December 1, 2025 23:20
dcreager added a commit that referenced this pull request Dec 2, 2025
* origin/main: (67 commits)
  Move `Token`, `TokenKind` and `Tokens` to `ruff-python-ast` (#21760)
  [ty] Don't confuse multiple occurrences of `typing.Self` when binding bound methods (#21754)
  Use our org-wide Renovate preset (#21759)
  Delete `my-script.py` (#21751)
  [ty] Move `all_members`, and related types/routines, out of `ide_support.rs` (#21695)
  [ty] Fix find-references for import aliases (#21736)
  [ty] add tests for workspaces (#21741)
  [ty] Stop testing the (brittle) constraint set display implementation (#21743)
  [ty] Use generator over list comprehension to avoid cast (#21748)
  [ty] Add a diagnostic for prohibited `NamedTuple` attribute overrides (#21717)
  [ty] Fix subtyping with `type[T]` and unions (#21740)
  Use `npm ci --ignore-scripts` everywhere (#21742)
  [`flake8-simplify`] Fix truthiness assumption for non-iterable arguments in tuple/list/set calls (`SIM222`, `SIM223`) (#21479)
  [`flake8-use-pathlib`] Mark fixes unsafe for return type changes (`PTH104`, `PTH105`, `PTH109`, `PTH115`) (#21440)
  [ty] Fix auto-import code action to handle pre-existing import
  Enable PEP 740 attestations when publishing to PyPI (#21735)
  [ty] Fix find references for type defined in stub (#21732)
  Use OIDC instead of codspeed token (#21719)
  [ty] Exclude `typing_extensions` from completions unless it's really available
  [ty] Fix false positives for `class F(Generic[*Ts]): ...` (#21723)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants