Skip to content

Commit

Permalink
Enable flake8 for most files, remove py2 remnants (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Oct 24, 2022
1 parent 5c2719d commit 3da92a4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 64 deletions.
11 changes: 5 additions & 6 deletions .flake8
@@ -1,15 +1,14 @@
[flake8]
max-line-length = 99
ignore = W503
ignore =
W503
E266 # too many leading '#' for block comment
exclude =
.git
.tox
venv*

# pending revision
cssselect/__init__.py
cssselect/parser.py
cssselect/xpath.py
docs/conf.py
setup.py
tests/test_cssselect.py
per-file-ignores =
cssselect/__init__.py:F401
12 changes: 2 additions & 10 deletions cssselect/parser.py
Expand Up @@ -17,14 +17,6 @@
import operator


if sys.version_info[0] < 3:
_unicode = unicode
_unichr = unichr
else:
_unicode = str
_unichr = chr


def ascii_lower(string):
"""Lower-case, but only in the ASCII range."""
return string.encode("utf8").lower().decode("utf8")
Expand Down Expand Up @@ -617,7 +609,7 @@ def parse_simple_selector(stream, inside_negation=False):
if ident.lower() in ("first-line", "first-letter", "before", "after"):
# Special case: CSS 2.1 pseudo-elements can have a single ':'
# Any new pseudo-element must have two.
pseudo_element = _unicode(ident)
pseudo_element = str(ident)
continue
if stream.peek() != ("DELIM", "("):
result = Pseudo(result, ident)
Expand Down Expand Up @@ -876,7 +868,7 @@ def _replace_unicode(match):
codepoint = int(match.group(1), 16)
if codepoint > sys.maxunicode:
codepoint = 0xFFFD
return _unichr(codepoint)
return chr(codepoint)


def unescape_ident(value):
Expand Down
18 changes: 4 additions & 14 deletions cssselect/xpath.py
Expand Up @@ -12,21 +12,11 @@
"""

import sys
import re
import copy

from cssselect.parser import parse, parse_series, SelectorError


if sys.version_info[0] < 3:
_basestring = basestring
_unicode = unicode
else:
_basestring = str
_unicode = str


def _unicode_safe_getattr(obj, name, default=None):
# getattr() with a non-ASCII name fails on Python 2.x
name = name.encode("ascii", "replace").decode("ascii")
Expand All @@ -47,7 +37,7 @@ def __init__(self, path="", element="*", condition="", star_prefix=False):
self.condition = condition

def __str__(self):
path = _unicode(self.path) + _unicode(self.element)
path = str(self.path) + str(self.element)
if self.condition:
path += "[%s]" % self.condition
return path
Expand Down Expand Up @@ -77,7 +67,7 @@ def add_star_prefix(self):
self.path += "*/"

def join(self, combiner, other, closing_combiner=None, has_inner_condition=False):
path = _unicode(self) + combiner
path = str(self) + combiner
# Any "star prefix" is redundant when joining.
if other.path != "*/":
path += other.path
Expand Down Expand Up @@ -230,7 +220,7 @@ def selector_to_xpath(
assert isinstance(xpath, self.xpathexpr_cls) # help debug a missing 'return'
if translate_pseudo_elements and selector.pseudo_element:
xpath = self.xpath_pseudo_element(xpath, selector.pseudo_element)
return (prefix or "") + _unicode(xpath)
return (prefix or "") + str(xpath)

def xpath_pseudo_element(self, xpath, pseudo_element):
"""Translate a pseudo-element.
Expand All @@ -243,7 +233,7 @@ def xpath_pseudo_element(self, xpath, pseudo_element):

@staticmethod
def xpath_literal(s):
s = _unicode(s)
s = str(s)
if "'" not in s:
s = "'%s'" % s
elif '"' not in s:
Expand Down
48 changes: 14 additions & 34 deletions tests/test_cssselect.py
Expand Up @@ -28,33 +28,20 @@
SelectorSyntaxError,
ExpressionError,
)
from cssselect.parser import tokenize, parse_series, _unicode, FunctionalPseudoElement
from cssselect.parser import tokenize, parse_series, FunctionalPseudoElement
from cssselect.xpath import _unicode_safe_getattr, XPathExpr


if sys.version_info[0] < 3:
# Python 2
def u(text):
return text.decode("utf8")

else:
# Python 3
def u(text):
return text


