Skip to content

asyncio, inspect: use static types in TypeIs#15931

Open
JelleZijlstra wants to merge 2 commits into
python:mainfrom
JelleZijlstra:typeis-top
Open

asyncio, inspect: use static types in TypeIs#15931
JelleZijlstra wants to merge 2 commits into
python:mainfrom
JelleZijlstra:typeis-top

Conversation

@JelleZijlstra

Copy link
Copy Markdown
Member

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.

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.
@github-actions

Copy link
Copy Markdown
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]

@JelleZijlstra

Copy link
Copy Markdown
Member Author

(Will post back with results from going over mypy-primer.)

@AlexWaygood

Copy link
Copy Markdown
Member

I previously tried a variant of this in #14784 and posted some primer analysis in that thread, if that helps

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