Skip to content

Commit

Permalink
Exclude the object possibility from the ClientSession.timeout type
Browse files Browse the repository at this point in the history
This is a follow-up PR to #6917, which fixes the overly inclusive typing
of `ClientSession.timeout` attribute, which can never actually end up as
an `object` instance. This leftover probably exists from the time when
there was the `_sentinel` value being assigned to the attribute (see
"3.8 branch `self._timeout` assignment" from the linked issue), hence
why it ended up this way.

For untyped code, there's absolutely no change — the `self._timeout`
attribute was never actually assigned the sentinel value to. Most of the
explanation already exists in #6917, here's the test code snippet from
there again:

```py
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())
```

For typed code, the current solution would be to `assert
isinstance(session.timeout, aiohttp.ClientTimeout)` everywhere the
attribute is being accessed. This PR removes this "unnecessary
necessity".

Co-authored-by: Sviatoslav Sydorenko <wk@sydorenko.org.ua>

Fixes #6917
PR #6923

(cherry picked from commit 78fa040)
  • Loading branch information
DevilXD authored and patchback[bot] committed Sep 20, 2022
1 parent 9fdf548 commit 39f9125
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES/6917.bugfix
@@ -0,0 +1,3 @@
Dropped the :class:`object` type possibility from
the :py:attr:`aiohttp.ClientSession.timeout`
property return type declaration.
1 change: 1 addition & 0 deletions CHANGES/6917.doc
1 change: 1 addition & 0 deletions CHANGES/6923.bugfix
1 change: 1 addition & 0 deletions CHANGES/6923.doc
2 changes: 1 addition & 1 deletion aiohttp/client.py
Expand Up @@ -1027,7 +1027,7 @@ def loop(self) -> asyncio.AbstractEventLoop:
return self._loop

@property
def timeout(self) -> Union[object, ClientTimeout]:
def timeout(self) -> ClientTimeout:
"""Timeout for the session."""
return self._timeout

Expand Down

0 comments on commit 39f9125

Please sign in to comment.