Skip to content

Commit

Permalink
Address some Deepsource warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jmanuel1 committed Mar 3, 2024
1 parent b77e819 commit 3c32cd5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 2 additions & 0 deletions concat/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def __repr__(self) -> str:


class ParseError(Node):
"""AST node for a parsing error that was recovered from."""

def __init__(self, result: concat.parser_combinators.Result) -> None:
super().__init__()
self.children = []
Expand Down
32 changes: 18 additions & 14 deletions concat/parser_combinators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"""

import itertools
import functools
import operator
from typing import (
Any,
Callable,
Expand All @@ -23,7 +21,7 @@
cast,
overload,
)
from typing_extensions import Protocol, TypeVarTuple
from typing_extensions import Protocol


class _SupportsPlus(Protocol):
Expand All @@ -41,14 +39,17 @@ def __add__(self, other: '_SupportsPlus') -> '_SupportsPlus':
V = TypeVar('V')


def _maybe_inf_range(min: int, max: float) -> Iterable[int]:
if max == float('inf'):
return itertools.count(min)
else:
return range(min, int(max))
def _maybe_inf_range(minimum: int, maximum: float) -> Iterable[int]:
if maximum == float('inf'):
return itertools.count(minimum)
return range(minimum, int(maximum))


class FailureTree:
"""Failure messages and positions from parsing.
Failures can be nested."""

def __init__(
self, expected: str, furthest_index: int, children: List['FailureTree']
) -> None:
Expand Down Expand Up @@ -83,6 +84,8 @@ def furthest_failure(failures: Iterable[FailureTree]) -> Optional[FailureTree]:


class Result(Generic[_T_co]):
"""Class representing the output of a parser and the parsing state."""

def __init__(
self,
output: _T_co,
Expand Down Expand Up @@ -124,6 +127,8 @@ def __hash__(self) -> int:


class Parser(Generic[_T_contra, _U_co]):
"""A parser in the functional style."""

def __init__(
self, f: Callable[[Sequence[_T_contra], int], Result[_U_co]]
) -> None:
Expand Down Expand Up @@ -159,8 +164,7 @@ def new_parser(
True,
new_failure,
)
else:
raise Exception('todo')
raise Exception('todo')
return right_result
assert left_result.failures is not None
assert right_result.failures is not None
Expand Down Expand Up @@ -263,10 +267,10 @@ def times(
@generate
def new_parser() -> Generator:
output = []
for i in range(min):
for _ in range(min):
result = yield self
output.append(result)
for i in _maybe_inf_range(min, max):
for _ in _maybe_inf_range(min, max):
result = yield self.map(lambda val: (val,)).optional() # type: ignore
if result is not None:
output.append(result[0])
Expand Down Expand Up @@ -359,7 +363,7 @@ def parse(self, seq: Sequence[_T_contra]) -> _U_co:


class ParseError(Exception):
pass
"""Exception raised for unrecoverable parser errors."""


def success(val: T) -> Parser[Any, T]:
Expand Down Expand Up @@ -395,7 +399,7 @@ def alt(*parsers: 'Parser[T, Any]') -> 'Parser[T, Any]':

def fail(expected: str) -> Parser[T, None]:
@Parser
def parser(stream: Sequence[T], index: int) -> Result[None]:
def parser(_: Sequence[T], index: int) -> Result[None]:
failure = FailureTree(expected, index, [])
return Result(None, index, False, failure)

Expand Down
6 changes: 1 addition & 5 deletions concat/tests/test_parser_combinators.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import concat.parser_combinators
from hypothesis import given, assume, example
from hypothesis import given, strategies as st
from hypothesis.strategies import (
SearchStrategy,
composite,
integers,
text,
one_of,
sampled_from,
)
import unittest
from typing import Callable, Optional, Tuple
Expand Down Expand Up @@ -63,8 +61,6 @@ def test_both_fail(self, left: str, right: str, stream: str, index: int):
)


from hypothesis import given, strategies as st

failure_trees = st.builds(
concat.parser_combinators.FailureTree,
text(),
Expand Down

0 comments on commit 3c32cd5

Please sign in to comment.