Skip to content

Constrain memoryview type var to allowed types - #13683

Open
srittau wants to merge 6 commits into
python:mainfrom
srittau:generic-memoryview
Open

srittau wants to merge 6 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

Copy link
Copy Markdown
Contributor

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

trio (https://github.com/python-trio/trio)
+ src/trio/_abc.py:331: 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:737: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:466: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:1155: error: Explicit "Any" is not allowed  [explicit-any]
+ src/trio/_subprocess.py:1164: 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:801: error: Explicit "Any" is not allowed  [explicit-any]

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1155: 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:1155: 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:2712: 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:2712: 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]

apprise (https://github.com/caronc/apprise)
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCMessage" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCMessage" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCAction" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCAction" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCContext" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCContext" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCContext" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCContext" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "XMPPConfig" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "XMPPConfig" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCContext" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCContext" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "IRCAction" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "IRCAction" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "XMPPConfig" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "XMPPConfig" defined here

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/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]

zulip (https://github.com/zulip/zulip)
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "SubTest" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "SubTest" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "SubmessageFixture" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "SubmessageFixture" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "FileFixture" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "FileFixture" defined here

ibis (https://github.com/ibis-project/ibis)
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "Any" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "Any" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:118: note: "__init__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:119: note: "__init__" of "object" defined here

prefect (https://github.com/PrefectHQ/prefect)
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here

strawberry (https://github.com/strawberry-graphql/strawberry)
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here
- ...typeshed_to_test/stdlib/builtins.pyi:140: note: "__init_subclass__" of "object" defined here
+ ...typeshed_to_test/stdlib/builtins.pyi:141: note: "__init_subclass__" of "object" defined here

This branch has not been deployed

No deployments
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