Skip to content

Conversation

MengAiDev
Copy link

  • Modify ExpressionChecker to handle async function calls differently
  • Don't propagate expected type for awaited function calls to avoid incorrect inference
  • Add test cases to verify correct type inference for awaited function calls

Fixes: #19716

MengAiDev and others added 2 commits August 25, 2025 16:29
- Modify ExpressionChecker to handle async function calls differently
- Don't propagate expected type for awaited function calls to avoid incorrect inference
- Add test cases to verify correct type inference for awaited function calls

This comment has been minimized.

Copy link
Contributor

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

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/asynchronous/pool.py:452: error: Incompatible return value type (got "Mapping[str, Any]", expected "dict[str, Any]")  [return-value]
+ pymongo/asynchronous/bulk.py:604: error: Function does not return a value (it only ever returns None)  [func-returns-value]

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/client/orchestration/__init__.py:1088: error: Function does not return a value (it only ever returns None)  [func-returns-value]
- src/prefect/utilities/engine.py:251: error: Incompatible types in assignment (expression has type "tuple[Union[Any, Exception]]", variable has type "list[Coroutine[Any, Any, Union[Any, Exception]]]")  [assignment]
+ src/prefect/utilities/engine.py:251: error: Incompatible types in assignment (expression has type "list[Union[Any, Exception]]", variable has type "list[Coroutine[Any, Any, Union[Any, Exception]]]")  [assignment]
+ src/prefect/cli/transfer/__init__.py:193: error: Incompatible types in assignment (expression has type "list[list[PrefectBaseModel]]", variable has type "list[Sequence[Union[WorkPool, WorkQueue, DeploymentResponse, Flow, BlockType, BlockSchema, BlockDocument, Automation, GlobalConcurrencyLimitResponse, Variable]]]")  [assignment]
+ src/prefect/cli/transfer/__init__.py:193: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/prefect/cli/transfer/__init__.py:193: note: Consider using "Sequence" instead, which is covariant

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:2594: error: Redundant cast to "tuple[Transport, StreamProtocol]"  [redundant-cast]

core (https://github.com/home-assistant/core)
+ homeassistant/components/device_automation/__init__.py:225: error: Unused "type: ignore" comment  [unused-ignore]
+ homeassistant/components/device_automation/__init__.py:225: error: Incompatible return value type (got "list[Any | BaseException]", expected "list[list[dict[str, Any]] | Exception]")  [return-value]
+ homeassistant/components/device_automation/__init__.py:225: note: Error code "return-value" not covered by "type: ignore" comment
+ homeassistant/components/wallbox/coordinator.py:213: error: Returning Any from function declared to return "dict[str, Any]"  [no-any-return]
- homeassistant/components/minecraft_server/api.py:88: error: Item "None" of "Any | None" has no attribute "timeout"  [union-attr]
+ homeassistant/components/fastdotcom/coordinator.py:34: error: Returning Any from function declared to return "float"  [no-any-return]
+ homeassistant/components/amcrest/camera.py:223: error: Returning Any from function declared to return "bytes | None"  [no-any-return]
- homeassistant/components/powerwall/config_flow.py:52: error: Returning Any from function declared to return "tuple[Any, str]"  [no-any-return]

starlette (https://github.com/encode/starlette)
+ starlette/middleware/errors.py:178: error: Incompatible types in assignment (expression has type "Awaitable[None] | Response | Awaitable[Response]", variable has type "Response")  [assignment]
- starlette/middleware/errors.py:178: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request, Exception], Response]"  [arg-type]
+ starlette/middleware/errors.py:178: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request, Exception], Awaitable[None] | Response | Awaitable[Response]]"  [arg-type]
- starlette/_exception_handler.py:61: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request | WebSocket, Exception], Any]"  [arg-type]
+ starlette/_exception_handler.py:61: error: Argument 1 to "run_in_threadpool" has incompatible type "Callable[[Request, Exception], Response | Awaitable[Response]] | Callable[[WebSocket, Exception], Awaitable[None]]"; expected "Callable[[Request | WebSocket, Exception], Awaitable[None] | Response | Awaitable[Response]]"  [arg-type]

steam.py (https://github.com/Gobot1234/steam.py)
+ steam/abc.py:242: error: Need type annotation for "comment"  [var-annotated]
+ steam/gateway.py:456: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLogonResponse")  [assignment]
+ steam/gateway.py:588: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "UpdateAuthSessionWithSteamGuardCodeResponse")  [assignment]
+ steam/gateway.py:670: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLogonResponse")  [assignment]
+ steam/state.py:682: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientFsGetFriendsSteamLevelsResponse")  [assignment]
+ steam/state.py:702: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientFriendProfileInfoResponse")  [assignment]
+ steam/state.py:711: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientGetUserStatsResponse")  [assignment]
+ steam/state.py:722: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "GetAchievementsProgressResponse")  [assignment]
+ steam/state.py:806: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "AddFriendResponse")  [assignment]
+ steam/state.py:1870: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "GetItemsResponse")  [assignment]
+ steam/state.py:1925: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgDpGetNumberOfCurrentPlayersResponse")  [assignment]
+ steam/state.py:1935: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "GetGameAchievementsResponse")  [assignment]
+ steam/state.py:1946: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLbsFindOrCreateLbResponse")  [assignment]
+ steam/state.py:1966: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLbsGetLbEntriesResponse")  [assignment]
+ steam/state.py:1990: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLbsSetScoreResponse")  [assignment]
+ steam/state.py:2004: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientLbsSetUgcResponse")  [assignment]
+ steam/state.py:2043: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "ClientGetFriendsWhoPlayGameResponse")  [assignment]
+ steam/state.py:2075: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientRequestFreeLicenseResponse")  [assignment]
+ steam/state.py:2096: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "ClientGetLegacyGameKeyResponse")  [assignment]
+ steam/state.py:2118: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientRequestEncryptedAppTicketResponse")  [assignment]
+ steam/state.py:2126: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientGetAppOwnershipTicketResponse")  [assignment]
+ steam/state.py:2182: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientAuthListAck")  [assignment]
+ steam/state.py:2526: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientCheckAppBetaPasswordResponse")  [assignment]
+ steam/state.py:2667: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientGetDepotDecryptionKeyResponse")  [assignment]
+ steam/state.py:2679: error: Incompatible types in assignment (expression has type "ProtobufMessage | Message", variable has type "CMsgClientPicsAccessTokenResponse")  [assignment]

trio (https://github.com/python-trio/trio)
+ src/trio/_core/_tests/test_run.py:2290: error: Unused "type: ignore" comment  [unused-ignore]
+ src/trio/_core/_tests/test_run.py:2290: error: Incompatible types in assignment (expression has type "Value[object] | Error", variable has type "Outcome[str] | None")  [assignment]
+ src/trio/_core/_tests/test_run.py:2290: note: Error code "assignment" not covered by "type: ignore" comment

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.

Spurious type error with async function + generic callable argument + optional return type
1 participant