Skip to content

Commit

Permalink
Address new issues reported by pylint (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmherbst committed Oct 13, 2021
1 parent eb46579 commit 13abef2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def load_selector(filename, **kwargs):
input_path = os.path.join(os.path.dirname(__file__), '_static', filename)
with open(input_path) as input_file:
with open(input_path, encoding="utf-8") as input_file:
return Selector(text=input_file.read(), **kwargs)


Expand Down
18 changes: 6 additions & 12 deletions parsel/csstranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __str__(self):
if self.attribute is not None:
if path.endswith("::*/*"):
path = path[:-2]
path += "/@%s" % self.attribute
path += f"/@{self.attribute}"

return path

Expand All @@ -58,24 +58,19 @@ def xpath_pseudo_element(self, xpath, pseudo_element):
Dispatch method that transforms XPath to support pseudo-element
"""
if isinstance(pseudo_element, FunctionalPseudoElement):
method = "xpath_%s_functional_pseudo_element" % (
pseudo_element.name.replace("-", "_")
)
method = f"xpath_{pseudo_element.name.replace('-', '_')}_functional_pseudo_element"
method = _unicode_safe_getattr(self, method, None)
if not method:
raise ExpressionError(
"The functional pseudo-element ::%s() is unknown"
% pseudo_element.name
f"The functional pseudo-element ::{pseudo_element.name}() is unknown"
)
xpath = method(xpath, pseudo_element)
else:
method = "xpath_%s_simple_pseudo_element" % (
pseudo_element.replace("-", "_")
)
method = f"xpath_{pseudo_element.replace('-', '_')}_simple_pseudo_element"
method = _unicode_safe_getattr(self, method, None)
if not method:
raise ExpressionError(
"The pseudo-element ::%s is unknown" % pseudo_element
f"The pseudo-element ::{pseudo_element} is unknown"
)
xpath = method(xpath)
return xpath
Expand All @@ -84,8 +79,7 @@ def xpath_attr_functional_pseudo_element(self, xpath, function):
"""Support selecting attribute values using ::attr() pseudo-element"""
if function.argument_types() not in (["STRING"], ["IDENT"]):
raise ExpressionError(
"Expected a single string or ident for ::attr(), got %r"
% function.arguments
f"Expected a single string or ident for ::attr(), got {function.arguments!r}"
)
return XPathExpr.from_xpath(xpath, attribute=function.arguments[0].value)

Expand Down
4 changes: 2 additions & 2 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _st(st: Optional[str]) -> str:
elif st in _ctgroup:
return st
else:
raise ValueError("Invalid type: %s" % st)
raise ValueError(f"Invalid type: {st}")


def create_root_node(text, parser_cls, base_url=None):
Expand Down Expand Up @@ -274,7 +274,7 @@ def __init__(

if text is not None:
if not isinstance(text, str):
msg = "text argument should be of type str, got %s" % (text.__class__)
msg = f"text argument should be of type str, got {text.__class__}"
raise TypeError(msg)
root = self._get_root(text, base_url)
elif root is None:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from setuptools import setup, __version__ as setuptools_version


with open("README.rst") as readme_file:
with open("README.rst", encoding="utf-8") as readme_file:
readme = readme_file.read()

with open("NEWS") as history_file:
with open("NEWS", encoding="utf-8") as history_file:
history = history_file.read().replace(".. :changelog:", "")

setup(
Expand Down
14 changes: 6 additions & 8 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,17 @@ def test_accessing_attributes(self) -> None:
)

def test_representation_slice(self) -> None:
body = "<p><input name='{}' value='\xa9'/></p>".format(50 * "b")
body = f"<p><input name='{50 * 'b'}' value='\xa9'/></p>"
sel = self.sscls(text=body)

representation = "<Selector xpath='//input/@name' data='{}...'>".format(
37 * "b"
)
representation = f"<Selector xpath='//input/@name' data='{37 * 'b'}...'>"

self.assertEqual(
[repr(it) for it in sel.xpath("//input/@name")], [representation]
)

def test_representation_unicode_query(self) -> None:
body = "<p><input name='{}' value='\xa9'/></p>".format(50 * "b")
body = f"<p><input name='{50 * 'b'}' value='\xa9'/></p>"

representation = "<Selector xpath='//input[@value=\"©\"]/@value' data='©'>"

Expand Down Expand Up @@ -807,9 +805,9 @@ def test_weakref_slots(self) -> None:
"""Check that classes are using slots and are weak-referenceable"""
x = self.sscls(text="")
weakref.ref(x)
assert not hasattr(x, "__dict__"), (
"%s does not use __slots__" % x.__class__.__name__
)
assert not hasattr(
x, "__dict__"
), f"{x.__class__.__name__} does not use __slots__"

def test_remove_namespaces(self) -> None:
xml = """<?xml version="1.0" encoding="UTF-8"?>
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ commands =
[testenv:pylint]
deps =
{[testenv]deps}
pylint
pylint==2.11.1
commands =
pylint docs parsel tests conftest.py setup.py

Expand Down

0 comments on commit 13abef2

Please sign in to comment.