asyncio, inspect: use static types in TypeIs#15931
Open
JelleZijlstra wants to merge 2 commits into
Open
Conversation
The set-theoretically correct way to write these TypeIs functions is with a type that includes all instances of the type they are narrowing to; for example "Awaitable[Any]" means "some unknown set of awaitables"; "Awaitable[object]" means "all awaitables, regardless of what they await to". We can't spell this type for cases involving invariant type parameters or callable parameter sets, so leave those alone for now. This may cause new type checker diagnostics because it will lead type checkers to often infer more precise types. Those errors are at least in theory correct, but let's see how big the fallout is.
# Conflicts: # stdlib/inspect.pyi
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: psycopg (https://github.com/psycopg/psycopg)
+ psycopg_pool/psycopg_pool/_acompat.py:184: error: Incompatible types in assignment (expression has type "object", variable has type "T | Coroutine[Any, Any, T]") [assignment]
+ psycopg_pool/psycopg_pool/_acompat.py:185: error: Incompatible return value type (got "T | Coroutine[Any, Any, T]", expected "T") [return-value]
aiohttp-devtools (https://github.com/aio-libs/aiohttp-devtools)
+ aiohttp_devtools/runserver/config.py:240: error: Incompatible types in assignment (expression has type "object", variable has type "Application | Awaitable[Application]") [assignment]
prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/flow_engine.py:1464: error: Incompatible types in assignment (expression has type "object", variable has type "R | type[NotSet]") [assignment]
+ src/prefect/concurrency/v1/sync.py:63: error: Argument 1 to "emit_concurrency_acquisition_events" has incompatible type "list[MinimalConcurrencyLimitResponse] | Coroutine[Any, Any, list[MinimalConcurrencyLimitResponse]]"; expected "list[MinimalConcurrencyLimitResponse]" [arg-type]
+ src/prefect/concurrency/v1/sync.py:72: error: Argument 1 to "emit_concurrency_release_events" has incompatible type "list[MinimalConcurrencyLimitResponse] | Coroutine[Any, Any, list[MinimalConcurrencyLimitResponse]]"; expected "list[MinimalConcurrencyLimitResponse]" [arg-type]
+ src/prefect/cli/deployment.py:175: error: "object" has no attribute "model_dump" [attr-defined]
steam.py (https://github.com/Gobot1234/steam.py)
+ steam/utils.py:894: error: Incompatible return value type (got "object", expected "_T") [return-value]
strawberry (https://github.com/strawberry-graphql/strawberry)
+ strawberry/utils/await_maybe.py:13: error: Incompatible return value type (got "object", expected "T") [return-value]
+ strawberry/types/object_type.py:295: error: Value of type variable "T" of "_inject_default_for_maybe_annotations" cannot be "object" [type-var]
+ strawberry/types/object_type.py:296: error: Value of type variable "T" of "_wrap_dataclass" cannot be "object" [type-var]
aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/web.py:295:15: error: Incompatible types in assignment (expression has type "object", variable has type "Application | Awaitable[Application]") [assignment]
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+ src/hydra_zen/third_party/beartype.py:125: error: Cannot assign to a method [method-assign]
scrapy (https://github.com/scrapy/scrapy)
+ scrapy/utils/defer.py:570: error: Incompatible return value type (got "_T | Awaitable[_T]", expected "Awaitable[_T]") [return-value]
+ scrapy/cmdline.py:70: error: Incompatible types in assignment (expression has type "object", target has type "ScrapyCommand") [assignment]
discord.py (https://github.com/Rapptz/discord.py)
+ discord/utils.py:714: error: Incompatible return value type (got "object", expected "T") [return-value]
pandera (https://github.com/pandera-dev/pandera)
+ pandera/dtypes.py:568: error: Unused "type: ignore" comment [unused-ignore]
+ tests/pandas/test_dtypes.py:306: error: Item "object" of "Any | object" has no attribute "foo" [union-attr]
pytest (https://github.com/pytest-dev/pytest)
+ testing/test_monkeypatch.py:439: error: Statement is unreachable [unreachable]
trio (https://github.com/python-trio/trio)
+ src/trio/_core/_tests/test_ki.py:682: error: Argument 1 to "_consume_async_generator" has incompatible type "AsyncGeneratorType[object, Never]"; expected "AsyncGenerator[None, None]" [arg-type]
+ src/trio/_core/_tests/test_ki.py:687: error: Argument 1 to "send" of "GeneratorType" has incompatible type "None"; expected "Never" [arg-type]
+ src/trio/_core/_tests/test_ki.py:687: error: Argument 1 to "send" of "CoroutineType" has incompatible type "None"; expected "Never" [arg-type]
anyio (https://github.com/agronholm/anyio)
+ src/anyio/from_thread.py:278: error: Argument 1 to "set_result" of "Future" has incompatible type "object"; expected "T_Retval" [arg-type]
kopf (https://github.com/nolar/kopf)
+ kopf/_core/intents/piggybacking.py:165: error: Incompatible types in assignment (expression has type "object", variable has type "str | None") [assignment]
graphql-core (https://github.com/graphql-python/graphql-core)
+ tests/execution/test_middleware.py:265: error: "object" has no attribute "data" [attr-defined]
+ tests/execution/test_middleware.py:267: error: "object" has no attribute "data" [attr-defined]
paasta (https://github.com/yelp/paasta)
+ paasta_tools/async_utils.py:162: error: "None" has no attribute "close" [attr-defined]
|
Member
Author
|
(Will post back with results from going over mypy-primer.) |
Member
|
I previously tried a variant of this in #14784 and posted some primer analysis in that thread, if that helps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The set-theoretically correct way to write these TypeIs functions is
with a type that includes all instances of the type they are narrowing to;
for example "Awaitable[Any]" means "some unknown set of awaitables";
"Awaitable[object]" means "all awaitables, regardless of what they await to".
We can't spell this type for cases involving invariant type parameters or
callable parameter sets, so leave those alone for now.
This may cause new type checker diagnostics because it will lead type
checkers to often infer more precise types. Those errors are at least
in theory correct, but let's see how big the fallout is.