Skip to content

Constrain memoryview type var to allowed types - #13683

Merged
srittau merged 7 commits into
python:mainfrom
srittau:generic-memoryview
Nov 30, 2026
Merged

srittau merged 7 commits into
python:mainfrom
srittau:generic-memoryview

Conversation

@srittau

@srittau srittau commented Mar 20, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@github-actions

This comment has been minimized.

@JelleZijlstra

Copy link
Copy Markdown
Member

I haven't fully thought this through but #13519 partially made me think that making memoryview generic with a default was a mistake. memoryview now means memoryview[int], so if you have a memoryview[bytes] you can't pass it to a function that wants a plain memoryview. But usually that's wrong; lots of memoryview functionality doesn't depend on the type parameter and lots of functions will work with any kind of memoryview.

@srittau

srittau commented Mar 20, 2025

Copy link
Copy Markdown
Collaborator Author

I was thinking about that, but I think changing the default to Any might be a better solution. That keeps the flexibility of restricting memoryviews while by default any memoryview is accepted.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

cvxpy (https://github.com/cvxpy/cvxpy)
- cvxpy/interface/matrix_utilities.py:190: error: Incompatible return value type (got "Number | generic[Any] | int | float | complex | str | bytes | memoryview[int]", expected "Number | generic[Any]")  [return-value]
+ cvxpy/interface/matrix_utilities.py:190: error: Incompatible return value type (got "Number | generic[Any] | int | float | complex | str | bytes | memoryview[Any]", expected "Number | generic[Any]")  [return-value]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1159: error: No overload variant of "builds" of "BuildsFn" matches argument types "type[DataclassInstance]", "dict[str, int | float | Path | DataClass_ | type[DataClass_] | ListConfig | DictConfig | Enum | Sequence[HydraSupportedType] | Mapping[Any, HydraSupportedType] | None]", "bool | None", "Literal['none', 'partial', 'all', 'object'] | None", "DataclassOptions"  [call-overload]
+ src/hydra_zen/structured_configs/_implementations.py:1159: error: No overload variant of "builds" of "BuildsFn" matches argument types "type[DataclassInstance]", "dict[str, int | float | memoryview[Any] | Path | DataClass_ | <6 more items> | None]", "bool | None", "Literal['none', 'partial', 'all', 'object'] | None", "DataclassOptions"  [call-overload]
- src/hydra_zen/structured_configs/_implementations.py:2724: error: Incompatible types in assignment (expression has type "tuple[int | float | Path | DataClass_ | type[DataClass_] | ListConfig | DictConfig | Enum | Sequence[HydraSupportedType] | Mapping[Any, HydraSupportedType] | None, ...]", variable has type "list[Importable | Callable[P, R] | type[Builds[Importable]] | type[BuildsWithSig[Importable, Any]] | type[BuildsWithSig[type[R], P]] | Any]")  [assignment]
+ src/hydra_zen/structured_configs/_implementations.py:2724: error: Incompatible types in assignment (expression has type "tuple[int | float | memoryview[Any] | Path | DataClass_ | <6 more items> | None, ...]", variable has type "list[Importable | Callable[P, R] | type[Builds[Importable]] | type[BuildsWithSig[Importable, Any]] | type[BuildsWithSig[type[R], P]] | Any]")  [assignment]

trio (https://github.com/python-trio/trio)
+ src/trio/_abc.py:339: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_unix_pipes.py:130: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_highlevel_socket.py:109: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_highlevel_generic.py:94: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_file_io.py:164: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_file_io.py:330: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_ssl.py:744: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:465: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:1154: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:1163: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_memory_streams.py:44: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_memory_streams.py:122: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_memory_streams.py:270: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_memory_streams.py:499: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_memory_streams.py:568: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_check_streams.py:101: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/testing/_check_streams.py:469: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_tests/test_ssl.py:810: error: Explicit "Any" is not allowed  [explicit-any]

pandera (https://github.com/pandera-dev/pandera)
- pandera/backends/xarray/error_formatters.py:35: error: Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has incompatible type "generic[Any] | int | float | complex | str | bytes | memoryview[int] | Number"; expected "complex | str | bytes | generic[Any]"  [arg-type]
+ pandera/backends/xarray/error_formatters.py:35: error: Argument 1 to "__call__" of "_UFunc_Nin1_Nout1" has incompatible type "generic[Any] | int | float | complex | str | bytes | memoryview[Any] | Number"; expected "complex | str | bytes | generic[Any]"  [arg-type]

steam.py (https://github.com/Gobot1234/steam.py)
- steam/app.py:251: error: Argument 1 to "verify_signature" has incompatible type "memoryview[int]"; expected "bytes"  [arg-type]
+ steam/app.py:251: error: Argument 1 to "verify_signature" has incompatible type "memoryview[Any]"; expected "bytes"  [arg-type]

aioredis (https://github.com/aio-libs/aioredis)
- aioredis/connection.py:933: error: Unsupported right operand type for in ("bytes | memoryview[int] | int")  [operator]
+ aioredis/connection.py:933: error: Unsupported right operand type for in ("bytes | memoryview[Any] | int")  [operator]
- aioredis/connection.py:934: error: Item "memoryview[int]" of "bytes | memoryview[int] | int" has no attribute "split"  [union-attr]
+ aioredis/connection.py:934: error: Item "memoryview[Any]" of "bytes | memoryview[Any] | int" has no attribute "split"  [union-attr]
- aioredis/connection.py:934: error: Item "int" of "bytes | memoryview[int] | int" has no attribute "split"  [union-attr]
+ aioredis/connection.py:934: error: Item "int" of "bytes | memoryview[Any] | int" has no attribute "split"  [union-attr]
- aioredis/connection.py:952: error: Argument 1 to "append" of "list" has incompatible type "bytes | memoryview[int]"; expected "bytes"  [arg-type]
+ aioredis/connection.py:952: error: Argument 1 to "append" of "list" has incompatible type "bytes | memoryview[Any]"; expected "bytes"  [arg-type]
- aioredis/client.py:4114: error: Incompatible types in assignment (expression has type "dict[bytes | str | memoryview[int], Any | None]", variable has type "dict[bytes | str | memoryview[int], Callable[[dict[str, str]], Awaitable[None]]]")  [assignment]
+ aioredis/client.py:4114: error: Incompatible types in assignment (expression has type "dict[bytes | str | memoryview[Any], Any | None]", variable has type "dict[bytes | str | memoryview[Any], Callable[[dict[str, str]], Awaitable[None]]]")  [assignment]
- aioredis/client.py:4158: error: Argument 1 to "update" of "MutableMapping" has incompatible type "dict[bytes | str | memoryview[int], Any | None]"; expected "SupportsKeysAndGetItem[bytes | str | memoryview[int], Callable[[dict[str, str]], Awaitable[None]]]"  [arg-type]
+ aioredis/client.py:4158: error: Argument 1 to "update" of "MutableMapping" has incompatible type "dict[bytes | str | memoryview[Any], Any | None]"; expected "SupportsKeysAndGetItem[bytes | str | memoryview[Any], Callable[[dict[str, str]], Awaitable[None]]]"  [arg-type]
- aioredis/client.py:4172: error: Incompatible types in assignment (expression has type "dict[bytes | str | memoryview[int], Callable[[dict[str, str]], Awaitable[None]]]", variable has type "dict[Any, Any | None]")  [assignment]
+ aioredis/client.py:4172: error: Incompatible types in assignment (expression has type "dict[bytes | str | memoryview[Any], Callable[[dict[str, str]], Awaitable[None]]]", variable has type "dict[Any, Any | None]")  [assignment]

@srittau
srittau merged commit b58c757 into python:main Nov 30, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants