Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove TypeType exception for protocol instantiation #18346

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

This disallows direct instantiation of cls: type[Proto]

Discussed in https://discuss.python.org/t/compatibility-of-protocol-class-object-with-type-t-and-type-any/48442/2

Users should use Callable[..., Proto] instead. This helps with __init__ unsoundness.

Subset of #18094, which also touched abstract classes.

This disallows direct instantiation of `cls: type[Proto]`

Discussed in https://discuss.python.org/t/compatibility-of-protocol-class-object-with-type-t-and-type-any/48442/2

Users should use `Callable[..., Proto]` instead. This helps with
`__init__` unsoundness.

Subset of python#18094, which also touched
abstract classes.

This comment has been minimized.

Copy link
Contributor

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

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/_py_transformer.py:254: error: Cannot instantiate protocol class "Dumper"  [misc]
+ psycopg/psycopg/_py_transformer.py:254: note: Consider using "Callable[..., Dumper]" instead of "type[Dumper]"
+ psycopg/psycopg/_py_transformer.py:296: error: Cannot instantiate protocol class "Dumper"  [misc]
+ psycopg/psycopg/_py_transformer.py:296: note: Consider using "Callable[..., Dumper]" instead of "type[Dumper]"
+ psycopg/psycopg/_py_transformer.py:360: error: Cannot instantiate protocol class "Loader"  [misc]
+ psycopg/psycopg/_py_transformer.py:360: note: Consider using "Callable[..., Loader]" instead of "type[Loader]"
+ psycopg/psycopg/sql.py:378: error: Cannot instantiate protocol class "Escaping"  [misc]
+ psycopg/psycopg/sql.py:378: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ psycopg/psycopg/adapt.py:64: error: Cannot instantiate protocol class "Escaping"  [misc]
+ psycopg/psycopg/adapt.py:64: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ psycopg/psycopg/adapt.py:74: error: Cannot instantiate protocol class "Escaping"  [misc]
+ psycopg/psycopg/adapt.py:74: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ psycopg/psycopg/_copy_base.py:99: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/_copy_base.py:99: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/types/string.py:135: error: Cannot instantiate protocol class "Escaping"  [misc]
+ psycopg/psycopg/types/string.py:135: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ psycopg/psycopg/types/string.py:182: error: Cannot instantiate protocol class "Escaping"  [misc]
+ psycopg/psycopg/types/string.py:182: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ psycopg/psycopg/_cursor_base.py:375: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/_cursor_base.py:375: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/client_cursor.py:44: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/client_cursor.py:44: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/types/composite.py:141: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/types/composite.py:141: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/types/composite.py:165: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/types/composite.py:165: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/types/composite.py:239: error: Cannot instantiate protocol class "Transformer"  [misc]
+ psycopg/psycopg/types/composite.py:239: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ psycopg/psycopg/types/array.py:49: error: Cannot instantiate protocol class "Dumper"  [misc]
+ psycopg/psycopg/types/array.py:49: note: Consider using "Callable[..., Dumper]" instead of "type[Dumper]"
+ tests/test_query.py:92: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_query.py:92: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_query.py:121: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_query.py:121: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_query.py:140: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_query.py:140: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_query.py:160: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_query.py:160: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/fix_faker.py:88: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/fix_faker.py:88: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/adapters_example.py:28: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/adapters_example.py:28: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/types/test_none.py:6: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_none.py:6: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_bool.py:29: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_bool.py:29: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_bool.py:40: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_bool.py:40: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:29: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:29: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:49: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:49: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:63: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:63: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:194: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:194: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:309: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:309: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:335: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:335: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_adapt.py:343: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_adapt.py:343: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_array.py:126: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_array.py:126: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_array.py:197: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_array.py:197: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/crdb/test_adapt.py:34: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/crdb/test_adapt.py:34: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_sql.py:57: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/test_sql.py:57: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/test_sql.py:59: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/test_sql.py:59: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/test_sql.py:63: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/test_sql.py:63: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/test_sql.py:65: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/test_sql.py:65: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/test_cursor_common_async.py:191: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_cursor_common_async.py:191: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/test_cursor_common.py:193: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/test_cursor_common.py:193: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_numeric.py:129: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_numeric.py:129: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_numeric.py:218: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_numeric.py:218: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/types/test_numeric.py:378: error: Cannot instantiate protocol class "Transformer"  [misc]
+ tests/types/test_numeric.py:378: note: Consider using "Callable[..., Transformer]" instead of "type[Transformer]"
+ tests/pq/test_escaping.py:19: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/pq/test_escaping.py:19: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/pq/test_escaping.py:28: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/pq/test_escaping.py:28: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/pq/test_escaping.py:38: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/pq/test_escaping.py:38: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/pq/test_escaping.py:42: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/pq/test_escaping.py:42: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/pq/test_escaping.py:58: error: Cannot instantiate protocol class "Escaping"  [misc]
+ tests/pq/test_escaping.py:58: note: Consider using "Callable[..., Escaping]" instead of "type[Escaping]"
+ tests/pq/test_escaping.py:67: error: Cannot instantiate protocol class "Escaping"  [misc]

... (truncated 23 lines) ...

urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/connectionpool.py:248: error: Cannot instantiate protocol class "BaseHTTPConnection"  [misc]
+ src/urllib3/connectionpool.py:248: note: Consider using "Callable[..., BaseHTTPConnection]" instead of "type[BaseHTTPConnection]"
+ src/urllib3/connectionpool.py:1067: error: Cannot instantiate protocol class "BaseHTTPSConnection"  [misc]
+ src/urllib3/connectionpool.py:1067: note: Consider using "Callable[..., BaseHTTPSConnection]" instead of "type[BaseHTTPSConnection]"

