Improve static typing of 3rd party imports (#7496)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add missing `typecheck` dependencies * Fix static typing of `compat.chardet` * Remove unused `# type: ignore` comments
authored by Joren Hammudoglu · committed by GitHub · parent 3be097d · Browse files · 5 files·+19 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+19 added−14 removed
- other:+19 added−14 removed
Provenance
- Commit
e50e594- Tree
15178df- Author
Joren Hammudoglu <jhammudoglu@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
3be097d
5 files changed+19 added−14 removed
modified:src/requests/__init__.py+5 added-5 removed
50 unmodified lines51 except ImportError:52 charset_normalizer_version = None53 54 try:55- from chardet import __version__ as chardet_version # type: ignore[import-not-found]55+ from chardet import __version__ as chardet_version56 except ImportError:57 chardet_version = None58 59 51 unmodified lines111 # Check imported dependencies for compatibility.112 try:113 check_compatibility(114 urllib3.__version__, # type: ignore[reportPrivateImportUsage]115- chardet_version, # type: ignore[reportUnknownArgumentType]115+ chardet_version,116 charset_normalizer_version,117 )118 except (AssertionError, ValueError):119 warnings.warn(17 unmodified lines137 138 pyopenssl.inject_into_urllib3()139 140 # Check cryptography version141- from cryptography import ( # type: ignore[reportMissingImports]142- __version__ as cryptography_version, # type: ignore[reportUnknownVariableType]141+ from cryptography import (142+ __version__ as cryptography_version,143 )144 145- _check_cryptography(cryptography_version) # type: ignore[reportUnknownArgumentType]145+ _check_cryptography(cryptography_version)146 except ImportError:147 pass148 149 # urllib3's DependencyWarnings should be silenced.70 unmodified linesmodified:src/requests/compat.py+5 added-1 removed
12 unmodified lines13 14 import importlib15 import sys16 from types import ModuleType17+from typing import TYPE_CHECKING18 19 # -------20 # urllib321 # -------24 unmodified lines46 pass47 return chardet48 49 50+if TYPE_CHECKING:51+ import chardet52+else:49-chardet = _resolve_char_detection()53+ chardet = _resolve_char_detection()54 55 # -------56 # Pythons57 # -------60 unmodified linesmodified:src/requests/help.py+4 added-6 removed
1 """Module containing bug report helper(s)."""2 3-# pyright: reportUnknownMemberType=false4-3 import json4 import platform5 import ssl6 import sys9 unmodified lines16 except ImportError:17 charset_normalizer = None18 19 try:22- import chardet # type: ignore[import-not-found]20+ import chardet21 except ImportError:22 chardet = None23 24 try:2 unmodified lines27 pyopenssl = None28 OpenSSL = None29 cryptography = None30 else:33- import cryptography # type: ignore[import-not-found]34- import OpenSSL # type: ignore[import-not-found]31+ import cryptography32+ import OpenSSL33 34 35 def _implementation():36 """Return a dict with the Python implementation and version.11 unmodified lines48 if implementation == "CPython":49 implementation_version = platform.python_version()50 elif implementation == "PyPy":51 pypy = sys.pypy_version_info # type: ignore[attr-defined]54- implementation_version = f"{pypy.major}.{pypy.minor}.{pypy.micro}"52+ implementation_version = f"{pypy.major}.{pypy.minor}.{pypy.micro}" # pyright: ignore[reportUnknownMemberType]53 if sys.pypy_version_info.releaselevel != "final": # type: ignore[attr-defined]54 implementation_version = "".join(55 [implementation_version, sys.pypy_version_info.releaselevel] # type: ignore[attr-defined]56 )76 unmodified linesmodified:src/requests/models.py+2 added-2 removed
236 unmodified lines237 if isinstance(fp, (str, bytes, bytearray)):238 fdata = fp239 elif isinstance(fp, _SupportsRead): # type: ignore[reportUnnecessaryIsInstance] # defensive check for untyped callers240 fdata = fp.read()241- elif fp is None: # type: ignore[reportUnnecessaryComparison] # defensive check for untyped callers241+ elif fp is None: # defensive check for untyped callers242 continue243 else:244 fdata = fp245 763 unmodified lines1009 chunk_size=chunk_size, decode_unicode=decode_unicode1010 ):1011 if pending is not None:1012 # TODO: remove cast after iter_lines rewrite1013- chunk = cast("str | bytes", pending + chunk) # type: ignore[operator]1013+ chunk = cast("str | bytes", pending + chunk)1014 1015 if delimiter:1016 lines = chunk.split(delimiter) # type: ignore[arg-type]1017 else:163 unmodified linesmodified:pyproject.toml+3 added
67 unmodified lines686970717273747576777842 unmodified lines67 unmodified lines]typecheck = [ "pyright", "typing_extensions", "chardet", "cryptography", "PyOpenSSL",]
[tool.setuptools]license-files = ["LICENSE", "NOTICE"]42 unmodified lines