From 565233e476126e59d667a998c8986c01d19da003 Mon Sep 17 00:00:00 2001 From: Dom Sekotill Date: Sat, 21 May 2022 19:05:22 +0100 Subject: [PATCH] Move bound TypeVar out of PatternEnum --- behave_utils/behave.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/behave_utils/behave.py b/behave_utils/behave.py index 765b62a..7ef10ae 100644 --- a/behave_utils/behave.py +++ b/behave_utils/behave.py @@ -1,4 +1,4 @@ -# Copyright 2021 Dominik Sekotill +# Copyright 2021-2022 Dominik Sekotill # # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this @@ -20,24 +20,27 @@ from typing import overload import behave import parse -T = TypeVar("T") - __all__ = [ "PatternEnum", "register_pattern", ] -class PatternConverter(Protocol): +if TYPE_CHECKING: + T = TypeVar("T") + C = TypeVar("C", bound="PatternEnum") + + + class PatternConverter(Protocol): - __name__: str + __name__: str - def __call__(self, match: str) -> Any: ... + def __call__(self, match: str) -> Any: ... -class Decorator(Protocol[T]): + class Decorator(Protocol[T]): - def __call__(self, converter: T) -> T: ... + def __call__(self, converter: T) -> T: ... @overload @@ -79,9 +82,10 @@ class EnumMeta(enum.EnumMeta): MEMBER_FILTER = 'member_filter' - T = TypeVar("T", bound="EnumMeta") + if TYPE_CHECKING: + T = TypeVar("T", bound="EnumMeta") - def __new__(mtc: type[T], name: str, bases: tuple[type, ...], attr: dict[str, Any], **kwds: Any) -> T: + def __new__(mtc: type[T], name: str, bases: tuple[type, ...], attr: enum._EnumDict, **kwds: Any) -> T: member_names: list[str] = attr._member_names # type: ignore member_filter = attr.pop(mtc.MEMBER_FILTER, None) if member_filter: @@ -107,9 +111,6 @@ class PatternEnum(enum.Enum, metaclass=EnumMeta): type. """ - if TYPE_CHECKING: - C = TypeVar("C", bound="PatternEnum") - @classmethod def _missing_(cls: type[C], key: Any) -> C: return cls[key] -- GitLab