Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Nov 28, 2025

Summary

This is a bugfix for subtyping of type[Any] / type[T] and protocols.

Test Plan

Regression test that will only be really meaningful once #21553 lands.

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

astral-sh-bot bot commented Nov 28, 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 28, 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 500 diagnostics

xarray (https://github.com/pydata/xarray)
- xarray/core/common.py:1702:22: error[invalid-assignment] Object of type `(_SupportsDType[dtype[Any]] & Top[Mapping[Unknown, object]]) | (str & Top[Mapping[Unknown, object]]) | (tuple[Any, SupportsIndex | Sequence[SupportsIndex]] & Top[Mapping[Unknown, object]]) | ... omitted 3 union elements` is not assignable to `Mapping[Any, dtype[Any] | None | _SupportsDType[dtype[Any]] | ... omitted 4 union elements]`
+ xarray/core/common.py:1702:22: error[invalid-assignment] Object of type `(type[Any] & Top[Mapping[Unknown, object]]) | (str & Top[Mapping[Unknown, object]]) | (tuple[Any, SupportsIndex | Sequence[SupportsIndex]] & Top[Mapping[Unknown, object]]) | ... omitted 4 union elements` is not assignable to `Mapping[Any, dtype[Any] | None | type[Any] | ... omitted 5 union elements]`
- xarray/tests/test_dataarray.py:4157:18: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_dataarray.py:4163:13: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_dataset.py:6905:18: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_dataset.py:6921:18: error[no-matching-overload] No overload of function `full_like` matches arguments
+ xarray/tests/test_plot.py:232:64: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
+ xarray/tests/test_plot.py:1320:55: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- xarray/tests/test_variable.py:2837:34: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_variable.py:2844:13: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_variable.py:2863:13: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_variable.py:2883:26: error[no-matching-overload] No overload of function `zeros_like` matches arguments
- xarray/tests/test_variable.py:2883:55: error[no-matching-overload] No overload of function `full_like` matches arguments
- xarray/tests/test_variable.py:2890:26: error[no-matching-overload] No overload of function `ones_like` matches arguments
- xarray/tests/test_variable.py:2890:54: error[no-matching-overload] No overload of function `full_like` matches arguments
- Found 1786 diagnostics
+ Found 1777 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

jax (https://github.com/google/jax)
- jax/_src/cudnn/scaled_matmul_stablehlo.py:689:54: error[invalid-argument-type] Argument to function `scaled_dot_general_transpose_rhs` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `Unknown | list[Unknown]`
+ jax/_src/cudnn/scaled_matmul_stablehlo.py:689:54: error[invalid-argument-type] Argument to function `scaled_dot_general_transpose_rhs` is incorrect: Expected `str | type[Any] | dtype[Any] | SupportsDType`, found `Unknown | list[Unknown]`
- jax/_src/dtypes.py:637:45: error[invalid-assignment] Object of type `(SupportsDType & tuple[object, ...]) | tuple[str | SupportsDType | dtype[Any], ...]` is not assignable to `tuple[str | SupportsDType | dtype[Any], ...]`
+ jax/_src/dtypes.py:637:45: error[invalid-assignment] Object of type `(SupportsDType & tuple[object, ...]) | tuple[str | type[Any] | dtype[Any] | SupportsDType, ...]` is not assignable to `tuple[str | type[Any] | dtype[Any] | SupportsDType, ...]`
- jax/_src/lax/lax.py:5901:54: error[invalid-argument-type] Argument to function `dot_algorithm_attr` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `str | SupportsDType | dtype[Any] | None`
+ jax/_src/lax/lax.py:5901:54: error[invalid-argument-type] Argument to function `dot_algorithm_attr` is incorrect: Expected `str | type[Any] | dtype[Any] | SupportsDType`, found `str | type[Any] | dtype[Any] | SupportsDType | None`
- jax/_src/lax/lax.py:5901:65: error[invalid-argument-type] Argument to function `dot_algorithm_attr` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `str | SupportsDType | dtype[Any] | None`
+ jax/_src/lax/lax.py:5901:65: error[invalid-argument-type] Argument to function `dot_algorithm_attr` is incorrect: Expected `str | type[Any] | dtype[Any] | SupportsDType`, found `str | type[Any] | dtype[Any] | SupportsDType | None`
- jax/_src/lax/linalg.py:2707:19: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/_src/lax/linalg.py:2712:19: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/_src/lax/other.py:224:7: error[invalid-argument-type] Argument to function `conv_general_dilated_patches` is incorrect: Expected `Precision | None`, found `None | Precision | str | SupportsDType | dtype[Any]`
+ jax/_src/lax/other.py:224:7: error[invalid-argument-type] Argument to function `conv_general_dilated_patches` is incorrect: Expected `Precision | None`, found `None | Precision | str | ... omitted 3 union elements`
- jax/_src/numpy/array_methods.py:566:41: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/_src/numpy/lax_numpy.py:5489:37: error[invalid-parameter-default] Default value of type `<class 'float'>` is not assignable to annotated parameter type `str | SupportsDType | dtype[Any]`
- jax/_src/numpy/lax_numpy.py:5637:21: error[invalid-parameter-default] Default value of type `<class 'float'>` is not assignable to annotated parameter type `str | SupportsDType | dtype[Any]`
- jax/_src/numpy/lax_numpy.py:5723:29: error[invalid-parameter-default] Default value of type `<class 'float'>` is not assignable to annotated parameter type `str | SupportsDType | dtype[Any]`
- jax/_src/numpy/lax_numpy.py:8269:60: error[invalid-argument-type] Argument to function `argmax` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'int'>`
- jax/_src/numpy/lax_numpy.py:8330:60: error[invalid-argument-type] Argument to function `argmin` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'int'>`
- jax/_src/numpy/lax_numpy.py:9021:40: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/_src/numpy/lax_numpy.py:9351:81: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `type`
- jax/_src/numpy/reductions.py:2542:35: error[invalid-argument-type] Argument to function `broadcasted_iota` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'int'>`
- jax/_src/numpy/ufuncs.py:1204:40: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/_src/numpy/ufuncs.py:1248:40: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/_src/pallas/mosaic_gpu/lowering.py:1407:13: error[invalid-assignment] Object of type `str | SupportsDType | dtype[Any] | None` is not assignable to `dtype[Any]`
+ jax/_src/pallas/mosaic_gpu/lowering.py:1407:13: error[invalid-assignment] Object of type `str | type[Any] | dtype[Any] | SupportsDType | None` is not assignable to `dtype[Any]`
- jax/_src/pallas/mosaic_gpu/lowering.py:2054:60: error[invalid-argument-type] Argument to function `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/_src/random.py:816:47: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/_src/scipy/linalg.py:2194:28: error[invalid-argument-type] Argument to function `broadcasted_iota` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'float'>`
- jax/_src/tpu/linalg/eigh.py:621:32: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/_src/tpu/linalg/eigh.py:623:48: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/_src/tpu/linalg/eigh.py:627:37: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/_src/tpu/linalg/eigh.py:629:53: error[invalid-argument-type] Argument to function `_tri` is incorrect: Expected `str | SupportsDType | dtype[Any]`, found `<class 'bool'>`
- jax/experimental/jax2tf/tests/flax_models/seq2seq_lstm.py:83:41: error[invalid-argument-type] Argument to function `zeros` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/experimental/pallas/ops/gpu/attention.py:662:68: error[invalid-argument-type] Argument to function `ones` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'bool'>`
- jax/experimental/pallas/ops/gpu/transposed_ragged_dot_mgpu.py:59:21: error[invalid-argument-type] Argument to function `zeros` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/pallas/ops/gpu/transposed_ragged_dot_mgpu.py:60:12: error[invalid-argument-type] Argument to bound method `astype` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:582:94: error[invalid-argument-type] Argument to function `zeros` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:1122:53: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:1123:53: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:1124:85: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:1125:85: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:1758:34: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- jax/experimental/sparse/bcoo.py:2238:46: error[invalid-argument-type] Argument to function `array` is incorrect: Expected `str | SupportsDType | dtype[Any] | None`, found `<class 'int'>`
- Found 2733 diagnostics
+ Found 2702 diagnostics

No memory usage changes detected ✅

Comment on lines +47 to +49
# TODO: no errors here
# error: [invalid-type-arguments] "Type `typing.TypeVar` is not assignable to upper bound `generic[Any]` of type variable `_ScalarT_co@dtype`"
# error: [invalid-type-arguments] "Type `typing.TypeVar` is not assignable to upper bound `generic[Any]` of type variable `_ScalarT_co@dtype`"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These errors prevent us from running into the problem on main. But on #21553, these errors will go away, and then we need to properly understand all of this code here, or otherwise we see np.array calls failing all over the place.

@sharkdp sharkdp marked this pull request as ready for review November 28, 2025 14:29
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Some suggestions, but they're untested, so accept them with care 😄

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 28, 2025

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-argument-type 0 28 4
no-matching-overload 0 11 0
invalid-assignment 0 0 3
invalid-parameter-default 0 3 0
unused-ignore-comment 2 0 0
Total 2 42 7

Full report with detailed diff (timing results)

@sharkdp sharkdp force-pushed the david/numpy-workaround branch from 80bc86e to 2be7bbe Compare November 28, 2025 15:23
@sharkdp sharkdp changed the title [ty] Workaround for problem with numpy's dtype [ty] Fix subtyping of type[Any] / type[T] and protocols Nov 28, 2025
@sharkdp sharkdp merged commit 0084e94 into main Nov 28, 2025
42 checks passed
@sharkdp sharkdp deleted the david/numpy-workaround branch November 28, 2025 15:56
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