class TestCssselect(unittest.TestCase):
def test_tokenizer(self):
tokens = [
_unicode(item) for item in tokenize(u(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)'))
]
tokens = [str(item) for item in tokenize(r'E\ é > f [a~="y\"x"]:nth(/* fu /]* */-3.7)')]
assert tokens == [
u("<IDENT 'E é' at 0>"),
"<IDENT 'E é' at 0>",
"<S ' ' at 4>",
"<DELIM '>' at 5>",
"<S ' ' at 6>",
# the no-break space is not whitespace in CSS
u("<IDENT 'f ' at 7>"), # f\xa0
"<IDENT 'f ' at 7>", # f\xa0
"<DELIM '[' at 9>",
"<IDENT 'a' at 10>",
"<DELIM '~' at 11>",
Expand Down Expand Up @@ -178,9 +165,9 @@ def parse_pseudo(css):
result = []
for selector in parse(css):
pseudo = selector.pseudo_element
pseudo = _unicode(pseudo) if pseudo else pseudo
pseudo = str(pseudo) if pseudo else pseudo
# No Symbol here
assert pseudo is None or type(pseudo) is _unicode
assert pseudo is None or type(pseudo) is str
selector = repr(selector.parsed_tree).replace("(u'", "('")
result.append((selector, pseudo))
return result
Expand Down Expand Up @@ -409,7 +396,7 @@ def get_error(css):

def test_translation(self):
def xpath(css):
return _unicode(GenericTranslator().css_to_xpath(css, prefix=""))
return str(GenericTranslator().css_to_xpath(css, prefix=""))

assert xpath("*") == "*"
assert xpath("e") == "e"
Expand Down Expand Up @@ -511,12 +498,12 @@ def xpath(css):
assert xpath("e:where(foo, bar)") == "e[(name() = 'foo') or (name() = 'bar')]"

# Invalid characters in XPath element names
assert xpath(r"di\a0 v") == (u("*[name() = 'di v']")) # di\xa0v
assert xpath(r"di\a0 v") == ("*[name() = 'di v']") # di\xa0v
assert xpath(r"di\[v") == ("*[name() = 'di[v']")
assert xpath(r"[h\a0 ref]") == (u("*[attribute::*[name() = 'h ref']]")) # h\xa0ref
assert xpath(r"[h\a0 ref]") == ("*[attribute::*[name() = 'h ref']]") # h\xa0ref
assert xpath(r"[h\]ref]") == ("*[attribute::*[name() = 'h]ref']]")

self.assertRaises(ExpressionError, xpath, u(":fİrst-child"))
self.assertRaises(ExpressionError, xpath, ":fİrst-child")
self.assertRaises(ExpressionError, xpath, ":first-of-type")
self.assertRaises(ExpressionError, xpath, ":only-of-type")
self.assertRaises(ExpressionError, xpath, ":last-of-type")
Expand All @@ -531,11 +518,7 @@ def xpath(css):
self.assertRaises(TypeError, GenericTranslator().selector_to_xpath, "foo")

def test_unicode(self):
if sys.version_info[0] < 3:
css = ".a\xc1b".decode("ISO-8859-1")
else:
css = ".a\xc1b"

css = ".a\xc1b"
xpath = GenericTranslator().css_to_xpath(css)
assert css[1:] in xpath
xpath = xpath.encode("ascii", "xmlcharrefreplace").decode("ASCII")
Expand Down Expand Up @@ -638,7 +621,7 @@ def xpath_first_or_second_pseudo(self, xpath):
return xpath.add_condition("@id = 'first' or @id = 'second'")

def xpath(css):
return _unicode(CustomTranslator().css_to_xpath(css))
return str(CustomTranslator().css_to_xpath(css))

assert xpath(":five-attributes") == "descendant-or-self::*[count(@*)=5]"
assert xpath(":nb-attr(3)") == "descendant-or-self::*[count(@*)=3]"
Expand Down Expand Up @@ -970,10 +953,7 @@ def test_select_shakespeare(self):
body = document.xpath("//body")[0]
css_to_xpath = GenericTranslator().css_to_xpath

try:
basestring_ = basestring
except NameError:
basestring_ = (str, bytes)
basestring_ = (str, bytes)

def count(selector):
xpath = css_to_xpath(selector)
Expand Down Expand Up @@ -1425,7 +1405,7 @@ def count(selector):
</div>
</body>
</html>
"""
""" # noqa: W191,E101


if __name__ == "__main__":
Expand Down

0 comments on commit 3da92a4

Please sign in to comment.