Skip to content

Commit e50e594

Browse files
authored
Improve static typing of 3rd party imports (#7496)
* Add missing `typecheck` dependencies * Fix static typing of `compat.chardet` * Remove unused `# type: ignore` comments
1 parent 3be097d commit e50e594

5 files changed

Lines changed: 19 additions & 14 deletions

File tree

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ test = [
6969
typecheck = [
7070
"pyright",
7171
"typing_extensions",
72+
"chardet",
73+
"cryptography",
74+
"PyOpenSSL",
7275
]
7376

7477
[tool.setuptools]

src/requests/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
charset_normalizer_version = None
5353

5454
try:
55-
from chardet import __version__ as chardet_version # type: ignore[import-not-found]
55+
from chardet import __version__ as chardet_version
5656
except ImportError:
5757
chardet_version = None
5858

@@ -112,7 +112,7 @@ def _check_cryptography(cryptography_version: str) -> None:
112112
try:
113113
check_compatibility(
114114
urllib3.__version__, # type: ignore[reportPrivateImportUsage]
115-
chardet_version, # type: ignore[reportUnknownArgumentType]
115+
chardet_version,
116116
charset_normalizer_version,
117117
)
118118
except (AssertionError, ValueError):
@@ -138,11 +138,11 @@ def _check_cryptography(cryptography_version: str) -> None:
138138
pyopenssl.inject_into_urllib3()
139139

140140
# Check cryptography version
141-
from cryptography import ( # type: ignore[reportMissingImports]
142-
__version__ as cryptography_version, # type: ignore[reportUnknownVariableType]
141+
from cryptography import (
142+
__version__ as cryptography_version,
143143
)
144144

145-
_check_cryptography(cryptography_version) # type: ignore[reportUnknownArgumentType]
145+
_check_cryptography(cryptography_version)
146146
except ImportError:
147147
pass
148148

src/requests/compat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import importlib
1515
import sys
1616
from types import ModuleType
17+
from typing import TYPE_CHECKING
1718

1819
# -------
1920
# urllib3
@@ -46,7 +47,10 @@ def _resolve_char_detection() -> ModuleType | None:
4647
return chardet
4748

4849

49-
chardet = _resolve_char_detection()
50+
if TYPE_CHECKING:
51+
import chardet
52+
else:
53+
chardet = _resolve_char_detection()
5054

5155
# -------
5256
# Pythons

src/requests/help.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""Module containing bug report helper(s)."""
22

3-
# pyright: reportUnknownMemberType=false
4-
53
import json
64
import platform
75
import ssl
@@ -19,7 +17,7 @@
1917
charset_normalizer = None
2018

2119
try:
22-
import chardet # type: ignore[import-not-found]
20+
import chardet
2321
except ImportError:
2422
chardet = None
2523

@@ -30,8 +28,8 @@
3028
OpenSSL = None
3129
cryptography = None
3230
else:
33-
import cryptography # type: ignore[import-not-found]
34-
import OpenSSL # type: ignore[import-not-found]
31+
import cryptography
32+
import OpenSSL
3533

3634

3735
def _implementation():
@@ -51,7 +49,7 @@ def _implementation():
5149
implementation_version = platform.python_version()
5250
elif implementation == "PyPy":
5351
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]
5553
if sys.pypy_version_info.releaselevel != "final": # type: ignore[attr-defined]
5654
implementation_version = "".join(
5755
[implementation_version, sys.pypy_version_info.releaselevel] # type: ignore[attr-defined]

src/requests/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def _encode_files(
238238
fdata = fp
239239
elif isinstance(fp, _SupportsRead): # type: ignore[reportUnnecessaryIsInstance] # defensive check for untyped callers
240240
fdata = fp.read()
241-
elif fp is None: # type: ignore[reportUnnecessaryComparison] # defensive check for untyped callers
241+
elif fp is None: # defensive check for untyped callers
242242
continue
243243
else:
244244
fdata = fp
@@ -1010,7 +1010,7 @@ def iter_lines(
10101010
):
10111011
if pending is not None:
10121012
# TODO: remove cast after iter_lines rewrite
1013-
chunk = cast("str | bytes", pending + chunk) # type: ignore[operator]
1013+
chunk = cast("str | bytes", pending + chunk)
10141014

10151015
if delimiter:
10161016
lines = chunk.split(delimiter) # type: ignore[arg-type]

0 commit comments

Comments
 (0)