Skip to content

Commit 8e69489

Browse files
authored
Remove redundant code and fix pylint (#175)
1 parent 2d76c99 commit 8e69489

File tree

10 files changed

+20
-31
lines changed

10 files changed

+20
-31
lines changed

setup.cfg

Lines changed: 0 additions & 2 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
include_package_data=True,
1414
zip_safe=False,
1515
platforms=["Any"],
16+
python_requires=">=3.6",
1617
classifiers=[
1718
"Development Status :: 5 - Production/Stable",
1819
"License :: OSI Approved :: BSD License",

tests/test_encoding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _assert_encoding(self, content_type, body, expected_encoding, expected_unico
149149
else:
150150
self.assertTrue(
151151
body_unicode in expected_unicode,
152-
"%s is not in %s" % (body_unicode, expected_unicode),
152+
f"{body_unicode} is not in {expected_unicode}",
153153
)
154154

155155
def test_content_type_and_conversion(self):

tests/test_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_missing_semicolon(self):
124124
):
125125
self.assertEqual(replace_entities(entity, encoding="cp1252"), result)
126126
self.assertEqual(
127-
replace_entities("x%sy" % entity, encoding="cp1252"), "x%sy" % result
127+
replace_entities(f"x{entity}y", encoding="cp1252"), f"x{result}y"
128128
)
129129

130130
def test_encoding(self):

tests/test_url.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,8 @@ def test_safe_url_idna_encoding_failure(self):
266266

267267
# DNS label too long
268268
self.assertEqual(
269-
safe_url_string(
270-
"http://www.{label}.com/résumé?q=résumé".format(label="example" * 11)
271-
),
272-
"http://www.{label}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9".format(
273-
label="example" * 11
274-
),
269+
safe_url_string(f"http://www.{'example' * 11}.com/résumé?q=résumé"),
270+
f"http://www.{'example' * 11}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9",
275271
)
276272

277273
def test_safe_url_port_number(self):
@@ -971,12 +967,8 @@ def test_canonicalize_url_idna_exceptions(self):
971967

972968
# DNS label too long
973969
self.assertEqual(
974-
canonicalize_url(
975-
"http://www.{label}.com/résumé?q=résumé".format(label="example" * 11)
976-
),
977-
"http://www.{label}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9".format(
978-
label="example" * 11
979-
),
970+
canonicalize_url(f"http://www.{'example' * 11}.com/résumé?q=résumé"),
971+
f"http://www.{'example' * 11}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9",
980972
)
981973

982974
def test_preserve_nonfragment_hash(self):
@@ -1033,7 +1025,7 @@ def test_bytes_uri(self):
10331025

10341026
def test_unicode_uri(self):
10351027
result = parse_data_uri("data:,é")
1036-
self.assertEqual(result.data, "é".encode("utf-8"))
1028+
self.assertEqual(result.data, "é".encode())
10371029

10381030
def test_default_mediatype(self):
10391031
result = parse_data_uri("data:;charset=iso-8859-7,%be%d3%be")

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py27, pypy, py35, py36, py37, py38, py39, py310, pypy3, docs, security, flake8, pylint, black
7+
envlist = py36, py37, py38, py39, py310, pypy3, docs, security, flake8, pylint, black
88

99
[testenv]
1010
deps =

w3lib/encoding.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
Functions for handling encoding of web pages
33
"""
44
import re, codecs, encodings
5-
from sys import version_info
65
from typing import Callable, Match, Optional, Tuple, Union, cast
76
from w3lib._types import AnyUnicodeError, StrOrBytes
8-
from w3lib.util import to_native_str
7+
import w3lib.util
98

109
_HEADER_ENCODING_RE = re.compile(r"charset=([\w-]+)", re.I)
1110

@@ -46,6 +45,7 @@ def http_content_type_encoding(content_type: Optional[str]) -> Optional[str]:
4645
_XML_ENCODING_RE = _TEMPLATE % ("encoding", r"(?P<xmlcharset>[\w-]+)")
4746

4847
# check for meta tags, or xml decl. and stop search if a body tag is encountered
48+
# pylint: disable=consider-using-f-string
4949
_BODY_ENCODING_PATTERN = (
5050
r"<\s*(?:meta%s(?:(?:\s+%s|\s+%s){2}|\s+%s)|\?xml\s[^>]+%s|body)"
5151
% (_SKIP_ATTRS, _HTTPEQUIV_RE, _CONTENT_RE, _CONTENT2_RE, _XML_ENCODING_RE)
@@ -93,7 +93,7 @@ def html_body_declared_encoding(html_body_str: StrOrBytes) -> Optional[str]:
9393
or match.group("xmlcharset")
9494
)
9595
if encoding:
96-
return resolve_encoding(to_native_str(encoding))
96+
return resolve_encoding(w3lib.util.to_unicode(encoding))
9797

9898
return None
9999

@@ -163,7 +163,7 @@ def resolve_encoding(encoding_alias: str) -> Optional[str]:
163163
(codecs.BOM_UTF16_LE, "utf-16-le"),
164164
(codecs.BOM_UTF8, "utf-8"),
165165
]
166-
_FIRST_CHARS = set(c[0] for (c, _) in _BOM_TABLE)
166+
_FIRST_CHARS = {c[0] for (c, _) in _BOM_TABLE}
167167

168168

169169
def read_bom(data: bytes) -> Union[Tuple[None, None], Tuple[str, bytes]]:
@@ -208,9 +208,7 @@ def to_unicode(data_str: bytes, encoding: str) -> str:
208208
Characters that cannot be converted will be converted to ``\\ufffd`` (the
209209
unicode replacement character).
210210
"""
211-
return data_str.decode(
212-
encoding, "replace" if version_info[0:2] >= (3, 3) else "w3lib_replace"
213-
)
211+
return data_str.decode(encoding, "replace")
214212

215213

216214
def html_to_unicode(

w3lib/html.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,7 @@ def remove_tags_with_content(
228228

229229
utext = to_unicode(text, encoding)
230230
if which_ones:
231-
tags = "|".join(
232-
[r"<%s\b.*?</%s>|<%s\s*/>" % (tag, tag, tag) for tag in which_ones]
233-
)
231+
tags = "|".join([fr"<{tag}\b.*?</{tag}>|<{tag}\s*/>" for tag in which_ones])
234232
retags = re.compile(tags, re.DOTALL | re.IGNORECASE)
235233
utext = retags.sub("", utext)
236234
return utext

w3lib/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from base64 import urlsafe_b64encode
22
from typing import Any, List, MutableMapping, Optional, AnyStr, Sequence, Union, Mapping
3-
from w3lib.util import to_bytes, to_native_str
3+
from w3lib.util import to_bytes, to_unicode
44

55
HeadersDictInput = Mapping[bytes, Union[Any, Sequence]]
66
HeadersDictOutput = MutableMapping[bytes, List[bytes]]
@@ -97,7 +97,7 @@ def basic_auth_header(
9797
9898
"""
9999

100-
auth = "%s:%s" % (to_native_str(username), to_native_str(password))
100+
auth = f"{to_unicode(username)}:{to_unicode(password)}"
101101
# XXX: RFC 2617 doesn't define encoding, but ISO-8859-1
102102
# seems to be the most widely used encoding here. See also:
103103
# http://greenbytes.de/tech/webdav/draft-ietf-httpauth-basicauth-enc-latest.html

w3lib/url.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def path_to_file_uri(path: str) -> str:
319319
x = pathname2url(os.path.abspath(path))
320320
if os.name == "nt":
321321
x = x.replace("|", ":") # http://bugs.python.org/issue5861
322-
return "file:///%s" % x.lstrip("/")
322+
return f"file:///{x.lstrip('/')}"
323323

324324

325325
def file_uri_to_path(uri: str) -> str:
@@ -344,6 +344,7 @@ def any_to_uri(uri_or_path: str) -> str:
344344
_char = set(map(chr, range(127)))
345345

346346
# RFC 2045 token.
347+
# pylint: disable=consider-using-f-string
347348
_token = r"[{}]+".format(
348349
re.escape(
349350
"".join(
@@ -359,6 +360,7 @@ def any_to_uri(uri_or_path: str) -> str:
359360
)
360361

361362
# RFC 822 quoted-string, without surrounding quotation marks.
363+
# pylint: disable=consider-using-f-string
362364
_quoted_string = r"(?:[{}]|(?:\\[{}]))*".format(
363365
re.escape("".join(_char - {'"', "\\", "\r"})), re.escape("".join(_char))
364366
)

0 commit comments

Comments
 (0)