Description
Describe the bug
The aiohttp.ClientSession.timeout attribute has a type of Union[object, aiohttp.ClientTimeout], however the code logic will never actually assign a bare object type to the self._timeout attribute, making this typing quite over-inclusive. Trying to use this attribute in typed code results in having to use cast(aiohttp.ClientTimeout, session.timeout), which is far from ideal considering one can just fix the typing in the library.
I ran into this while using Python 3.8.10, but the exact same explanation above applies to the current master branch (and the version I'm using of course), as shown by the snippets below.
3.8 branch __init__ parameter:
Line 217 in 6243204
3.8 branch self._timeout assignment:
Lines 261 to 290 in 6243204
Note the
# type: ignore comment on L278 there - it's because the timeout is sentinel check does not narrow down the timeout type. The correct way to go about this would be to use a cast there instead of ignoring the issue like that.
3.8 branch timeout attribute declaration:
Lines 1029 to 1032 in 6243204
Master branch __init__ parameter:
Line 215 in 52fa599
Master branch self._timeout assignment:
Lines 260 to 263 in 52fa599
Due to a different handling of the
sentinel value via an Enum member, no cast is needed here.
Master branch timeout attribute declaration:
Lines 1008 to 1011 in 52fa599
The attribute type is still over-inclusive here though.
The solution would be quite simple:
@property
def timeout(self) -> ClientTimeout:
"""Timeout for the session."""
return self._timeoutPlease let me know if you'd welcome a PR for this. I'd like to get this backported back to 3.8 (that I'm using) if possible, but if not, just fixing it in the master branch so that it's correct going forward would be good enough for me.
To Reproduce
Utilize some kind of a type checker like MyPy.
import asyncio
import aiohttp
async def main:
session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
# read back the total time attribute
total_time = session.timeout.total # "object" type of "Union[object, ClientTimeout]" has no attribute "total"
print(total_time)
asyncio.run(main())Expected behavior
The attribute having only the aiohttp.ClientTimeout type and not requiring cast usage when accessing the attribute during library usage in user code.
Logs/tracebacks
Not applicablePython Version
Python 3.8.10aiohttp Version
Version: 3.8.1multidict Version
Version: 6.0.2yarl Version
Version: 1.7.2OS
Windows
Related component
Client
Additional context
Related issues and PRs:
Code of Conduct
- I agree to follow the aio-libs Code of Conduct