Type narrowing failure inside AsyncExitStack
blocks
#13936
Labels
bug
mypy got something wrong
topic-async
async, await, asyncio
topic-type-context
Type context / bidirectional inference
topic-type-narrowing
Conditional type narrowing / binder
Bug Report
In the following synchronous code, mypy is correctly able to perform type narrowing to determine that the
resource
variable must be an instance ofResource
whenresource.do_thing()
is called inside thefoo()
function:However, in an asynchronous version of this code, mypy is not able to narrow the type in the same way, leading to a false positive being emitted:
Curiously, if we rewrite
async_foo()
like this, the false-positive error goes away:Real-life use case
My use case is to write a function like this using
aiohttp
, in which a user can optionally supply an existingaiohttp.ClientSession()
instance if they want to, or let the function create one on the fly if not.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.10&gist=0ee494765b5abc08fc9f161bc63656a7
Your Environment
The text was updated successfully, but these errors were encountered: