Skip to content

Commit

Permalink
fix recursive typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
valkolovos committed Sep 20, 2024
1 parent 511c41a commit 4cd67f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/pact/v3/match/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from pact.v3.generators import date as date_generator
from pact.v3.generators import regex as regex_generator
from pact.v3.generators import time as time_generator
from pact.v3.match.matchers import ConcreteMatcher, Matcher
from pact.v3.match.matchers import ConcreteMatcher
from pact.v3.match.types import Matcher

if TYPE_CHECKING:
from pact.v3.match.types import MatchType
Expand Down Expand Up @@ -61,8 +62,8 @@ def decimal(value: float | None = None, digits: int | None = None) -> Matcher:

def number(
value: float | None = None,
min_val: float | None = None,
max_val: float | None = None,
min_val: int | None = None,
max_val: int | None = None,
digits: int | None = None,
) -> Matcher:
"""
Expand Down
19 changes: 4 additions & 15 deletions src/pact/v3/match/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

from __future__ import annotations

from abc import ABC, abstractmethod
from json import JSONEncoder
from typing import TYPE_CHECKING, Any, Literal

from pact.v3.match.types import Matcher

if TYPE_CHECKING:
from pact.v3.generators import Generator
from pact.v3.match.types import AtomicType
from pact.v3.match.types import MatchType

MatcherTypeV3 = Literal[
"equality",
Expand Down Expand Up @@ -47,18 +48,6 @@
)


class Matcher(ABC):
"""
Matcher interface for exporting.
"""

@abstractmethod
def to_dict(self) -> dict[str, Any]:
"""
Convert the matcher to a dictionary for json serialization.
"""


class ConcreteMatcher(Matcher):
"""
ConcreteMatcher class.
Expand All @@ -71,7 +60,7 @@ def __init__(
generator: Generator | None = None,
*,
force_generator: bool | None = False,
**kwargs: AtomicType,
**kwargs: MatchType,
) -> None:
"""
Initialize the matcher class.
Expand Down
30 changes: 24 additions & 6 deletions src/pact/v3/match/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@
Typing definitions for the matchers.
"""

from typing import Mapping, Sequence
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Any, Mapping, Sequence

AtomicType = str | int | float | bool | None


class Matcher(ABC):
"""
Matcher interface for exporting.
"""

@abstractmethod
def to_dict(self) -> dict[str, Any]:
"""
Convert the matcher to a dictionary for json serialization.
"""


MatchType = (
AtomicType
| dict[AtomicType, AtomicType]
| list[AtomicType]
| tuple[AtomicType]
| Sequence[AtomicType]
| Mapping[AtomicType, AtomicType]
| Matcher
| dict[AtomicType, "MatchType"]
| list["MatchType"]
| tuple["MatchType"]
| Sequence["MatchType"]
| Mapping[AtomicType, "MatchType"]
)

0 comments on commit 4cd67f5

Please sign in to comment.