Skip to content

Commit 3b600b1

Browse files
committed
Typing improvements.
1 parent 0329304 commit 3b600b1

File tree

6 files changed

+64
-30
lines changed

6 files changed

+64
-30
lines changed

.flake8

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
11
[flake8]
2-
ignore =
3-
# Refers to the max-line length. Let's suppress the error and simply
4-
# let black take care on how it wants to format the lines.
5-
E501,
6-
7-
# Refers to "line break before/after binary operator".
8-
# Similar to above, let black take care of the formatting.
9-
W503,
10-
W504,
11-
12-
# black disagrees with flake8, and inserts whitespace
13-
# E203: whitespace before ':'
14-
E203,
2+
ignore = E203, E501, E701, E704, W503, W504

mypy.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[mypy]
22
exclude = .*flycheck_.*
3-
show_error_codes = True
43
check_untyped_defs = True
54

65
[mypy-w3lib.*]

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ commands =
1919
[testenv:typing]
2020
basepython = python3
2121
deps =
22-
# mypy would error if pytest (or its sub) not found
22+
# mypy would error if pytest (or its stub) not found
2323
pytest
24-
mypy==1.0.0
24+
mypy==1.10.0
2525
commands =
2626
mypy --strict {posargs: w3lib tests}
2727

w3lib/html.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import re
66
from html.entities import name2codepoint
7-
from typing import AnyStr, Iterable, Match, Optional, Pattern, Tuple, Union
7+
from typing import Iterable, Match, Optional, Pattern, Tuple, Union
88
from urllib.parse import urljoin
99

1010
from w3lib._types import StrOrBytes
@@ -34,7 +34,7 @@
3434

3535

