Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Improve typing support for _TupleParser #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions funcparserlib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import logging
import warnings
import sys
from typing import (
Any,
Callable,
Expand All @@ -82,13 +83,20 @@

from funcparserlib.lexer import Token

if sys.version_info >= (3, 11):
from typing import TypeVarTuple, Unpack
else:
from typing_extensions import TypeVarTuple, Unpack


log = logging.getLogger("funcparserlib")

debug = False

_A = TypeVar("_A")
_B = TypeVar("_B")
_C = TypeVar("_C")
_Ts = TypeVarTuple("_Ts")


class Parser(Generic[_A, _B]):
Expand Down Expand Up @@ -273,13 +281,13 @@ def __add__( # type: ignore[misc]
pass

@overload
def __add__(self, other: "Parser[_A, _C]") -> "_TupleParser[_A, Tuple[_B, _C]]":
def __add__(self, other: "Parser[_A, _C]") -> "_TupleParser[_A, _B, _C]":
pass

def __add__(
self,
other: Union["_IgnoredParser[_A]", "Parser[_A, _C]"],
) -> Union["Parser[_A, _B]", "_TupleParser[_A, Tuple[_B, _C]]"]:
) -> Union["Parser[_A, _B]", "_TupleParser[_A, _B, _C]"]:
"""Sequential combination of parsers. It runs this parser, then the other
parser.

Expand Down Expand Up @@ -551,18 +559,22 @@ class _Tuple(tuple):
pass


class _TupleParser(Parser[_A, _B], Generic[_A, _B]):
@overload # type: ignore[override]
def __add__(self, other: "_IgnoredParser[_A]") -> "_TupleParser[_A, _B]":
class _TupleParser(Parser[_A, Tuple[Unpack[_Ts]]], Generic[_A, Unpack[_Ts]]):
@overload # type: ignore[override, overload-overlap]
def __add__(self, other: "_IgnoredParser[_A]") -> "_TupleParser[_A, Unpack[_Ts]]":
pass

@overload
def __add__(self, other: Parser[_A, Any]) -> Parser[_A, Any]:
def __add__(self, other: Parser[_A, _B]) -> "_TupleParser[_A, Unpack[_Ts], _B]":
pass

def __add__(
self, other: Union["_IgnoredParser[_A]", Parser[_A, Any]]
) -> Union["_TupleParser[_A, _B]", Parser[_A, Any]]:
self,
other: Union[
"_IgnoredParser[_A]",
Parser[_A, _B],
],
) -> Union["_TupleParser[_A, Unpack[_Ts]]", "_TupleParser[_A, Unpack[_Ts], _B]"]:
return super().__add__(other)


Expand Down
14 changes: 2 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8"
typing-extensions = {version = ">=4.1.0", python = "<3.11"}

[tool.poetry.dev-dependencies]
pre-commit = {version = "^3.5.0"}
Expand Down
3 changes: 2 additions & 1 deletion tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Parser,
maybe,
_Ignored, # noqa
_TupleParser, # noqa
tok,
finished,
forward_decl,
Expand Down Expand Up @@ -103,7 +104,7 @@ def test_ok_ignored_ok(self) -> None:
def test_ok_ok_ok(self) -> None:
x = a("x")
y = a("y")
expr: Parser[str, Tuple[str, str]] = x + y + x
expr: _TupleParser[str, str, str, str] = x + y + x
self.assertEqual(expr.parse("xyx"), ("x", "y", "x"))

def test_ok_ok_ignored(self) -> None:
Expand Down
Loading