From 3a9abf850a1d82c04586ae897e3ec0f4a03fa7b2 Mon Sep 17 00:00:00 2001 From: Joseph Young <80432516+jpy-git@users.noreply.github.com> Date: Sun, 13 Mar 2022 02:43:07 +0000 Subject: [PATCH] Stubs for parsimonious (#7477) --- .../@tests/stubtest_allowlist.txt | 2 + stubs/parsimonious/METADATA.toml | 1 + stubs/parsimonious/parsimonious/__init__.pyi | 8 +++ .../parsimonious/parsimonious/exceptions.pyi | 24 +++++++ .../parsimonious/parsimonious/expressions.pyi | 63 +++++++++++++++++++ stubs/parsimonious/parsimonious/grammar.pyi | 53 ++++++++++++++++ stubs/parsimonious/parsimonious/nodes.pyi | 38 +++++++++++ stubs/parsimonious/parsimonious/utils.pyi | 10 +++ 8 files changed, 199 insertions(+) create mode 100644 stubs/parsimonious/@tests/stubtest_allowlist.txt create mode 100644 stubs/parsimonious/METADATA.toml create mode 100644 stubs/parsimonious/parsimonious/__init__.pyi create mode 100644 stubs/parsimonious/parsimonious/exceptions.pyi create mode 100644 stubs/parsimonious/parsimonious/expressions.pyi create mode 100644 stubs/parsimonious/parsimonious/grammar.pyi create mode 100644 stubs/parsimonious/parsimonious/nodes.pyi create mode 100644 stubs/parsimonious/parsimonious/utils.pyi diff --git a/stubs/parsimonious/@tests/stubtest_allowlist.txt b/stubs/parsimonious/@tests/stubtest_allowlist.txt new file mode 100644 index 000000000000..ce8bec20df65 --- /dev/null +++ b/stubs/parsimonious/@tests/stubtest_allowlist.txt @@ -0,0 +1,2 @@ +parsimonious.nodes.Node.__repr__ +parsimonious.nodes.RuleDecoratorMeta.__new__ diff --git a/stubs/parsimonious/METADATA.toml b/stubs/parsimonious/METADATA.toml new file mode 100644 index 000000000000..29511ee7d5a7 --- /dev/null +++ b/stubs/parsimonious/METADATA.toml @@ -0,0 +1 @@ +version = "0.8.*" diff --git a/stubs/parsimonious/parsimonious/__init__.pyi b/stubs/parsimonious/parsimonious/__init__.pyi new file mode 100644 index 000000000000..31f99b479767 --- /dev/null +++ b/stubs/parsimonious/parsimonious/__init__.pyi @@ -0,0 +1,8 @@ +from parsimonious.exceptions import ( + BadGrammar as BadGrammar, + IncompleteParseError as IncompleteParseError, + ParseError as ParseError, + UndefinedLabel as UndefinedLabel, +) +from parsimonious.grammar import Grammar as Grammar, TokenGrammar as TokenGrammar +from parsimonious.nodes import NodeVisitor as NodeVisitor, VisitationError as VisitationError, rule as rule diff --git a/stubs/parsimonious/parsimonious/exceptions.pyi b/stubs/parsimonious/parsimonious/exceptions.pyi new file mode 100644 index 000000000000..243b0a4589a8 --- /dev/null +++ b/stubs/parsimonious/parsimonious/exceptions.pyi @@ -0,0 +1,24 @@ +from parsimonious.expressions import Expression +from parsimonious.grammar import LazyReference +from parsimonious.nodes import Node +from parsimonious.utils import StrAndRepr + +class ParseError(StrAndRepr, Exception): + text: str + pos: int + expr: Expression | None + def __init__(self, text: str, pos: int = ..., expr: Expression | None = ...) -> None: ... + def line(self) -> int: ... + def column(self) -> int: ... + +class IncompleteParseError(ParseError): ... + +class VisitationError(Exception): + original_class: type[BaseException] + def __init__(self, exc: BaseException, exc_class: type[BaseException], node: Node) -> None: ... + +class BadGrammar(StrAndRepr, Exception): ... + +class UndefinedLabel(BadGrammar): + label: LazyReference + def __init__(self, label: LazyReference) -> None: ... diff --git a/stubs/parsimonious/parsimonious/expressions.pyi b/stubs/parsimonious/parsimonious/expressions.pyi new file mode 100644 index 000000000000..577f7e1113e0 --- /dev/null +++ b/stubs/parsimonious/parsimonious/expressions.pyi @@ -0,0 +1,63 @@ +import typing +from typing import Any, Callable, Mapping, Pattern, Union + +from parsimonious.exceptions import ParseError +from parsimonious.grammar import Grammar +from parsimonious.nodes import Node +from parsimonious.utils import StrAndRepr + +MARKER: Any + +_CALLABLE_RETURN_TYPE = Union[int, tuple[int, list[Node]], Node, None] +_CALLABLE_TYPE = ( + Callable[[str, int], _CALLABLE_RETURN_TYPE] + | Callable[[str, int, Mapping[tuple[int, int], Node], ParseError, Grammar], _CALLABLE_RETURN_TYPE] +) + +def expression(callable: _CALLABLE_TYPE, rule_name: str, grammar: Grammar) -> Expression: ... + +class Expression(StrAndRepr): + name: str + identity_tuple: tuple[str] + def __init__(self, name: str = ...) -> None: ... + def parse(self, text: str, pos: int = ...) -> Node: ... + def match(self, text: str, pos: int = ...) -> Node: ... + def match_core(self, text: str, pos: int, cache: Mapping[tuple[int, int], Node], error: ParseError) -> Node: ... + def as_rule(self) -> str: ... + +class Literal(Expression): + literal: str + identity_tuple: tuple[str, str] # type: ignore + def __init__(self, literal: str, name: str = ...) -> None: ... + +class TokenMatcher(Literal): ... + +class Regex(Expression): + re: Pattern[str] + identity_tuple: tuple[str, Pattern[str]] # type: ignore + def __init__( + self, + pattern: str, + name: str = ..., + ignore_case: bool = ..., + locale: bool = ..., + multiline: bool = ..., + dot_all: bool = ..., + unicode: bool = ..., + verbose: bool = ..., + ) -> None: ... + +class Compound(Expression): + members: typing.Sequence[Expression] + def __init__(self, *members: Expression, **kwargs: Any) -> None: ... + +class Sequence(Compound): ... +class OneOf(Compound): ... +class Lookahead(Compound): ... +class Not(Compound): ... +class Optional(Compound): ... +class ZeroOrMore(Compound): ... + +class OneOrMore(Compound): + min: int + def __init__(self, member: Expression, name: str = ..., min: int = ...) -> None: ... diff --git a/stubs/parsimonious/parsimonious/grammar.pyi b/stubs/parsimonious/parsimonious/grammar.pyi new file mode 100644 index 000000000000..78d21b2d6e93 --- /dev/null +++ b/stubs/parsimonious/parsimonious/grammar.pyi @@ -0,0 +1,53 @@ +import typing +from collections import OrderedDict +from typing import Any, Callable, Mapping, NoReturn + +from parsimonious.expressions import _CALLABLE_TYPE, Expression, Literal, Lookahead, Not, OneOf, Regex, Sequence, TokenMatcher +from parsimonious.nodes import Node, NodeVisitor + +class Grammar(OrderedDict[str, Expression]): + default_rule: Expression | Any + def __init__(self, rules: str = ..., **more_rules: Expression | _CALLABLE_TYPE) -> None: ... + def default(self, rule_name: str) -> Grammar: ... + def parse(self, text: str, pos: int = ...) -> Node: ... + def match(self, text: str, pos: int = ...) -> Node: ... + +class TokenGrammar(Grammar): ... +class BootstrappingGrammar(Grammar): ... + +rule_syntax: str + +class LazyReference(str): + name: str + +class RuleVisitor(NodeVisitor): + quantifier_classes: dict[str, type[Expression]] + visit_expression: Callable[[RuleVisitor, Node, typing.Sequence[Any]], Any] + visit_term: Callable[[RuleVisitor, Node, typing.Sequence[Any]], Any] + visit_atom: Callable[[RuleVisitor, Node, typing.Sequence[Any]], Any] + custom_rules: dict[str, Expression] + def __init__(self, custom_rules: Mapping[str, Expression] | None = ...) -> None: ... + def visit_rules( + self, node: Node, rules_list: typing.Sequence[Any] + ) -> tuple[OrderedDict[str, Expression], Expression | None]: ... + def visit_rule(self, node: Node, rule: typing.Sequence[Any]) -> Expression: ... + def visit_label(self, node: Node, label: typing.Sequence[Any]) -> str: ... + def visit_ored(self, node: Node, ored: typing.Sequence[Any]) -> OneOf: ... + def visit_or_term(self, node: Node, or_term: typing.Sequence[Any]) -> Expression: ... + def visit_sequence(self, node: Node, sequence: typing.Sequence[Any]) -> Sequence: ... + def visit_not_term(self, node: Node, not_term: typing.Sequence[Any]) -> Not: ... + def visit_lookahead_term(self, node: Node, lookahead_term: typing.Sequence[Any]) -> Lookahead: ... + def visit_quantified(self, node: Node, quantified: typing.Sequence[Any]) -> Expression: ... + def visit_quantifier(self, node: Node, quantifier: typing.Sequence[Any]) -> Node: ... + def visit_reference(self, node: Node, reference: typing.Sequence[Any]) -> LazyReference: ... + def visit_literal(self, node: Node, literal: typing.Sequence[Any]) -> Literal: ... + def visit_spaceless_literal(self, spaceless_literal: Node, visited_children: typing.Sequence[Any]) -> Literal: ... + def visit_regex(self, node: Node, regex: typing.Sequence[Any]) -> Regex: ... + def visit_parenthesized(self, node: Node, parenthesized: typing.Sequence[Any]) -> Expression: ... + def generic_visit(self, node: Node, visited_children: typing.Sequence[Any]) -> typing.Sequence[Any] | Node: ... # type: ignore + +class TokenRuleVisitor(RuleVisitor): + def visit_spaceless_literal(self, spaceless_literal: Node, visited_children: typing.Sequence[Any]) -> TokenMatcher: ... + def visit_regex(self, node: Node, regex: typing.Sequence[Any]) -> NoReturn: ... + +rule_grammar: Grammar diff --git a/stubs/parsimonious/parsimonious/nodes.pyi b/stubs/parsimonious/parsimonious/nodes.pyi new file mode 100644 index 000000000000..841b04c4ba21 --- /dev/null +++ b/stubs/parsimonious/parsimonious/nodes.pyi @@ -0,0 +1,38 @@ +from re import Match +from typing import Any, Callable, Iterator, NoReturn, Sequence, TypeVar + +from parsimonious.exceptions import VisitationError as VisitationError +from parsimonious.expressions import Expression +from parsimonious.grammar import Grammar + +class Node: + expr: Expression + full_text: str + start: int + end: int + children: Sequence[Node] + def __init__(self, expr: Expression, full_text: str, start: int, end: int, children: Sequence[Node] | None = ...) -> None: ... + @property + def expr_name(self) -> str: ... + def __iter__(self) -> Iterator[Node]: ... + @property + def text(self) -> str: ... + def prettily(self, error: Node | None = ...) -> str: ... + +class RegexNode(Node): + match: Match[str] + +class RuleDecoratorMeta(type): ... + +class NodeVisitor(metaclass=RuleDecoratorMeta): + grammar: Grammar | Any + unwrapped_exceptions: tuple[type[Exception], ...] + def visit(self, node: Node) -> Any: ... + def generic_visit(self, node: Node, visited_children: Sequence[Any]) -> NoReturn: ... + def parse(self, text: str, pos: int = ...) -> Node: ... + def match(self, text: str, pos: int = ...) -> Node: ... + def lift_child(self, node: Node, children: Sequence[Any]) -> Any: ... + +_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) + +def rule(rule_string: str) -> Callable[[_CallableT], _CallableT]: ... diff --git a/stubs/parsimonious/parsimonious/utils.pyi b/stubs/parsimonious/parsimonious/utils.pyi new file mode 100644 index 000000000000..eea5d11851d9 --- /dev/null +++ b/stubs/parsimonious/parsimonious/utils.pyi @@ -0,0 +1,10 @@ +import ast +from typing import Any + +class StrAndRepr: ... + +def evaluate_string(string: str | ast.AST) -> Any: ... + +class Token(StrAndRepr): + type: str + def __init__(self, type: str) -> None: ...