Skip to content

Commit

Permalink
Update to supported Python features (#111)
Browse files Browse the repository at this point in the history
* Replace format with f-strings
* Use builtin generics
* Update to Ruff 0.6
  • Loading branch information
drhagen authored Oct 11, 2024
1 parent 2b48fed commit db3b8c0
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 117 deletions.
2 changes: 1 addition & 1 deletion examples/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def make_expr(args):
expressions = ["123", "2 ^ 3", "1 + 1", "1 - 2 + 3 - 4", "3 - 4 * 2 + 10", "14 / (3.1 + 3.9)"]

for expression in expressions:
print("{} = {}".format(expression, ExpressionParsers.expr.parse(expression).unwrap()))
print(f"{expression} = {ExpressionParsers.expr.parse(expression).unwrap()}")
2 changes: 1 addition & 1 deletion examples/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ class JsonParsers(ParserContext, whitespace=r"[ \t\n\r]*"):
]

for string in strings:
print("source: {}\nvalue: {}".format(string, JsonParsers.value.parse(string)))
print(f"source: {string}\nvalue: {JsonParsers.value.parse(string)}")
2 changes: 1 addition & 1 deletion examples/positioned.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _consume(self, state: State, reader: Reader[Input]):
return status

def __repr__(self):
return self.name_or_nothing() + "positioned({})".format(self.parser.name_or_repr())
return self.name_or_nothing() + f"positioned({self.parser.name_or_repr()})"


def positioned(parser: Parser[Input, PositionAware[Output]]):
Expand Down
2 changes: 1 addition & 1 deletion examples/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ class TypicalUrlParsers(ParserContext):
]

for string in strings:
print("source: {}\nvalue: {}".format(string, TypicalUrlParsers.url.parse(string)))
print(f"source: {string}\nvalue: {TypicalUrlParsers.url.parse(string)}")
201 changes: 96 additions & 105 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pytest-cov = "*"
pytest-timeout = "*"

# Lint
ruff = "^0.1"
ruff = "^0.6"

# Docs
mkdocs-material = "^9"
Expand Down
4 changes: 2 additions & 2 deletions src/parsita/parsers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = ["Parser", "wrap_literal"]

from abc import abstractmethod
from typing import Any, Generic, List, Optional, Sequence, Union
from typing import Any, Generic, Optional, Sequence, Union

from .. import options
from ..state import (
Expand Down Expand Up @@ -188,7 +188,7 @@ def __or__(self, other) -> Parser:
from ._alternative import LongestAlternativeParser

other = wrap_literal(other)
parsers: List[Parser] = []
parsers: list[Parser] = []
if isinstance(self, LongestAlternativeParser) and not self.protected:
parsers.extend(self.parsers)
else:
Expand Down
4 changes: 2 additions & 2 deletions src/parsita/state/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = ["ParseError", "RecursionError"]

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, List
from typing import TYPE_CHECKING, Any

from ._reader import Reader

Expand All @@ -19,7 +19,7 @@ class ParseError(Exception):
"""

farthest: Reader[Any]
expected: List[str]
expected: list[str]

def __str__(self):
return self.farthest.expected_error(self.expected)
Expand Down
6 changes: 3 additions & 3 deletions src/parsita/state/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = ["State", "Continue", "Input", "Output"]

from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, Generic, List, Optional, Tuple, TypeVar
from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar

from ._reader import Reader

Expand All @@ -17,8 +17,8 @@
class State(Generic[Input]):
def __init__(self):
self.farthest: Optional[Reader[Any]] = None
self.expected: List[str] = []
self.memo: Dict[Tuple[Parser[Input, Any], int], Optional[Continue[Input, Any]]] = {}
self.expected: list[str] = []
self.memo: dict[tuple[Parser[Input, Any], int], Optional[Continue[Input, Any]]] = {}

def register_failure(self, expected: str, reader: Reader[Any]):
if self.farthest is None or self.farthest.position < reader.position:
Expand Down

0 comments on commit db3b8c0

Please sign in to comment.