Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ History and release notes

.. currentmodule:: parsy

Unreleased
----------
* Fixed various type hints

2.2 - 2025-09-12
----------------
* Dropped support for Python 3.7, 3.8 which are past EOL
Expand Down
20 changes: 12 additions & 8 deletions src/parsy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def many(self) -> Parser:
"""
return self.times(0, float("inf"))

def times(self, min: int, max: int = None) -> Parser:
def times(self, min: int, max: float | None = None) -> Parser:
"""
Returns a parser that expects the initial parser at least ``min`` times,
and at most ``max`` times, and produces a list of the results. If only one
Expand Down Expand Up @@ -260,7 +260,7 @@ def optional(self, default: Any = None) -> Parser:
"""
return self.times(0, 1).map(lambda v: v[0] if v else default)

def until(self, other: Parser, min: int = 0, max: int = float("inf"), consume_other: bool = False) -> Parser:
def until(self, other: Parser, min: int = 0, max: float = float("inf"), consume_other: bool = False) -> Parser:
"""
Returns a parser that expects the initial parser followed by ``other``.
The initial parser is expected at least ``min`` times and at most ``max`` times.
Expand Down Expand Up @@ -305,7 +305,7 @@ def until_parser(stream: str | bytes | list, index: int) -> Result:

return until_parser

def sep_by(self, sep: Parser, *, min: int = 0, max: int = float("inf")) -> Parser:
def sep_by(self, sep: Parser, *, min: int = 0, max: float = float("inf")) -> Parser:
"""
Returns a new parser that repeats the initial parser and
collects the results in a list. Between each item, the ``sep`` parser
Expand Down Expand Up @@ -544,7 +544,11 @@ def string_parser(stream: str, index: int) -> Result:
return string_parser


def regex(exp: str, flags=0, group: int | str | tuple = 0) -> Parser:
def regex(
exp: str | bytes | re.Pattern[str] | re.Pattern[bytes],
flags: int = 0,
group: int | str | tuple[int | str, ...] = 0,
) -> Parser:
"""
Returns a parser that expects the given ``exp``, and produces the
matched string. ``exp`` can be a compiled regular expression, or a
Expand Down Expand Up @@ -606,7 +610,7 @@ def test_char(func: Callable[..., bool], description: str) -> Parser:
return test_item(func, description)


def match_item(item: Any, description: str = None) -> Parser:
def match_item(item: Any, description: str | None = None) -> Parser:
"""
Returns a parser that tests the next item (or character) from the stream (or
string) for equality against the provided item. Optionally a string
Expand All @@ -618,7 +622,7 @@ def match_item(item: Any, description: str = None) -> Parser:
return test_item(lambda i: item == i, description)


def string_from(*strings: str, transform: Callable[[str], str] = noop):
def string_from(*strings: str, transform: Callable[[str], str] = noop) -> Parser:
"""
Accepts a sequence of strings as positional arguments, and returns a parser
that matches and returns one string from the list. The list is first sorted
Expand Down Expand Up @@ -680,7 +684,7 @@ def eof(stream: str | bytes | list, index: int) -> Result:
return Result.failure(index, "EOF")


def from_enum(enum_cls: type[enum.Enum], transform=noop) -> Parser:
def from_enum(enum_cls: type[enum.Enum], transform: Callable[[str], str] = noop) -> Parser:
"""
Given a class that is an enum.Enum class
https://docs.python.org/3/library/enum.html , returns a parser that
Expand Down Expand Up @@ -711,7 +715,7 @@ def _raise_error(self, *args, **kwargs):
parse = _raise_error
parse_partial = _raise_error

def become(self, other: Parser):
def become(self, other: Parser) -> None:
"""
Take on the behavior of the given parser.
"""
Expand Down
Loading