diff --git a/.bandit.yml b/.bandit.yml index 7fcde04..4f60a02 100644 --- a/.bandit.yml +++ b/.bandit.yml @@ -1,2 +1,6 @@ skips: - B101 +- B311 +- B320 +- B410 +exclude_dirs: ['tests'] diff --git a/.flake8 b/.flake8 index 8b0608f..2417f2e 100644 --- a/.flake8 +++ b/.flake8 @@ -4,6 +4,7 @@ ignore = W503 # too many leading '#' for block comment E266 + E704 exclude = .git .tox diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..9d2c8f6 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# applying pre-commit hooks to the project +e91101b37f82558db84a6b8ee9a6dba1fd2ae0bb \ No newline at end of file diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 847d788..5b6cfbf 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -7,18 +7,9 @@ jobs: strategy: matrix: include: - - python-version: 3.12 - env: - TOXENV: black - - python-version: 3.12 - env: - TOXENV: flake8 - python-version: 3.12 env: TOXENV: pylint - - python-version: 3.12 - env: - TOXENV: security - python-version: 3.12 env: TOXENV: docs @@ -40,3 +31,9 @@ jobs: pip install -U pip pip install -U tox tox + + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: pre-commit/action@v3.0.0 diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..6860bdb --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,2 @@ +[settings] +profile = black \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..a27d3db --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +repos: +- repo: https://github.com/PyCQA/bandit + rev: 1.7.8 + hooks: + - id: bandit + args: [-r, -c, .bandit.yml] +- repo: https://github.com/PyCQA/flake8 + rev: 7.0.0 + hooks: + - id: flake8 +- repo: https://github.com/psf/black.git + rev: 24.3.0 + hooks: + - id: black +- repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort \ No newline at end of file diff --git a/cssselect/__init__.py b/cssselect/__init__.py index 77f028b..a59995c 100644 --- a/cssselect/__init__.py +++ b/cssselect/__init__.py @@ -14,13 +14,13 @@ """ from cssselect.parser import ( - parse, - Selector, FunctionalPseudoElement, + Selector, SelectorError, SelectorSyntaxError, + parse, ) -from cssselect.xpath import GenericTranslator, HTMLTranslator, ExpressionError +from cssselect.xpath import ExpressionError, GenericTranslator, HTMLTranslator __all__ = ( "ExpressionError", diff --git a/cssselect/parser.py b/cssselect/parser.py index 25a650c..354713d 100644 --- a/cssselect/parser.py +++ b/cssselect/parser.py @@ -12,9 +12,9 @@ """ -import sys -import re import operator +import re +import sys import typing from typing import Iterable, Iterator, List, Optional, Sequence, Tuple, Union @@ -67,9 +67,13 @@ class Selector: """ - def __init__(self, tree: Tree, pseudo_element: Optional[PseudoElement] = None) -> None: + def __init__( + self, tree: Tree, pseudo_element: Optional[PseudoElement] = None + ) -> None: self.parsed_tree = tree - if pseudo_element is not None and not isinstance(pseudo_element, FunctionalPseudoElement): + if pseudo_element is not None and not isinstance( + pseudo_element, FunctionalPseudoElement + ): pseudo_element = ascii_lower(pseudo_element) #: A :class:`FunctionalPseudoElement`, #: or the identifier for the pseudo-element as a string, @@ -247,7 +251,11 @@ def __init__(self, selector: Tree, subselector: Tree) -> None: self.subselector = subselector def __repr__(self) -> str: - return "%s[%r:not(%r)]" % (self.__class__.__name__, self.selector, self.subselector) + return "%s[%r:not(%r)]" % ( + self.__class__.__name__, + self.selector, + self.subselector, + ) def canonical(self) -> str: subsel = self.subselector.canonical() @@ -317,7 +325,10 @@ def canonical(self) -> str: for s in self.selector_list: selarg = s.canonical() selector_arguments.append(selarg.lstrip("*")) - return "%s:is(%s)" % (self.selector.canonical(), ", ".join(map(str, selector_arguments))) + return "%s:is(%s)" % ( + self.selector.canonical(), + ", ".join(map(str, selector_arguments)), + ) def specificity(self) -> Tuple[int, int, int]: return max(x.specificity() for x in self.selector_list) @@ -367,14 +378,17 @@ def __init__( attrib: str, operator: 'typing.Literal["exists"]', value: None, - ) -> None: - ... + ) -> None: ... @typing.overload def __init__( - self, selector: Tree, namespace: Optional[str], attrib: str, operator: str, value: "Token" - ) -> None: - ... + self, + selector: Tree, + namespace: Optional[str], + attrib: str, + operator: str, + value: "Token", + ) -> None: ... def __init__( self, @@ -415,7 +429,11 @@ def canonical(self) -> str: if self.operator == "exists": op = attrib else: - op = "%s%s%s" % (attrib, self.operator, typing.cast("Token", self.value).css()) + op = "%s%s%s" % ( + attrib, + self.operator, + typing.cast("Token", self.value).css(), + ) return "%s[%s]" % (self.selector.canonical(), op) @@ -433,7 +451,9 @@ class Element: """ - def __init__(self, namespace: Optional[str] = None, element: Optional[str] = None) -> None: + def __init__( + self, namespace: Optional[str] = None, element: Optional[str] = None + ) -> None: self.namespace = namespace self.element = element @@ -486,7 +506,12 @@ def __repr__(self) -> str: comb = "" else: comb = self.combinator - return "%s[%r %s %r]" % (self.__class__.__name__, self.selector, comb, self.subselector) + return "%s[%r %s %r]" % ( + self.__class__.__name__, + self.selector, + comb, + self.subselector, + ) def canonical(self) -> str: subsel = self.subselector.canonical() @@ -509,7 +534,9 @@ def specificity(self) -> Tuple[int, int, int]: _id_re = re.compile(r"^[ \t\r\n\f]*([a-zA-Z]*)#([a-zA-Z0-9_-]+)[ \t\r\n\f]*$") # foo.bar or .bar -_class_re = re.compile(r"^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$") +_class_re = re.compile( + r"^[ \t\r\n\f]*([a-zA-Z]*)\.([a-zA-Z][a-zA-Z0-9_-]*)[ \t\r\n\f]*$" +) def parse(css: str) -> List[Selector]: @@ -536,7 +563,9 @@ def parse(css: str) -> List[Selector]: return [Selector(Hash(Element(element=match.group(1) or None), match.group(2)))] match = _class_re.match(css) if match is not None: - return [Selector(Class(Element(element=match.group(1) or None), match.group(2)))] + return [ + Selector(Class(Element(element=match.group(1) or None), match.group(2))) + ] stream = TokenStream(tokenize(css)) stream.source = css @@ -708,7 +737,10 @@ def parse_arguments(stream: "TokenStream") -> List["Token"]: while 1: stream.skip_whitespace() next = stream.next() - if next.type in ("IDENT", "STRING", "NUMBER") or next in [("DELIM", "+"), ("DELIM", "-")]: + if next.type in ("IDENT", "STRING", "NUMBER") or next in [ + ("DELIM", "+"), + ("DELIM", "-"), + ]: arguments.append(next) elif next == ("DELIM", ")"): return arguments @@ -729,7 +761,10 @@ def parse_relative_selector(stream: "TokenStream") -> Tuple["Token", Selector]: combinator = Token("DELIM", " ", pos=0) while 1: - if next.type in ("IDENT", "STRING", "NUMBER") or next in [("DELIM", "."), ("DELIM", "*")]: + if next.type in ("IDENT", "STRING", "NUMBER") or next in [ + ("DELIM", "."), + ("DELIM", "*"), + ]: subselector += typing.cast(str, next.value) elif next == ("DELIM", ")"): result = parse(subselector) @@ -787,7 +822,9 @@ def parse_attrib(selector: Tree, stream: "TokenStream") -> Attrib: return Attrib(selector, namespace, typing.cast(str, attrib), "exists", None) elif next == ("DELIM", "="): op = "=" - elif next.is_delim("^", "$", "*", "~", "|", "!") and (stream.peek() == ("DELIM", "=")): + elif next.is_delim("^", "$", "*", "~", "|", "!") and ( + stream.peek() == ("DELIM", "=") + ): op = typing.cast(str, next.value) + "=" stream.next() else: @@ -850,12 +887,12 @@ def __new__( type_: 'typing.Literal["IDENT", "HASH", "STRING", "S", "DELIM", "NUMBER"]', value: str, pos: int, - ) -> "Token": - ... + ) -> "Token": ... @typing.overload - def __new__(cls, type_: 'typing.Literal["EOF"]', value: None, pos: int) -> "Token": - ... + def __new__( + cls, type_: 'typing.Literal["EOF"]', value: None, pos: int + ) -> "Token": ... def __new__(cls, type_: str, value: Optional[str], pos: int) -> "Token": obj = tuple.__new__(cls, (type_, value)) @@ -910,8 +947,7 @@ class TokenMacros: class MatchFunc(typing.Protocol): def __call__( self, string: str, pos: int = ..., endpos: int = ... - ) -> Optional["re.Match[str]"]: - ... + ) -> Optional["re.Match[str]"]: ... def _compile(pattern: str) -> "MatchFunc": @@ -970,7 +1006,8 @@ def tokenize(s: str) -> Iterator[Token]: match = _match_hash(s, pos=pos) if match: value = _sub_simple_escape( - _replace_simple, _sub_unicode_escape(_replace_unicode, match.group()[1:]) + _replace_simple, + _sub_unicode_escape(_replace_unicode, match.group()[1:]), ) yield Token("HASH", value, pos) pos = match.end() @@ -987,7 +1024,9 @@ def tokenize(s: str) -> Iterator[Token]: raise SelectorSyntaxError("Invalid string at %s" % pos) value = _sub_simple_escape( _replace_simple, - _sub_unicode_escape(_replace_unicode, _sub_newline_escape("", match.group())), + _sub_unicode_escape( + _replace_unicode, _sub_newline_escape("", match.group()) + ), ) yield Token("STRING", value, pos) pos = end_pos + 1 diff --git a/cssselect/xpath.py b/cssselect/xpath.py index fd28c47..4255f66 100644 --- a/cssselect/xpath.py +++ b/cssselect/xpath.py @@ -18,23 +18,23 @@ from typing import Optional from cssselect.parser import ( - parse, - parse_series, - PseudoElement, - Selector, - SelectorError, - Tree, - Element, - Hash, + Attrib, Class, + CombinedSelector, + Element, Function, - Pseudo, - Attrib, + Hash, + Matching, Negation, + Pseudo, + PseudoElement, Relation, - Matching, + Selector, + SelectorError, SpecificityAdjustment, - CombinedSelector, + Tree, + parse, + parse_series, ) @@ -58,7 +58,11 @@ class ExpressionError(SelectorError, RuntimeError): class XPathExpr: def __init__( - self, path: str = "", element: str = "*", condition: str = "", star_prefix: bool = False + self, + path: str = "", + element: str = "*", + condition: str = "", + star_prefix: bool = False, ) -> None: self.path = path self.element = element @@ -84,7 +88,9 @@ def add_name_test(self) -> None: if self.element == "*": # We weren't doing a test anyway return - self.add_condition("name() = %s" % GenericTranslator.xpath_literal(self.element)) + self.add_condition( + "name() = %s" % GenericTranslator.xpath_literal(self.element) + ) self.element = "*" def add_star_prefix(self) -> None: @@ -107,7 +113,9 @@ def join( path += other.path self.path = path if not has_inner_condition: - self.element = other.element + closing_combiner if closing_combiner else other.element + self.element = ( + other.element + closing_combiner if closing_combiner else other.element + ) self.condition = other.condition else: self.element = other.element @@ -259,7 +267,9 @@ def selector_to_xpath( xpath = self.xpath_pseudo_element(xpath, selector.pseudo_element) return (prefix or "") + str(xpath) - def xpath_pseudo_element(self, xpath: XPathExpr, pseudo_element: PseudoElement) -> XPathExpr: + def xpath_pseudo_element( + self, xpath: XPathExpr, pseudo_element: PseudoElement + ) -> XPathExpr: """Translate a pseudo-element. Defaults to not supporting pseudo-elements at all, @@ -300,7 +310,8 @@ def xpath_combinedselector(self, combined: CombinedSelector) -> XPathExpr: combinator = self.combinator_mapping[combined.combinator] method = getattr(self, "xpath_%s_combinator" % combinator) return typing.cast( - XPathExpr, method(self.xpath(combined.selector), self.xpath(combined.subselector)) + XPathExpr, + method(self.xpath(combined.selector), self.xpath(combined.subselector)), ) def xpath_negation(self, negation: Negation) -> XPathExpr: @@ -381,7 +392,9 @@ def xpath_attrib(self, selector: Attrib) -> XPathExpr: value = typing.cast(str, selector.value.value).lower() else: value = selector.value.value - return typing.cast(XPathExpr, method(self.xpath(selector.selector), attrib, value)) + return typing.cast( + XPathExpr, method(self.xpath(selector.selector), attrib, value) + ) def xpath_class(self, class_selector: Class) -> XPathExpr: """Translate a class selector.""" @@ -416,7 +429,9 @@ def xpath_element(self, selector: Element) -> XPathExpr: # CombinedSelector: dispatch by combinator - def xpath_descendant_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr: + def xpath_descendant_combinator( + self, left: XPathExpr, right: XPathExpr + ) -> XPathExpr: """right is a child, grand-child or further descendant of left""" return left.join("/descendant-or-self::*/", right) @@ -424,21 +439,31 @@ def xpath_child_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr """right is an immediate child of left""" return left.join("/", right) - def xpath_direct_adjacent_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr: + def xpath_direct_adjacent_combinator( + self, left: XPathExpr, right: XPathExpr + ) -> XPathExpr: """right is a sibling immediately after left""" xpath = left.join("/following-sibling::", right) xpath.add_name_test() return xpath.add_condition("position() = 1") - def xpath_indirect_adjacent_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr: + def xpath_indirect_adjacent_combinator( + self, left: XPathExpr, right: XPathExpr + ) -> XPathExpr: """right is a sibling after left, immediately or not""" return left.join("/following-sibling::", right) - def xpath_relation_descendant_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr: + def xpath_relation_descendant_combinator( + self, left: XPathExpr, right: XPathExpr + ) -> XPathExpr: """right is a child, grand-child or further descendant of left; select left""" - return left.join("[descendant::", right, closing_combiner="]", has_inner_condition=True) + return left.join( + "[descendant::", right, closing_combiner="]", has_inner_condition=True + ) - def xpath_relation_child_combinator(self, left: XPathExpr, right: XPathExpr) -> XPathExpr: + def xpath_relation_child_combinator( + self, left: XPathExpr, right: XPathExpr + ) -> XPathExpr: """right is an immediate child of left; select left""" return left.join("[./", right, closing_combiner="]") @@ -447,7 +472,9 @@ def xpath_relation_direct_adjacent_combinator( ) -> XPathExpr: """right is a sibling immediately after left; select left""" xpath = left.add_condition( - "following-sibling::*[(name() = '{}') and (position() = 1)]".format(right.element) + "following-sibling::*[(name() = '{}') and (position() = 1)]".format( + right.element + ) ) return xpath @@ -460,7 +487,11 @@ def xpath_relation_indirect_adjacent_combinator( # Function: dispatch by function/pseudo-class name def xpath_nth_child_function( - self, xpath: XPathExpr, function: Function, last: bool = False, add_name_test: bool = True + self, + xpath: XPathExpr, + function: Function, + last: bool = False, + add_name_test: bool = True, ) -> XPathExpr: try: a, b = parse_series(function.arguments) @@ -589,28 +620,41 @@ def xpath_nth_child_function( template = "(%s)" else: template = "%s" - xpath.add_condition(" and ".join(template % expression for expression in expressions)) + xpath.add_condition( + " and ".join(template % expression for expression in expressions) + ) return xpath - def xpath_nth_last_child_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: + def xpath_nth_last_child_function( + self, xpath: XPathExpr, function: Function + ) -> XPathExpr: return self.xpath_nth_child_function(xpath, function, last=True) - def xpath_nth_of_type_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: + def xpath_nth_of_type_function( + self, xpath: XPathExpr, function: Function + ) -> XPathExpr: if xpath.element == "*": raise ExpressionError("*:nth-of-type() is not implemented") return self.xpath_nth_child_function(xpath, function, add_name_test=False) - def xpath_nth_last_of_type_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: + def xpath_nth_last_of_type_function( + self, xpath: XPathExpr, function: Function + ) -> XPathExpr: if xpath.element == "*": raise ExpressionError("*:nth-of-type() is not implemented") - return self.xpath_nth_child_function(xpath, function, last=True, add_name_test=False) + return self.xpath_nth_child_function( + xpath, function, last=True, add_name_test=False + ) - def xpath_contains_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: + def xpath_contains_function( + self, xpath: XPathExpr, function: Function + ) -> XPathExpr: # Defined there, removed in later drafts: # http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#content-selectors if function.argument_types() not in (["STRING"], ["IDENT"]): raise ExpressionError( - "Expected a single string or ident for :contains(), got %r" % function.arguments + "Expected a single string or ident for :contains(), got %r" + % function.arguments ) value = typing.cast(str, function.arguments[0].value) return xpath.add_condition("contains(., %s)" % self.xpath_literal(value)) @@ -618,7 +662,8 @@ def xpath_contains_function(self, xpath: XPathExpr, function: Function) -> XPath def xpath_lang_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: if function.argument_types() not in (["STRING"], ["IDENT"]): raise ExpressionError( - "Expected a single string or ident for :lang(), got %r" % function.arguments + "Expected a single string or ident for :lang(), got %r" + % function.arguments ) value = typing.cast(str, function.arguments[0].value) return xpath.add_condition("lang(%s)" % (self.xpath_literal(value))) @@ -679,12 +724,16 @@ def pseudo_never_matches(self, xpath: XPathExpr) -> XPathExpr: # Attrib: dispatch by attribute operator - def xpath_attrib_exists(self, xpath: XPathExpr, name: str, value: Optional[str]) -> XPathExpr: + def xpath_attrib_exists( + self, xpath: XPathExpr, name: str, value: Optional[str] + ) -> XPathExpr: assert not value xpath.add_condition(name) return xpath - def xpath_attrib_equals(self, xpath: XPathExpr, name: str, value: Optional[str]) -> XPathExpr: + def xpath_attrib_equals( + self, xpath: XPathExpr, name: str, value: Optional[str] + ) -> XPathExpr: assert value is not None xpath.add_condition("%s = %s" % (name, self.xpath_literal(value))) return xpath @@ -695,7 +744,9 @@ def xpath_attrib_different( assert value is not None # FIXME: this seems like a weird hack... if value: - xpath.add_condition("not(%s) or %s != %s" % (name, name, self.xpath_literal(value))) + xpath.add_condition( + "not(%s) or %s != %s" % (name, name, self.xpath_literal(value)) + ) else: xpath.add_condition("%s != %s" % (name, self.xpath_literal(value))) return xpath @@ -719,7 +770,13 @@ def xpath_attrib_dashmatch( # Weird, but true... xpath.add_condition( "%s and (%s = %s or starts-with(%s, %s))" - % (name, name, self.xpath_literal(value), name, self.xpath_literal(value + "-")) + % ( + name, + name, + self.xpath_literal(value), + name, + self.xpath_literal(value + "-"), + ) ) return xpath @@ -798,7 +855,8 @@ def xpath_checked_pseudo(self, xpath: XPathExpr) -> XPathExpr: # type: ignore def xpath_lang_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: if function.argument_types() not in (["STRING"], ["IDENT"]): raise ExpressionError( - "Expected a single string or ident for :lang(), got %r" % function.arguments + "Expected a single string or ident for :lang(), got %r" + % function.arguments ) value = function.arguments[0].value assert value @@ -807,7 +865,8 @@ def xpath_lang_function(self, xpath: XPathExpr, function: Function) -> XPathExpr # XPath 1.0 has no lower-case function... "translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', " "'abcdefghijklmnopqrstuvwxyz'), " - "'-'), %s)]" % (self.lang_attribute, self.xpath_literal(value.lower() + "-")) + "'-'), %s)]" + % (self.lang_attribute, self.xpath_literal(value.lower() + "-")) ) def xpath_link_pseudo(self, xpath: XPathExpr) -> XPathExpr: # type: ignore diff --git a/docs/conf.py b/docs/conf.py index 811de25..aa5ae22 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,83 +12,86 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os, re +import os +import re +import sys # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -#sys.path.insert(0, os.path.abspath('.')) +# sys.path.insert(0, os.path.abspath('.')) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' +# needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. -extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', - 'sphinx.ext.doctest'] +extensions = ["sphinx.ext.autodoc", "sphinx.ext.intersphinx", "sphinx.ext.doctest"] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix of source filenames. -source_suffix = '.rst' +source_suffix = ".rst" # The encoding of source files. -#source_encoding = 'utf-8-sig' +# source_encoding = 'utf-8-sig' # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = 'cssselect' -copyright = '2012-2017, Simon Sapin, Scrapy developers' +project = "cssselect" +copyright = "2012-2017, Simon Sapin, Scrapy developers" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The full version, including alpha/beta/rc tags. -with open(os.path.join(os.path.dirname(__file__), '..', 'cssselect', '__init__.py')) as init_file: +with open( + os.path.join(os.path.dirname(__file__), "..", "cssselect", "__init__.py") +) as init_file: init_py = init_file.read() release = re.search('VERSION = "([^"]+)"', init_py).group(1) # The short X.Y version. -version = release.rstrip('dev') +version = release.rstrip("dev") # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. -#language = None +# language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +# today = '' # Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' +# today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['_build'] +exclude_patterns = ["_build"] # The reST default role (used for this markup: `text`) to use for all documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). -#add_module_names = True +# add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] +# modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- @@ -100,129 +103,123 @@ # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. -#html_theme_options = {} +# html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] +# html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -#html_title = None +# html_title = None # A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None +# html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. -#html_logo = None +# html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. -#html_favicon = None +# html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -#html_static_path = ['_static'] +# html_static_path = ['_static'] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' +# html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. -#html_sidebars = {} +# html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} +# html_additional_pages = {} # If false, no module index is generated. -#html_domain_indices = True +# html_domain_indices = True # If false, no index is generated. -#html_use_index = True +# html_use_index = True # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True +# html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True +# html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True +# html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +# html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None +# html_file_suffix = None # Output file base name for HTML help builder. -htmlhelp_basename = 'cssselectdoc' +htmlhelp_basename = "cssselectdoc" # -- Options for LaTeX output -------------------------------------------------- latex_elements = { -# The paper size ('letterpaper' or 'a4paper'). -#'papersize': 'letterpaper', - -# The font size ('10pt', '11pt' or '12pt'). -#'pointsize': '10pt', - -# Additional stuff for the LaTeX preamble. -#'preamble': '', + # The paper size ('letterpaper' or 'a4paper'). + #'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + #'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'cssselect.tex', 'cssselect Documentation', - 'Simon Sapin', 'manual'), + ("index", "cssselect.tex", "cssselect Documentation", "Simon Sapin", "manual"), ] # The name of an image file (relative to this directory) to place at the top of # the title page. -#latex_logo = None +# latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # If true, show page references after internal links. -#latex_show_pagerefs = False +# latex_show_pagerefs = False # If true, show URL addresses after external links. -#latex_show_urls = False +# latex_show_urls = False # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. -#latex_domain_indices = True +# latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - ('index', 'cssselect', 'cssselect Documentation', - ['Simon Sapin'], 1) -] +man_pages = [("index", "cssselect", "cssselect Documentation", ["Simon Sapin"], 1)] # If true, show URL addresses after external links. -#man_show_urls = False +# man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ @@ -231,23 +228,29 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'cssselect', 'cssselect Documentation', - 'Simon Sapin', 'cssselect', 'One line description of project.', - 'Miscellaneous'), + ( + "index", + "cssselect", + "cssselect Documentation", + "Simon Sapin", + "cssselect", + "One line description of project.", + "Miscellaneous", + ), ] # Documents to append as an appendix to all manuals. -#texinfo_appendices = [] +# texinfo_appendices = [] # If false, no module index is generated. -#texinfo_domain_indices = True +# texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' +# texinfo_show_urls = 'footnote' # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} +intersphinx_mapping = {"python": ("https://docs.python.org/3", None)} # --- Nitpicking options ------------------------------------------------------ @@ -255,5 +258,5 @@ nitpicky = True nitpick_ignore = [ # explicitly not a part of the public API - ('py:class', 'cssselect.parser.Token'), + ("py:class", "cssselect.parser.Token"), ] diff --git a/docs/conftest.py b/docs/conftest.py index 9d16bb7..a71d108 100644 --- a/docs/conftest.py +++ b/docs/conftest.py @@ -3,6 +3,7 @@ from sybil import Sybil from sybil.parsers.doctest import DocTestParser from sybil.parsers.skip import skip + try: # sybil 3.0.0+ from sybil.parsers.codeblock import PythonCodeBlockParser @@ -13,8 +14,8 @@ pytest_collect_file = Sybil( parsers=[ DocTestParser(optionflags=ELLIPSIS | NORMALIZE_WHITESPACE), - PythonCodeBlockParser(future_imports=['print_function']), + PythonCodeBlockParser(future_imports=["print_function"]), skip, ], - pattern='*.rst', + pattern="*.rst", ).pytest() diff --git a/pyproject.toml b/pyproject.toml index 57a5583..261fe3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,11 @@ +[tool.isort] +profile = "black" +multi_line_output = 3 + +[tool.mypy] +check_untyped_defs = true +ignore_missing_imports = true +no_warn_no_return = true + [tool.black] -line-length = 99 +target-version = ["py38", "py39", "py310", "py311", "py312"] \ No newline at end of file diff --git a/setup.py b/setup.py index f7b51eb..43eecc0 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,10 @@ # -*- coding: utf-8 -*- -import re import os.path +import re from setuptools import setup - ROOT = os.path.dirname(__file__) with open(os.path.join(ROOT, "README.rst")) as readme_file: README = readme_file.read() diff --git a/tests/test_cssselect.py b/tests/test_cssselect.py index 2c9e94c..32c1683 100644 --- a/tests/test_cssselect.py +++ b/tests/test_cssselect.py @@ -23,27 +23,31 @@ from typing import List, Optional, Sequence, Tuple from lxml import etree, html + from cssselect import ( - parse, + ExpressionError, GenericTranslator, HTMLTranslator, SelectorSyntaxError, - ExpressionError, + parse, ) from cssselect.parser import ( - tokenize, - parse_series, - PseudoElement, - FunctionalPseudoElement, Function, + FunctionalPseudoElement, + PseudoElement, Token, + parse_series, + tokenize, ) from cssselect.xpath import XPathExpr class TestCssselect(unittest.TestCase): def test_tokenizer(self) -> None: - tokens = [str(item) for item in tokenize(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 == [ "", "", @@ -70,7 +74,10 @@ def repr_parse(css: str) -> List[str]: selectors = parse(css) for selector in selectors: assert selector.pseudo_element is None - return [repr(selector.parsed_tree).replace("(u'", "('") for selector in selectors] + return [ + repr(selector.parsed_tree).replace("(u'", "('") + for selector in selectors + ] def parse_many(first: str, *others: str) -> List[str]: result = repr_parse(first) @@ -95,7 +102,9 @@ def parse_many(first: str, *others: str) -> List[str]: "div\r>\n\n\n.foo", "div\f>\f.foo", ) == ["CombinedSelector[Element[div] > Class[Element[*].foo]]"] - assert parse_many("td.foo,.bar", "td.foo, .bar", "td.foo\t\r\n\f ,\t\r\n\f .bar") == [ + assert parse_many( + "td.foo,.bar", "td.foo, .bar", "td.foo\t\r\n\f ,\t\r\n\f .bar" + ) == [ "Class[Element[td].foo]", "Class[Element[*].bar]", ] @@ -123,11 +132,15 @@ def parse_many(first: str, *others: str) -> List[str]: assert parse_many("a[hreflang |= 'en']", "a[hreflang|=en]") == [ "Attrib[Element[a][hreflang |= 'en']]" ] - assert parse_many("div:nth-child(10)") == ["Function[Element[div]:nth-child(['10'])]"] + assert parse_many("div:nth-child(10)") == [ + "Function[Element[div]:nth-child(['10'])]" + ] assert parse_many(":nth-child(2n+2)") == [ "Function[Element[*]:nth-child(['2', 'n', '+2'])]" ] - assert parse_many("div:nth-of-type(10)") == ["Function[Element[div]:nth-of-type(['10'])]"] + assert parse_many("div:nth-of-type(10)") == [ + "Function[Element[div]:nth-of-type(['10'])]" + ] assert parse_many("div div:nth-of-type(10) .aclass") == [ "CombinedSelector[CombinedSelector[Element[div] " "Function[Element[div]:nth-of-type(['10'])]] " @@ -135,7 +148,9 @@ def parse_many(first: str, *others: str) -> List[str]: ] assert parse_many("label:only") == ["Pseudo[Element[label]:only]"] assert parse_many("a:lang(fr)") == ["Function[Element[a]:lang(['fr'])]"] - assert parse_many('div:contains("foo")') == ["Function[Element[div]:contains(['foo'])]"] + assert parse_many('div:contains("foo")') == [ + "Function[Element[div]:contains(['foo'])]" + ] assert parse_many("div#foobar") == ["Hash[Element[div]#foobar]"] assert parse_many("div:not(div.foo)") == [ "Negation[Element[div]:not(Class[Element[div].foo])]" @@ -253,7 +268,10 @@ def test_pseudo_repr(css: str) -> str: assert selector.pseudo_element == "foo" assert tr.selector_to_xpath(selector, prefix="") == "e" self.assertRaises( - ExpressionError, tr.selector_to_xpath, selector, translate_pseudo_elements=True + ExpressionError, + tr.selector_to_xpath, + selector, + translate_pseudo_elements=True, ) # Special test for the unicode symbols and ':scope' element if check @@ -301,7 +319,11 @@ def specificity(css: str) -> Tuple[int, int, int]: assert specificity("foo::before") == (0, 0, 2) assert specificity("foo:empty::before") == (0, 1, 2) - assert specificity("#lorem + foo#ipsum:first-child > bar:first-line") == (2, 1, 3) + assert specificity("#lorem + foo#ipsum:first-child > bar:first-line") == ( + 2, + 1, + 3, + ) def test_css_export(self) -> None: def css2css(css: str, res: Optional[str] = None) -> None: @@ -354,7 +376,9 @@ def get_error(css: str) -> Optional[str]: assert get_error("attributes(href)/html/body/a") == ( "Expected selector, got " ) - assert get_error("attributes(href)") == ("Expected selector, got ") + assert get_error("attributes(href)") == ( + "Expected selector, got " + ) assert get_error("html/body/a") == ("Expected selector, got ") assert get_error(" ") == ("Expected selector, got ") assert get_error("div, ") == ("Expected selector, got ") @@ -369,10 +393,14 @@ def get_error(css: str) -> Optional[str]: assert get_error("[*]") == ("Expected '|', got ") assert get_error("[foo|]") == ("Expected ident, got ") assert get_error("[#]") == ("Expected ident or '*', got ") - assert get_error("[foo=#]") == ("Expected string or ident, got ") + assert get_error("[foo=#]") == ( + "Expected string or ident, got " + ) assert get_error("[href]a") == ("Expected selector, got ") assert get_error("[rel=stylesheet]") is None - assert get_error("[rel:stylesheet]") == ("Operator expected, got ") + assert get_error("[rel:stylesheet]") == ( + "Operator expected, got " + ) assert get_error("[rel=stylesheet") == ("Expected ']', got ") assert get_error(":lang(fr)") is None assert get_error(":lang(fr") == ("Expected an argument, got ") @@ -386,12 +414,20 @@ def get_error(css: str) -> Optional[str]: assert get_error("li:before a") == ( "Got pseudo-element ::before not at the end of a selector" ) - assert get_error(":not(:before)") == ("Got pseudo-element ::before inside :not() at 12") + assert get_error(":not(:before)") == ( + "Got pseudo-element ::before inside :not() at 12" + ) assert get_error(":not(:not(a))") == ("Got nested :not()") - assert get_error(":is(:before)") == ("Got pseudo-element ::before inside function") + assert get_error(":is(:before)") == ( + "Got pseudo-element ::before inside function" + ) assert get_error(":is(a b)") == ("Expected an argument, got ") - assert get_error(":where(:before)") == ("Got pseudo-element ::before inside function") - assert get_error(":where(a b)") == ("Expected an argument, got ") + assert get_error(":where(:before)") == ( + "Got pseudo-element ::before inside function" + ) + assert get_error(":where(a b)") == ( + "Expected an argument, got " + ) assert get_error(":scope > div :scope header") == ( 'Got immediate child pseudo-element ":scope" not at the start of a selector' ) @@ -446,19 +482,29 @@ def xpath(css: str) -> str: "e[(count(preceding-sibling::*) >= 1) and " "((count(preceding-sibling::*) +2) mod 3 = 0)]" ) - assert xpath("e:nth-child(3n-2)") == ("e[count(preceding-sibling::*) mod 3 = 0]") + assert xpath("e:nth-child(3n-2)") == ( + "e[count(preceding-sibling::*) mod 3 = 0]" + ) assert xpath("e:nth-child(-n+6)") == ("e[count(preceding-sibling::*) <= 5]") assert xpath("e:nth-last-child(1)") == ("e[count(following-sibling::*) = 0]") - assert xpath("e:nth-last-child(2n)") == ("e[(count(following-sibling::*) +1) mod 2 = 0]") - assert xpath("e:nth-last-child(2n+1)") == ("e[count(following-sibling::*) mod 2 = 0]") + assert xpath("e:nth-last-child(2n)") == ( + "e[(count(following-sibling::*) +1) mod 2 = 0]" + ) + assert xpath("e:nth-last-child(2n+1)") == ( + "e[count(following-sibling::*) mod 2 = 0]" + ) assert xpath("e:nth-last-child(2n+2)") == ( "e[(count(following-sibling::*) >= 1) and " "((count(following-sibling::*) +1) mod 2 = 0)]" ) - assert xpath("e:nth-last-child(3n+1)") == ("e[count(following-sibling::*) mod 3 = 0]") + assert xpath("e:nth-last-child(3n+1)") == ( + "e[count(following-sibling::*) mod 3 = 0]" + ) # represents the two last e elements - assert xpath("e:nth-last-child(-n+2)") == ("e[count(following-sibling::*) <= 1]") + assert xpath("e:nth-last-child(-n+2)") == ( + "e[count(following-sibling::*) <= 1]" + ) assert xpath("e:nth-of-type(1)") == ("e[count(preceding-sibling::e) = 0]") assert xpath("e:nth-last-of-type(1)") == ("e[count(following-sibling::e) = 0]") @@ -486,24 +532,32 @@ def xpath(css: str) -> str: assert xpath("e:has(f)") == "e[descendant::f]" assert xpath("e:has(~ f)") == "e[following-sibling::f]" assert ( - xpath("e:has(+ f)") == "e[following-sibling::*[(name() = 'f') and (position() = 1)]]" + xpath("e:has(+ f)") + == "e[following-sibling::*[(name() = 'f') and (position() = 1)]]" ) assert xpath('e:contains("foo")') == ("e[contains(., 'foo')]") assert xpath("e:ConTains(foo)") == ("e[contains(., 'foo')]") assert xpath("e.warning") == ( - "e[@class and contains(" "concat(' ', normalize-space(@class), ' '), ' warning ')]" + "e[@class and contains(" + "concat(' ', normalize-space(@class), ' '), ' warning ')]" ) assert xpath("e#myid") == ("e[@id = 'myid']") - assert xpath("e:not(:nth-child(odd))") == ("e[not(count(preceding-sibling::*) mod 2 = 0)]") + assert xpath("e:not(:nth-child(odd))") == ( + "e[not(count(preceding-sibling::*) mod 2 = 0)]" + ) assert xpath("e:nOT(*)") == ("e[0]") # never matches assert xpath("e f") == ("e/descendant-or-self::*/f") assert xpath("e > f") == ("e/f") - assert xpath("e + f") == ("e/following-sibling::*[(name() = 'f') and (position() = 1)]") + assert xpath("e + f") == ( + "e/following-sibling::*[(name() = 'f') and (position() = 1)]" + ) assert xpath("e ~ f") == ("e/following-sibling::f") assert xpath("e ~ f:nth-child(3)") == ( "e/following-sibling::f[count(preceding-sibling::*) = 2]" ) - assert xpath("div#container p") == ("div[@id = 'container']/descendant-or-self::*/p") + assert xpath("div#container p") == ( + "div[@id = 'container']/descendant-or-self::*/p" + ) assert xpath("e:where(foo)") == "e[name() = 'foo']" assert xpath("e:where(foo, bar)") == "e[(name() = 'foo') or (name() = 'bar')]" @@ -539,10 +593,18 @@ def test_unicode(self) -> None: def test_quoting(self) -> None: css_to_xpath = GenericTranslator().css_to_xpath - assert css_to_xpath('*[aval="\'"]') == ("""descendant-or-self::*[@aval = "'"]""") - assert css_to_xpath("*[aval=\"'''\"]") == ("""descendant-or-self::*[@aval = "'''"]""") - assert css_to_xpath("*[aval='\"']") == ("""descendant-or-self::*[@aval = '"']""") - assert css_to_xpath('*[aval=\'"""\']') == ('''descendant-or-self::*[@aval = '"""']''') + assert css_to_xpath('*[aval="\'"]') == ( + """descendant-or-self::*[@aval = "'"]""" + ) + assert css_to_xpath("*[aval=\"'''\"]") == ( + """descendant-or-self::*[@aval = "'''"]""" + ) + assert css_to_xpath("*[aval='\"']") == ( + """descendant-or-self::*[@aval = '"']""" + ) + assert css_to_xpath('*[aval=\'"""\']') == ( + '''descendant-or-self::*[@aval = '"""']''' + ) assert css_to_xpath(':scope > div[dataimg=""]') == ( "descendant-or-self::*[1]/div[@dataimg = '']" ) @@ -575,7 +637,8 @@ def xpath_pseudo_element( method = getattr(self, method_name, None) if not method: raise ExpressionError( - "The functional pseudo-element ::%s() is unknown" % pseudo_element.name + "The functional pseudo-element ::%s() is unknown" + % pseudo_element.name ) xpath = method(xpath, pseudo_element.arguments) else: @@ -592,7 +655,9 @@ def xpath_pseudo_element( # functional pseudo-class: # elements that have a certain number of attributes - def xpath_nb_attr_function(self, xpath: XPathExpr, function: Function) -> XPathExpr: + def xpath_nb_attr_function( + self, xpath: XPathExpr, function: Function + ) -> XPathExpr: assert function.arguments[0].value nb_attributes = int(function.arguments[0].value) return xpath.add_condition("count(@*)=%d" % nb_attributes) @@ -616,7 +681,9 @@ def xpath_attr_functional_pseudo_element( # pseudo-element: # element's text() nodes - def xpath_text_node_simple_pseudo_element(self, xpath: XPathExpr) -> XPathExpr: + def xpath_text_node_simple_pseudo_element( + self, xpath: XPathExpr + ) -> XPathExpr: other = XPathExpr( "text()", "", @@ -625,7 +692,9 @@ def xpath_text_node_simple_pseudo_element(self, xpath: XPathExpr) -> XPathExpr: # pseudo-element: # element's href attribute - def xpath_attr_href_simple_pseudo_element(self, xpath: XPathExpr) -> XPathExpr: + def xpath_attr_href_simple_pseudo_element( + self, xpath: XPathExpr + ) -> XPathExpr: other = XPathExpr( "@href", "", @@ -656,7 +725,9 @@ def xpath(css: str) -> str: assert str(XPathExpr("", "", condition="@href")) == "[@href]" document = etree.fromstring(OPERATOR_PRECEDENCE_IDS) - sort_key = dict((el, count) for count, el in enumerate(document.iter())).__getitem__ + sort_key = dict( + (el, count) for count, el in enumerate(document.iter()) + ).__getitem__ def operator_id(selector: str) -> List[str]: xpath = CustomTranslator().css_to_xpath(selector) @@ -698,7 +769,9 @@ def series(css: str) -> Optional[Tuple[int, int]]: def test_lang(self) -> None: document = etree.fromstring(XMLLANG_IDS) - sort_key = dict((el, count) for count, el in enumerate(document.iter())).__getitem__ + sort_key = dict( + (el, count) for count, el in enumerate(document.iter()) + ).__getitem__ css_to_xpath = GenericTranslator().css_to_xpath def langid(selector: str) -> List[str]: @@ -714,7 +787,13 @@ def langid(selector: str) -> List[str]: assert langid(":lang(ru)") == ["sixth"] assert langid(":lang('ZH')") == ["eighth"] assert langid(":lang(de) :lang(zh)") == ["eighth"] - assert langid(":lang(en), :lang(zh)") == ["first", "second", "third", "fourth", "eighth"] + assert langid(":lang(en), :lang(zh)") == [ + "first", + "second", + "third", + "fourth", + "eighth", + ] assert langid(":lang(es)") == [] def test_argument_types(self) -> None: @@ -747,7 +826,9 @@ def argument_types(css: str) -> List[str]: def test_select(self) -> None: document = etree.fromstring(HTML_IDS) - sort_key = dict((el, count) for count, el in enumerate(document.iter())).__getitem__ + sort_key = dict( + (el, count) for count, el in enumerate(document.iter()) + ).__getitem__ css_to_xpath = GenericTranslator().css_to_xpath html_css_to_xpath = HTMLTranslator().css_to_xpath @@ -769,7 +850,14 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> List[str]: return result all_ids = pcss("*") - assert all_ids[:6] == ["html", "nil", "link-href", "link-nohref", "nil", "outer-div"] + assert all_ids[:6] == [ + "html", + "nil", + "link-href", + "link-nohref", + "nil", + "outer-div", + ] assert all_ids[-1:] == ["foobar-span"] assert pcss("div") == ["outer-div", "li-div", "foobar-div"] assert pcss("DIV", html_only=True) == [ @@ -780,7 +868,9 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> List[str]: assert pcss("div div") == ["li-div"] assert pcss("div, div div") == ["outer-div", "li-div", "foobar-div"] assert pcss("a[name]") == ["name-anchor"] - assert pcss("a[NAme]", html_only=True) == ["name-anchor"] # case-insensitive in HTML: + assert pcss("a[NAme]", html_only=True) == [ + "name-anchor" + ] # case-insensitive in HTML: assert pcss("a[rel]") == ["tag-anchor", "nofollow-anchor"] assert pcss('a[rel="tag"]') == ["tag-anchor"] assert pcss('a[href*="localhost"]') == ["tag-anchor"] @@ -798,7 +888,10 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> List[str]: assert pcss('*[lang|="en"]', '[lang|="en-US"]') == [] assert pcss('*[lang|="e"]') == [] # ... :lang() is not. - assert pcss(':lang("EN")', "*:lang(en-US)", html_only=True) == ["second-li", "li-div"] + assert pcss(':lang("EN")', "*:lang(en-US)", html_only=True) == [ + "second-li", + "li-div", + ] assert pcss(':lang("e")', html_only=True) == [] assert pcss(":scope > div") == [] assert pcss(":scope body") == ["nil"] @@ -852,7 +945,11 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> List[str]: "seventh-li", ] assert pcss("li:nth-last-child(2n+2)") == ["second-li", "fourth-li", "sixth-li"] - assert pcss("li:nth-last-child(3n+1)") == ["first-li", "fourth-li", "seventh-li"] + assert pcss("li:nth-last-child(3n+1)") == [ + "first-li", + "fourth-li", + "seventh-li", + ] assert pcss("ol:first-of-type") == ["first-ol"] assert pcss("ol:nth-child(1)") == [] assert pcss("ol:nth-of-type(2)") == ["second-ol"] @@ -901,7 +998,10 @@ def pcss(main: str, *selectors: str, **kwargs: bool) -> List[str]: assert pcss('*:contains("E")') == [] # case-sensitive assert pcss(".a", ".b", "*.a", "ol.a") == ["first-ol"] assert pcss(".c", "*.c") == ["first-ol", "third-li", "fourth-li"] - assert pcss("ol *.c", "ol li.c", "li ~ li.c", "ol > li.c") == ["third-li", "fourth-li"] + assert pcss("ol *.c", "ol li.c", "li ~ li.c", "ol > li.c") == [ + "third-li", + "fourth-li", + ] assert pcss("#first-li", "li#first-li", "*#first-li") == ["first-li"] assert pcss("li div", "li > div", "div div") == ["li-div"] assert pcss("div > div") == [] diff --git a/tox.ini b/tox.ini index 24dec48..6831d3f 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = black,flake8,pylint,security,py,docs +envlist = pre-commit,pylint,py,docs,typing [testenv] deps = @@ -13,18 +13,6 @@ commands = --cov-report=term-missing --cov-report=html --cov-report=xml \ --verbose {posargs: cssselect tests docs} -[testenv:black] -deps = - black==22.10.0 -commands = - black --check {posargs: cssselect setup.py tests} - -[testenv:flake8] -deps = - flake8==6.1.0 -commands = - flake8 {posargs: cssselect setup.py tests docs/conf.py} - [testenv:pylint] deps = {[testenv]deps} @@ -32,12 +20,6 @@ deps = commands = pylint {posargs: cssselect setup.py tests docs} -[testenv:security] -deps = - bandit -commands = - bandit -r -c .bandit.yml {posargs: cssselect} - [testenv:docs] changedir = docs deps = @@ -52,3 +34,8 @@ deps = mypy==0.982 commands = mypy --strict {posargs: cssselect tests} + +[testenv:pre-commit] +deps = pre-commit +commands = pre-commit run --all-files --show-diff-on-failure +skip_install = true \ No newline at end of file