3838:license: Apache 2.0, see LICENSE for more details.
3939"""
4040
41+ from __future__ import annotations
42+
4143import warnings
4244
4345import urllib3
5052 charset_normalizer_version = None
5153
5254try :
53- from chardet import __version__ as chardet_version
55+ from chardet import __version__ as chardet_version # type: ignore[import-not-found]
5456except ImportError :
5557 chardet_version = None
5658
5759
58- def check_compatibility (urllib3_version , chardet_version , charset_normalizer_version ):
59- urllib3_version = urllib3_version .split ("." )
60- assert urllib3_version != ["dev" ] # Verify urllib3 isn't installed from git.
60+ def check_compatibility (
61+ urllib3_version : str ,
62+ chardet_version : str | None ,
63+ charset_normalizer_version : str | None ,
64+ ) -> None :
65+ urllib3_version_list = urllib3_version .split ("." )
66+ assert urllib3_version_list != ["dev" ] # Verify urllib3 isn't installed from git.
6167
6268 # Sometimes, urllib3 only reports its version as 16.1.
63- if len (urllib3_version ) == 2 :
64- urllib3_version .append ("0" )
69+ if len (urllib3_version_list ) == 2 :
70+ urllib3_version_list .append ("0" )
6571
6672 # Check urllib3 for compatibility.
67- major , minor , patch = urllib3_version # noqa: F811
73+ major , minor , patch = urllib3_version_list # noqa: F811
6874 major , minor , patch = int (major ), int (minor ), int (patch )
6975 # urllib3 >= 1.21.1
7076 assert major >= 1
@@ -75,8 +81,8 @@ def check_compatibility(urllib3_version, chardet_version, charset_normalizer_ver
7581 if chardet_version :
7682 major , minor , patch = chardet_version .split ("." )[:3 ]
7783 major , minor , patch = int (major ), int (minor ), int (patch )
78- # chardet_version >= 3.0.2, < 8 .0.0
79- assert (3 , 0 , 2 ) <= (major , minor , patch ) < (8 , 0 , 0 )
84+ # chardet_version >= 3.0.2, < 6 .0.0
85+ assert (3 , 0 , 2 ) <= (major , minor , patch ) < (7 , 0 , 0 )
8086 elif charset_normalizer_version :
8187 major , minor , patch = charset_normalizer_version .split ("." )[:3 ]
8288 major , minor , patch = int (major ), int (minor ), int (patch )
@@ -90,28 +96,28 @@ def check_compatibility(urllib3_version, chardet_version, charset_normalizer_ver
9096 )
9197
9298
93- def _check_cryptography (cryptography_version ) :
99+ def _check_cryptography (cryptography_version : str ) -> None :
94100 # cryptography < 1.3.4
95101 try :
96- cryptography_version = list (map (int , cryptography_version .split ("." )))
102+ cryptography_version_list = list (map (int , cryptography_version .split ("." )))
97103 except ValueError :
98104 return
99105
100- if cryptography_version < [1 , 3 , 4 ]:
101- warning = (
102- f"Old version of cryptography ({ cryptography_version } ) may cause slowdown."
103- )
106+ if cryptography_version_list < [1 , 3 , 4 ]:
107+ warning = f"Old version of cryptography ({ cryptography_version_list } ) may cause slowdown."
104108 warnings .warn (warning , RequestsDependencyWarning )
105109
106110
107111# Check imported dependencies for compatibility.
108112try :
109113 check_compatibility (
110- urllib3 .__version__ , chardet_version , charset_normalizer_version
114+ urllib3 .__version__ , # type: ignore[reportPrivateImportUsage]
115+ chardet_version , # type: ignore[reportUnknownArgumentType]
116+ charset_normalizer_version ,
111117 )
112118except (AssertionError , ValueError ):
113119 warnings .warn (
114- f"urllib3 ({ urllib3 .__version__ } ) or chardet "
120+ f"urllib3 ({ urllib3 .__version__ } ) or chardet " # type: ignore[reportPrivateImportUsage]
115121 f"({ chardet_version } )/charset_normalizer ({ charset_normalizer_version } ) "
116122 "doesn't match a supported version!" ,
117123 RequestsDependencyWarning ,
@@ -132,9 +138,11 @@ def _check_cryptography(cryptography_version):
132138 pyopenssl .inject_into_urllib3 ()
133139
134140 # Check cryptography version
135- from cryptography import __version__ as cryptography_version
141+ from cryptography import ( # type: ignore[reportMissingImports]
142+ __version__ as cryptography_version , # type: ignore[reportUnknownVariableType]
143+ )
136144
137- _check_cryptography (cryptography_version )
145+ _check_cryptography (cryptography_version ) # type: ignore[reportUnknownArgumentType]
138146except ImportError :
139147 pass
140148
@@ -177,6 +185,34 @@ def _check_cryptography(cryptography_version):
177185from .sessions import Session , session
178186from .status_codes import codes
179187
188+ __all__ = (
189+ "ConnectionError" ,
190+ "ConnectTimeout" ,
191+ "HTTPError" ,
192+ "JSONDecodeError" ,
193+ "PreparedRequest" ,
194+ "ReadTimeout" ,
195+ "Request" ,
196+ "RequestException" ,
197+ "Response" ,
198+ "Session" ,
199+ "Timeout" ,
200+ "TooManyRedirects" ,
201+ "URLRequired" ,
202+ "codes" ,
203+ "delete" ,
204+ "get" ,
205+ "head" ,
206+ "options" ,
207+ "packages" ,
208+ "patch" ,
209+ "post" ,
210+ "put" ,
211+ "request" ,
212+ "session" ,
213+ "utils" ,
214+ )
215+
180216logging .getLogger (__name__ ).addHandler (NullHandler ())
181217
182218# FileModeWarnings go off per the default.
0 commit comments