Skip to content

Improve typing support for _TupleParser #80

@beekill95

Description

@beekill95

Right now, when working with 3 parsers combined together, the resulting type of the combined parser is Any.

For instance,

from typing import reveal_type
from funcparserlib.parser import a

expr = a("x") + a("y") + a("z")
reveal_type(expr) # Parser[str, Any]

, and with 4 parsers, the resulting type is Tuple:

expr2 = a("x") + a("y") + a("z") + a("w")
reveal_type(expr2) # _TupleParser[str, Tuple[Any, str]]

This is troublesome when working with mypy for type checking. For example, the second code snippet will lead to mypy error:

def parse(args: tuple[str, str, str, str]):
    pass

expr2 = (a("x") + a("y") + a("z") + a("w")) >> parse
# Unsupported operand types for >> ("_TupleParser[str, tuple[Any, str]]" and "Callable[[tuple[str, str, str, str]], int]")

Ideally, for both examples, the resulting parsers should be _TupleParser, and the resulting types should be recognized correctly, tuple[str, str, str] and tuple[str, str, str, str] respectively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions