From 2f33caf9ecd1288046c01e32db476becb777906b Mon Sep 17 00:00:00 2001 From: JP-Ellis Date: Thu, 26 Sep 2024 13:38:35 +1000 Subject: [PATCH] feat: add matchable typevar This is the TypeVar equivalent of the Matchable union type. Signed-off-by: JP-Ellis --- src/pact/v3/match/types.py | 11 ++++++++++- src/pact/v3/match/types.pyi | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/pact/v3/match/types.py b/src/pact/v3/match/types.py index 393f8ae220..90b06c9f15 100644 --- a/src/pact/v3/match/types.py +++ b/src/pact/v3/match/types.py @@ -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. +""" diff --git a/src/pact/v3/match/types.pyi b/src/pact/v3/match/types.pyi index eefda39c63..e3fcd6dfd3 100644 --- a/src/pact/v3/match/types.pyi +++ b/src/pact/v3/match/types.pyi @@ -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 ) @@ -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, +)