Skip to content

Commit ed50e70

Browse files
committed
defered import of hedge in ParseResult so that ParseResult can be imported by builders
1 parent aea10c0 commit ed50e70

2 files changed

Lines changed: 6 additions & 14 deletions

File tree

src/hyperbase/builders.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from __future__ import annotations
22

33
from collections.abc import Iterable
4-
from typing import TYPE_CHECKING, cast
4+
from typing import cast
55

66
from hyperbase.constants import ATOM_ENCODE_TABLE
77
from hyperbase.hyperedge import Atom, Hyperedge, UniqueAtom
8-
9-
if TYPE_CHECKING:
10-
from hyperbase.parsers.parse_result import ParseResult
8+
from hyperbase.parsers.parse_result import ParseResult
119

1210

1311
def str_to_atom(s: str) -> str:
@@ -114,15 +112,8 @@ def hedge(
114112
source: str | Hyperedge | list | tuple | ParseResult,
115113
) -> Hyperedge:
116114
"""Create a hyperedge."""
117-
# Check for ParseResult via duck typing to avoid circular import
118-
if (
119-
hasattr(source, "tok_pos")
120-
and hasattr(source, "tokens")
121-
and hasattr(source, "edge")
122-
):
123-
from hyperbase.parsers import ParseResult
124-
125-
_source = cast(ParseResult, source)
115+
if isinstance(source, ParseResult):
116+
_source = source
126117
edge = _rebuild_with_text(_source.edge, _source.tok_pos, _source.tokens)
127118
object.__setattr__(edge, "text", _source.text)
128119
return edge

src/hyperbase/parsers/parse_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from dataclasses import dataclass, field
55
from typing import Any, cast
66

7-
from hyperbase.builders import hedge
87
from hyperbase.hyperedge import Hyperedge
98

109

@@ -37,6 +36,8 @@ def to_json(self) -> str:
3736

3837
@classmethod
3938
def from_dict(cls, d: dict[str, Any]) -> ParseResult:
39+
from hyperbase.builders import hedge
40+
4041
edge = d["edge"]
4142
if isinstance(edge, str):
4243
edge = hedge(edge)

0 commit comments

Comments
 (0)