From b58d001177c5acf67a8e89c49bdf5c31fc989591 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 26 Oct 2024 19:45:16 +0200 Subject: [PATCH] Cleanup --- httpie/client.py | 7 ++++--- httpie/compat.py | 43 +++++++++++++++++++++---------------------- tests/test_network.py | 18 +++++++++--------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/httpie/client.py b/httpie/client.py index 61801d37cc..de42a74350 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -123,14 +123,15 @@ def collect_messages( happy_eyeballs=args.happy_eyeballs, ) - if args.disable_http3 is False and args.force_http3 is True: + if not args.disable_http3 and args.force_http3: requests_session.quic_cache_layer[(parsed_url.host, parsed_url.port or 443)] = (parsed_url.host, parsed_url.port or 443) # well, this one is tricky. If we allow HTTP/3, and remote host was marked as QUIC capable # but is not anymore, we may face an indefinite hang if timeout isn't set. This could surprise some user. elif ( - args.disable_http3 is False + not args.disable_http3 + and not args.force_http3 and requests_session.quic_cache_layer.get((parsed_url.host, parsed_url.port or 443)) is not None - and args.force_http3 is False + ): # we only set the connect timeout, the rest is still indefinite. if send_kwargs["timeout"] is None: diff --git a/httpie/compat.py b/httpie/compat.py index 2ba557972a..a728e9d674 100644 --- a/httpie/compat.py +++ b/httpie/compat.py @@ -2,11 +2,12 @@ from typing import Any, Optional, Iterable from httpie.cookies import HTTPieCookiePolicy -from http import cookiejar # noqa +from http import cookiejar # noqa import niquests from niquests._compat import HAS_LEGACY_URLLIB3 + # to understand why this is required # see https://niquests.readthedocs.io/en/latest/community/faq.html#what-is-urllib3-future # short story, urllib3 (import/top-level import) may be the legacy one https://github.com/urllib3/urllib3 @@ -31,6 +32,25 @@ resolve_ssl_version, ) +# Importlib_metadata was a provisional module, so the APIs changed quite a few times +# between 3.8-3.10. It was also not included in the standard library until 3.8, so +# we install the backport for <3.8. +if sys.version_info >= (3, 8): + import importlib.metadata as importlib_metadata +else: + import importlib_metadata + +is_windows = 'win32' in str(sys.platform).lower() +is_frozen = getattr(sys, 'frozen', False) + +MIN_SUPPORTED_PY_VERSION = (3, 7) +MAX_SUPPORTED_PY_VERSION = (3, 11) + +# Request does not carry the original policy attached to the +# cookie jar, so until it is resolved we change the global cookie +# policy. +cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy + def has_ipv6_support(new_value: Optional[bool] = None) -> bool: if new_value is not None: @@ -52,17 +72,6 @@ def enforce_niquests(): sys.modules["requests.exceptions"] = niquests.exceptions sys.modules["requests.packages.urllib3"] = urllib3 -# Request does not carry the original policy attached to the -# cookie jar, so until it is resolved we change the global cookie -# policy. -cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy - - -is_windows = 'win32' in str(sys.platform).lower() -is_frozen = getattr(sys, 'frozen', False) - -MIN_SUPPORTED_PY_VERSION = (3, 7) -MAX_SUPPORTED_PY_VERSION = (3, 11) try: from functools import cached_property @@ -114,16 +123,6 @@ def __get__(self, instance, cls=None): return res -# importlib_metadata was a provisional module, so the APIs changed quite a few times -# between 3.8-3.10. It was also not included in the standard library until 3.8, so -# we install the backport for <3.8. - -if sys.version_info >= (3, 8): - import importlib.metadata as importlib_metadata -else: - import importlib_metadata - - def find_entry_points(entry_points: Any, group: str) -> Iterable[importlib_metadata.EntryPoint]: if hasattr(entry_points, "select"): # Python 3.10+ / importlib_metadata >= 3.9.0 return entry_points.select(group=group) diff --git a/tests/test_network.py b/tests/test_network.py index 0e53d6b7bb..f586053c5f 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -45,15 +45,15 @@ def test_non_existent_interface_arg(httpbin): ('-0-8000', (0, 8000)), ('0-0', (0, 0)), # Port ranges — invalid - (f'2-1', 'not a valid port range'), - (f'2-', 'not a number'), - (f'2-A', 'not a number'), - (f'A-A', 'not a number'), - (f'A-2', 'not a number'), - (f'-10-1', OUTSIDE_VALID_PORT_RANGE_ERROR), - (f'1--1', OUTSIDE_VALID_PORT_RANGE_ERROR), - (f'-10--1', OUTSIDE_VALID_PORT_RANGE_ERROR), - (f'1-{MAX_PORT + 1}', OUTSIDE_VALID_PORT_RANGE_ERROR), + ('2-1', 'not a valid port range'), + ('2-', 'not a number'), + ('2-A', 'not a number'), + ('A-A', 'not a number'), + ('A-2', 'not a number'), + ('-10-1', OUTSIDE_VALID_PORT_RANGE_ERROR), + ('1--1', OUTSIDE_VALID_PORT_RANGE_ERROR), + ('-10--1', OUTSIDE_VALID_PORT_RANGE_ERROR), + ('1-{MAX_PORT + 1}', OUTSIDE_VALID_PORT_RANGE_ERROR), ]) def test_parse_local_port_arg(local_port_arg, expected_output): expected_error = expected_output if isinstance(expected_output, str) else None