3636
def replace_entities(
37-
text: AnyStr,
37+
text: StrOrBytes,
3838
keep: Iterable[str] = (),
3939
remove_illegal: bool = True,
4040
encoding: str = "utf-8",
@@ -99,11 +99,13 @@ def convert_entity(m: Match[str]) -> str:
9999
return _ent_re.sub(convert_entity, to_unicode(text, encoding))
100100

101101

102-
def has_entities(text: AnyStr, encoding: Optional[str] = None) -> bool:
102+
def has_entities(text: StrOrBytes, encoding: Optional[str] = None) -> bool:
103103
return bool(_ent_re.search(to_unicode(text, encoding)))
104104

105105

106-
def replace_tags(text: AnyStr, token: str = "", encoding: Optional[str] = None) -> str:
106+
def replace_tags(
107+
text: StrOrBytes, token: str = "", encoding: Optional[str] = None
108+
) -> str:
107109
"""Replace all markup tags found in the given `text` by the given token.
108110
By default `token` is an empty string so it just removes all tags.
109111
@@ -129,7 +131,7 @@ def replace_tags(text: AnyStr, token: str = "", encoding: Optional[str] = None)
129131
_REMOVECOMMENTS_RE = re.compile("<!--.*?(?:-->|$)", re.DOTALL)
130132

131133

132-
def remove_comments(text: AnyStr, encoding: Optional[str] = None) -> str:
134+
def remove_comments(text: StrOrBytes, encoding: Optional[str] = None) -> str:
133135
"""Remove HTML Comments.
134136
135137
>>> import w3lib.html
@@ -144,7 +146,7 @@ def remove_comments(text: AnyStr, encoding: Optional[str] = None) -> str:
144146

145147

146148
def remove_tags(
147-
text: AnyStr,
149+
text: StrOrBytes,
148150
which_ones: Iterable[str] = (),
149151
keep: Iterable[str] = (),
150152
encoding: Optional[str] = None,
@@ -216,7 +218,7 @@ def remove_tag(m: Match[str]) -> str:
216218

217219

218220
def remove_tags_with_content(
219-
text: AnyStr, which_ones: Iterable[str] = (), encoding: Optional[str] = None
221+
text: StrOrBytes, which_ones: Iterable[str] = (), encoding: Optional[str] = None
220222
) -> str:
221223
"""Remove tags and their content.
222224
@@ -240,7 +242,7 @@ def remove_tags_with_content(
240242

241243

242244
def replace_escape_chars(
243-
text: AnyStr,
245+
text: StrOrBytes,
244246
which_ones: Iterable[str] = ("\n", "\t", "\r"),
245247
replace_by: StrOrBytes = "",
246248
encoding: Optional[str] = None,
@@ -262,7 +264,7 @@ def replace_escape_chars(
262264

263265

264266
def unquote_markup(
265-
text: AnyStr,
267+
text: StrOrBytes,
266268
keep: Iterable[str] = (),
267269
remove_illegal: bool = True,
268270
encoding: Optional[str] = None,
@@ -304,7 +306,7 @@ def _get_fragments(
304306

305307

306308
def get_base_url(
307-
text: AnyStr, baseurl: StrOrBytes = "", encoding: str = "utf-8"
309+
text: StrOrBytes, baseurl: StrOrBytes = "", encoding: str = "utf-8"
308310
) -> str:
309311
"""Return the base url if declared in the given HTML `text`,
310312
relative to the given base url.
@@ -324,7 +326,7 @@ def get_base_url(
324326

325327

326328
def get_meta_refresh(
327-
text: AnyStr,
329+
text: StrOrBytes,
328330
baseurl: str = "",
329331
encoding: str = "utf-8",
330332
ignore_tags: Iterable[str] = ("script", "noscript"),

w3lib/http.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
from base64 import b64encode
2-
from typing import Any, AnyStr, List, Mapping, MutableMapping, Optional, Sequence, Union
3-
2+
from typing import (
3+
Any,
4+
List,
5+
Mapping,
6+
MutableMapping,
7+
Optional,
8+
Sequence,
9+
Union,
10+
overload,
11+
)
12+
13+
from w3lib._types import StrOrBytes
414
from w3lib.util import to_bytes, to_unicode
515

616
HeadersDictInput = Mapping[bytes, Union[Any, Sequence[bytes]]]
717
HeadersDictOutput = MutableMapping[bytes, List[bytes]]
818

919

20+
@overload
21+
def headers_raw_to_dict(headers_raw: bytes) -> HeadersDictOutput: ...
22+
23+
24+
@overload
25+
def headers_raw_to_dict(headers_raw: None) -> None: ...
26+
27+
1028
def headers_raw_to_dict(headers_raw: Optional[bytes]) -> Optional[HeadersDictOutput]:
1129
r"""
1230
Convert raw headers (single multi-line bytestring)
@@ -52,6 +70,14 @@ def headers_raw_to_dict(headers_raw: Optional[bytes]) -> Optional[HeadersDictOut
5270
return result_dict
5371

5472

73+
@overload
74+
def headers_dict_to_raw(headers_dict: HeadersDictInput) -> bytes: ...
75+
76+
77+
@overload
78+
def headers_dict_to_raw(headers_dict: None) -> None: ...
79+
80+
5581
def headers_dict_to_raw(headers_dict: Optional[HeadersDictInput]) -> Optional[bytes]:
5682
r"""
5783
Returns a raw HTTP headers representation of headers
@@ -85,7 +111,7 @@ def headers_dict_to_raw(headers_dict: Optional[HeadersDictInput]) -> Optional[by
85111

86112

87113
def basic_auth_header(
88-
username: AnyStr, password: AnyStr, encoding: str = "ISO-8859-1"
114+
username: StrOrBytes, password: StrOrBytes, encoding: str = "ISO-8859-1"
89115
) -> bytes:
90116
"""
91117
Return an `Authorization` header field value for `HTTP Basic Access Authentication (RFC 2617)`_

w3lib/url.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Tuple,
2020
Union,
2121
cast,
22+
overload,
2223
)
2324
from urllib.parse import _coerce_args # type: ignore
2425
from urllib.parse import (
@@ -221,6 +222,24 @@ def is_url(text: str) -> bool:
221222
return text.partition("://")[0] in ("file", "http", "https")
222223

223224

225+
@overload
226+
def url_query_parameter(
227+
url: StrOrBytes,
228+
parameter: str,
229+
default: None = None,
230+
keep_blank_values: Union[bool, int] = 0,
231+
) -> Optional[str]: ...
232+
233+
234+
@overload
235+
def url_query_parameter(
236+
url: StrOrBytes,
237+
parameter: str,
238+
default: str,
239+
keep_blank_values: Union[bool, int] = 0,
240+
) -> str: ...
241+
242+
224243
def url_query_parameter(
225244
url: StrOrBytes,
226245
parameter: str,

0 commit comments

Comments
 (0)