Skip to content

Commit

Permalink
fix(typings): tree generic
Browse files Browse the repository at this point in the history
  • Loading branch information
knopki committed Sep 17, 2023
1 parent 8f87202 commit 30d1c44
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pytopas/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# pylint: disable=import-outside-toplevel

import json
from typing import Any, Generic, List, Sequence, Tuple, Type, Union, cast
from typing import Any, List, Sequence, Tuple, Type, Union, cast

from .lark_standalone import Token, Tree, _Leaf_T
from .lark_standalone import Tree, _Leaf_T
from .token import AllTokens, BaseToken


class TOPASTree(Tree[Generic[_Leaf_T]]):
class TOPASTree(Tree[_Leaf_T]):
"Enchanced Tree"

def __repr__(self):
Expand Down Expand Up @@ -66,16 +66,17 @@ def to_topas(self) -> str:
return " ".join(folded.children)

@classmethod
def _from_str(cls, data: str) -> "TOPASTree":
def _from_str(cls, data: str) -> "TOPASTree[_Leaf_T]":
"Private helper"
from .parser import TOPASParser

return TOPASParser().parse(data)
tree = TOPASParser().parse(data)
return cast(TOPASTree[_Leaf_T], tree)

@classmethod
def _from_tuples(
cls, data: Union[Tuple[str, Any], str]
) -> "Union[TOPASTree, BaseToken]":
) -> "Union[TOPASTree[_Leaf_T], BaseToken]":
"Private helper"
if isinstance(data, str):
return cls._from_str(data)
Expand Down Expand Up @@ -104,7 +105,7 @@ def _from_tuples(
raise AssertionError(f"{head!r} is unknown rule name")

@classmethod
def from_tuples(cls, data: Tuple[str, Any]) -> "TOPASTree":
def from_tuples(cls, data: Tuple[str, Any]) -> "TOPASTree[_Leaf_T]":
"Convert tuples to Tree"
assert type(data) in [list, tuple]
assert len(data) == 2
Expand All @@ -120,7 +121,7 @@ def from_tuples(cls, data: Tuple[str, Any]) -> "TOPASTree":
return TopasTree.from_children(children)

@classmethod
def from_json(cls, data: str) -> "TOPASTree | BaseToken":
def from_json(cls, data: str) -> "TOPASTree[_Leaf_T] | BaseToken":
"Convert JSON to Tree"
return cls.from_tuples(json.loads(data))

Expand Down Expand Up @@ -149,7 +150,7 @@ class ParameterEquationTree(TOPASTree):
data = "parameter_equation"


TOPASParseTree = TOPASTree[Token]
TOPASParseTree = TOPASTree[BaseToken]

AllTrees: Sequence[Type[TOPASTree]] = [
TopasTree,
Expand Down

0 comments on commit 30d1c44

Please sign in to comment.