koda-validate (https://github.com/keithasaurus/koda-validate)
+ koda_validate/dataclasses.py:214: error: Cannot instantiate protocol class "DataclassLike"  [misc]
+ koda_validate/dataclasses.py:214: note: Consider using "Callable[..., DataclassLike]" instead of "type[DataclassLike]"
+ koda_validate/dataclasses.py:266: error: Cannot instantiate protocol class "DataclassLike"  [misc]
+ koda_validate/dataclasses.py:266: note: Consider using "Callable[..., DataclassLike]" instead of "type[DataclassLike]"

streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/runtime/runtime.py: note: In member "__init__" of class "Runtime":
+ lib/streamlit/runtime/runtime.py:210:29: error: Cannot instantiate protocol class "SessionManager"  [misc]
+ lib/streamlit/runtime/runtime.py:210:29: note: Consider using "Callable[..., SessionManager]" instead of "type[SessionManager]"

scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/python.py:326: error: Cannot instantiate protocol class "Iterable"  [misc]
+ scrapy/utils/python.py:326: note: Error code "misc" not covered by "type: ignore" comment
+ scrapy/utils/python.py:326: note: Consider using "Callable[..., Iterable]" instead of "type[Iterable]"
+ scrapy/pipelines/files.py:561: error: Cannot instantiate protocol class "FilesStoreProtocol"  [misc]
+ scrapy/pipelines/files.py:561: note: Consider using "Callable[..., FilesStoreProtocol]" instead of "type[FilesStoreProtocol]"

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ tests/annotations/mypy_checks.py:26: error: Cannot instantiate protocol class "BuildsWithSig"  [misc]
+ tests/annotations/mypy_checks.py:26: note: Error code "misc" not covered by "type: ignore" comment
+ tests/annotations/mypy_checks.py:26: note: Consider using "Callable[..., BuildsWithSig]" instead of "type[BuildsWithSig]"
+ tests/annotations/mypy_checks.py:50: error: Cannot instantiate protocol class "BuildsWithSig"  [misc]
+ tests/annotations/mypy_checks.py:50: note: Error code "misc" not covered by "type: ignore" comment
+ tests/annotations/mypy_checks.py:50: note: Consider using "Callable[..., BuildsWithSig]" instead of "type[BuildsWithSig]"

discord.py (https://github.com/Rapptz/discord.py)
+ discord/gateway.py:328: error: Cannot instantiate protocol class "_DecompressionContext"  [misc]
+ discord/gateway.py:328: note: Consider using "Callable[..., _DecompressionContext]" instead of "type[_DecompressionContext]"
+ discord/ext/commands/converter.py:1151: error: Cannot instantiate protocol class "Converter"  [misc]
+ discord/ext/commands/converter.py:1151: note: Consider using "Callable[..., Converter]" instead of "type[Converter]"
+ discord/ext/commands/converter.py:1326: error: Cannot instantiate protocol class "Converter"  [misc]
+ discord/ext/commands/converter.py:1326: note: Consider using "Callable[..., Converter]" instead of "type[Converter]"
+ discord/ext/commands/hybrid.py:143: error: Cannot instantiate protocol class "Converter"  [misc]
+ discord/ext/commands/hybrid.py:143: note: Consider using "Callable[..., Converter]" instead of "type[Converter]"

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/profile.py:145: error: Cannot instantiate protocol class "SupportsEquip"  [misc]
+ steam/profile.py:145: note: Consider using "Callable[..., SupportsEquip]" instead of "type[SupportsEquip]"
+ steam/ext/commands/commands.py:520: error: Cannot instantiate protocol class "ConverterBase"  [misc]
+ steam/ext/commands/commands.py:520: note: Consider using "Callable[..., ConverterBase]" instead of "type[ConverterBase]"
+ steam/ext/commands/commands.py:566: error: Cannot instantiate protocol class "ConverterBase"  [misc]
+ steam/ext/commands/commands.py:566: note: Consider using "Callable[..., ConverterBase]" instead of "type[ConverterBase]"

kornia (https://github.com/kornia/kornia)
+ kornia/utils/helpers.py:366: error: Cannot instantiate protocol class "DataclassInstance"  [misc]
+ kornia/utils/helpers.py:366: note: Error code "misc" not covered by "type: ignore" comment
+ kornia/utils/helpers.py:366: note: Consider using "Callable[..., DataclassInstance]" instead of "type[DataclassInstance]"

xarray-dataclasses (https://github.com/astropenguin/xarray-dataclasses)
+ xarray_dataclasses/datamodel.py:149: error: Cannot instantiate protocol class "DataClass"  [misc]
+ xarray_dataclasses/datamodel.py:149: note: Consider using "Callable[..., DataClass]" instead of "type[DataClass]"

schemathesis (https://github.com/schemathesis/schemathesis)
+ src/schemathesis/auths.py: note: In member "_set_provider" of class "AuthStorage":
+ src/schemathesis/auths.py:333: error: Cannot instantiate protocol class "AuthProvider"  [misc]
+ src/schemathesis/auths.py:333: note: Consider using "Callable[..., AuthProvider]" instead of "type[AuthProvider]"
+ src/schemathesis/models.py: note: In member "_get_serializer" of class "Case":
+ src/schemathesis/models.py:392: error: Cannot instantiate protocol class "Serializer"  [misc]
+ src/schemathesis/models.py:392: note: Consider using "Callable[..., Serializer]" instead of "type[Serializer]"
+ src/schemathesis/models.py: note: At top level:

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.

1 participant