Skip to content
Myr

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

5 files+19 added−14 removed

modified:src/requests/__init__.py+5 added-5 removed
50 unmodified lines
51except ImportError:52 charset_normalizer_version = None5354try:55 from chardet import __version__ as chardet_version # type: ignore[import-not-found]55 from chardet import __version__ as chardet_version56except ImportError:57 chardet_version = None5859
51 unmodified lines
111# Check imported dependencies for compatibility.112try:113 check_compatibility(114 urllib3.__version__, # type: ignore[reportPrivateImportUsage]115 chardet_version, # type: ignore[reportUnknownArgumentType]115 chardet_version,116 charset_normalizer_version,117 )118except (AssertionError, ValueError):119 warnings.warn(
17 unmodified lines
137138 pyopenssl.inject_into_urllib3()139140 # 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 )144145 _check_cryptography(cryptography_version) # type: ignore[reportUnknownArgumentType]145 _check_cryptography(cryptography_version)146except ImportError:147 pass148149# urllib3's DependencyWarnings should be silenced.
70 unmodified lines
modified:src/requests/compat.py+5 added-1 removed
12 unmodified lines
1314import importlib15import sys16from types import ModuleType17from typing import TYPE_CHECKING1819# -------20# urllib321# -------
24 unmodified lines
46 pass47 return chardet484950if TYPE_CHECKING:51 import chardet52else:49chardet = _resolve_char_detection()53 chardet = _resolve_char_detection()5455# -------56# Pythons57# -------
60 unmodified lines
modified:src/requests/help.py+4 added-6 removed
1"""Module containing bug report helper(s)."""23# pyright: reportUnknownMemberType=false43import json4import platform5import ssl6import sys
9 unmodified lines
16except ImportError:17 charset_normalizer = None1819try:22 import chardet # type: ignore[import-not-found]20 import chardet21except ImportError:22 chardet = None2324try:
2 unmodified lines
27 pyopenssl = None28 OpenSSL = None29 cryptography = None30else:33 import cryptography # type: ignore[import-not-found]34 import OpenSSL # type: ignore[import-not-found]31 import cryptography32 import OpenSSL333435def _implementation():36 """Return a dict with the Python implementation and version.
11 unmodified lines
48 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 lines
modified:src/requests/models.py+2 added-2 removed
236 unmodified lines
237 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 lines
1009 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)10141015 if delimiter:1016 lines = chunk.split(delimiter) # type: ignore[arg-type]1017 else:
163 unmodified lines
modified:pyproject.toml+3 added