Skip to content

Commit

Permalink
feat: add matchable typevar
Browse files Browse the repository at this point in the history
This is the TypeVar equivalent of the Matchable union type.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Sep 26, 2024
1 parent 0dfc73c commit 2f33caf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/pact/v3/match/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
Typing definitions for the matchers.
"""

from typing import Any, TypeAlias
from typing import Any, TypeAlias, TypeVar

# Make _MatchableT explicitly public, despite ultimately only being used
# privately.
__all__ = ["Matchable", "_MatchableT"]

Matchable: TypeAlias = Any
"""
All supported matchable types.
"""

_MatchableT = TypeVar("_MatchableT")
"""
Matchable type variable.
"""
26 changes: 25 additions & 1 deletion src/pact/v3/match/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ 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 typing import TypeAlias, TypeVar

from pydantic import BaseModel

# Make _MatchableT explicitly public, despite ultimately only being used
# privately.
__all__ = ["Matchable", "_MatchableT"]

_BaseMatchable: TypeAlias = (
int | float | complex | bool | str | bytes | bytearray | memoryview | None
)
Expand Down Expand Up @@ -34,3 +38,23 @@ Matchable: TypeAlias = _BaseMatchable | _ContainerMatchable | _ExtraMatchable
"""
All supported matchable types.
"""

_MatchableT = TypeVar(
"_MatchableT",
int,
float,
complex,
bool,
str,
bytes,
bytearray,
memoryview,
None,
Sequence[Matchable],
AbstractSet[Matchable],
Mapping[_BaseMatchable, Matchable],
Collection[Matchable],
BaseModel,
Decimal,
Fraction,
)

0 comments on commit 2f33caf

Please sign in to comment.