Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.3k
Unify timeout behaviour#463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ae8fa56aab0b211248837edf56f2a68c82fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,9 +8,18 @@ | ||
| from .__version__ import __version__ | ||
| from .utils import get_ca_bundle_from_env, get_logger | ||
| class UnsetType: | ||
| pass | ||
| UNSET = UnsetType() | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to put this here instead of the model package due to a circular dependency. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| CertTypes = typing.Union[str, typing.Tuple[str, str], typing.Tuple[str, str, str]] | ||
| VerifyTypes = typing.Union[str, bool, ssl.SSLContext] | ||
| TimeoutTypes = typing.Union[float, typing.Tuple[float, float, float], "TimeoutConfig"] | ||
| TimeoutTypes = typing.Union[ | ||
| float, typing.Tuple[float, float, float], "TimeoutConfig", UnsetType | ||
| ] | ||
| HTTPVersionTypes = typing.Union[ | ||
| str, typing.List[str], typing.Tuple[str], "HTTPVersionConfig" | ||
| ] | ||
| @@ -224,23 +233,39 @@ def __init__( | ||
| self, | ||
| timeout: TimeoutTypes = None, | ||
| *, | ||
| connect_timeout: float = None, | ||
| read_timeout: float = None, | ||
| write_timeout: float = None, | ||
| connect_timeout: typing.Union[float, UnsetType, None] = UNSET, | ||
| read_timeout: typing.Union[float, UnsetType, None] = UNSET, | ||
| write_timeout: typing.Union[float, UnsetType, None] = UNSET, | ||
| ): | ||
| if timeout is None: | ||
| self.connect_timeout = connect_timeout | ||
| self.read_timeout = read_timeout | ||
| self.write_timeout = write_timeout | ||
| self.connect_timeout: typing.Optional[float] = ( | ||
| connect_timeout | ||
| if not isinstance(connect_timeout, UnsetType) | ||
| else DEFAULT_TIMEOUT_CONFIG.connect_timeout | ||
| ) | ||
| self.read_timeout: typing.Optional[float] = ( | ||
| read_timeout | ||
| if not isinstance(read_timeout, UnsetType) | ||
| else DEFAULT_TIMEOUT_CONFIG.read_timeout | ||
| ) | ||
| self.write_timeout: typing.Optional[float] = ( | ||
| write_timeout | ||
| if not isinstance(write_timeout, UnsetType) | ||
| else DEFAULT_TIMEOUT_CONFIG.write_timeout | ||
| ) | ||
| else: | ||
| # Specified as a single timeout value | ||
| assert connect_timeout is None | ||
| assert read_timeout is None | ||
| assert write_timeout is None | ||
| assert connect_timeout is UNSET | ||
| assert read_timeout is UNSET | ||
| assert write_timeout is UNSET | ||
| if isinstance(timeout, TimeoutConfig): | ||
| self.connect_timeout = timeout.connect_timeout | ||
| self.read_timeout = timeout.read_timeout | ||
| self.write_timeout = timeout.write_timeout | ||
florimondmanca marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| elif isinstance(timeout, UnsetType): | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason (maybe type hints-related?) we can't use ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that first but ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the mypy error: The weird thing is that it complains about these lines:
| ||
| self.connect_timeout = DEFAULT_TIMEOUT_CONFIG.connect_timeout | ||
| self.read_timeout = DEFAULT_TIMEOUT_CONFIG.read_timeout | ||
| self.write_timeout = DEFAULT_TIMEOUT_CONFIG.write_timeout | ||
| elif isinstance(timeout, tuple): | ||
| self.connect_timeout = timeout[0] | ||
| self.read_timeout = timeout[1] | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately can't use this pattern
because
NewTypedoes not allowisinstance()on it, and it was needed otherwise mypy was complaining inTimeoutConfig.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, well done here.
From Python 3.8,
typing.Literalwould allow to solve this quite elegantly:Not sure that there'll be a backport to 3.6 and 3.7 unfortunately, but just thought it was interesting to share. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's available in the
typing_extensionspackage https://github.com/python/typing/tree/master/typing_extensionsBut I guess it's not worth adding a new dependency just for this (since we have the class workaround anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@florimondmanca I think
typing.Literalis not valid in this usage.UNSETis not aLiteral.