Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding matcher POC #761

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 5 additions & 28 deletions src/pact/v3/match/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,9 @@
Typing definitions for the matchers.
"""

from __future__ import annotations
from typing import Any, TypeAlias

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

AtomicType = Union[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 = Union[
AtomicType,
Matcher,
dict[AtomicType, "MatchType"],
list["MatchType"],
tuple["MatchType"],
Sequence["MatchType"],
Mapping[AtomicType, "MatchType"],
]
Matchable: TypeAlias = Any
"""
All supported matchable types.
"""
36 changes: 36 additions & 0 deletions src/pact/v3/match/types.pyi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about stub files :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections.abc import Collection, Mapping, Sequence
from collections.abc import Set as AbstractSet
from decimal import Decimal
from fractions import Fraction
from typing import TypeAlias

from pydantic import BaseModel

_BaseMatchable: TypeAlias = (
int | float | complex | bool | str | bytes | bytearray | memoryview | None
)
"""
Base types that generally can't be further decomposed.

See: https://docs.python.org/3/library/stdtypes.html
"""

_ContainerMatchable: TypeAlias = (
Sequence[Matchable]
| AbstractSet[Matchable]
| Mapping[_BaseMatchable, Matchable]
| Collection[Matchable]
)
"""
Containers that can be further decomposed.

These are defined based on the abstract base classes defined in the
[`collections.abc`][collections.abc] module.
"""

_ExtraMatchable: TypeAlias = BaseModel | Decimal | Fraction

Matchable: TypeAlias = _BaseMatchable | _ContainerMatchable | _ExtraMatchable
"""
All supported matchable types.
"""