-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test coverage for the Python 3.12 way to defining generic clas…
…ses (#24)
- Loading branch information
Showing
3 changed files
with
116 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, List, Optional, Union, TypeVar, Generic, Literal | ||
|
||
import attr | ||
|
||
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | ||
# Classes used by the tests | ||
|
||
class NormalClass: | ||
def __init__(self, v: str) -> None: | ||
self.value = v | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return isinstance(other, NormalClass) and self.value == other.value | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DataClass: | ||
field: str | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class AttrClass: | ||
attrib: str | ||
|
||
TTestType = TypeVar("TTestType") | ||
TSelfRef = TypeVar("TSelfRef") | ||
|
||
@dataclass | ||
class ClassBase(Generic[TTestType, TSelfRef]): | ||
normal: TTestType | ||
optional: Optional[TTestType] | ||
union: Union[str, NormalClass, DataClass, TTestType] | ||
any: Any | ||
self_ref: Optional[TSelfRef] | ||
|
||
list_normal: List[TTestType] | ||
list_optional: List[Optional[TTestType]] | ||
list_union: List[Union[str, NormalClass, DataClass, TTestType]] | ||
list_any: List[Any] | ||
list_self_ref: List[TSelfRef] | ||
|
||
dict_normal: Dict[str, TTestType] | ||
dict_optional: Dict[str, Optional[TTestType]] | ||
dict_union: Dict[str, Union[str, NormalClass, DataClass, TTestType]] | ||
dict_any: Dict[str, Any] | ||
dict_self_ref: Dict[str, TSelfRef] | ||
|
||
|
||
ClassPrimitives = ClassBase[int, "ClassPrimitives"] | ||
ClassDict = ClassBase[Dict[str,str], "ClassDict"] | ||
ClassDictSimple = ClassBase[dict, "ClassDictSimple"] | ||
ClassList = ClassBase[List[str], "ClassList"] | ||
ClassDataClass = ClassBase[DataClass, "ClassDataClass"] | ||
ClassNormalClass = ClassBase[NormalClass, "ClassNormalClass"] | ||
ClassAttrClass = ClassBase[AttrClass, "ClassAttrClass"] | ||
ClassListDataClass = ClassBase[List[DataClass], "ClassListDataClass"] | ||
ClassDictDataClass = ClassBase[Dict[str, DataClass], "ClassDictDataClass"] | ||
ClassLiteral = ClassBase[Literal["my-literal"], "ClassLiteral"] | ||
ClassMultiLiteral = ClassBase[Literal[2, 3, 5, 7, 11], "ClassMultiLiteral"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Optional, Literal | ||
|
||
|
||
from _type_checking_classes import DataClass, NormalClass, AttrClass | ||
|
||
@dataclass | ||
class ClassBase[TTestType, TSelfRef]: | ||
normal: TTestType | ||
optional: Optional[TTestType] | ||
union: str | NormalClass | DataClass | TTestType | ||
any: Any | ||
self_ref: Optional[TSelfRef] | ||
|
||
list_normal: list[TTestType] | ||
list_optional: list[Optional[TTestType]] | ||
list_union: list[str | NormalClass | DataClass | TTestType] | ||
list_any: list[Any] | ||
list_self_ref: list[TSelfRef] | ||
|
||
dict_normal: dict[str, TTestType] | ||
dict_optional: dict[str, Optional[TTestType]] | ||
dict_union: dict[str, str | NormalClass | DataClass | TTestType] | ||
dict_any: dict[str, Any] | ||
dict_self_ref: dict[str, TSelfRef] | ||
|
||
|
||
ClassPrimitives = ClassBase[int, "ClassPrimitives"] | ||
ClassDict = ClassBase[dict[str,str], "ClassDict"] | ||
ClassDictSimple = ClassBase[dict, "ClassDictSimple"] | ||
ClassList = ClassBase[list[str], "ClassList"] | ||
ClassDataClass = ClassBase[DataClass, "ClassDataClass"] | ||
ClassNormalClass = ClassBase[NormalClass, "ClassNormalClass"] | ||
ClassAttrClass = ClassBase[AttrClass, "ClassAttrClass"] | ||
ClassListDataClass = ClassBase[list[DataClass], "ClassListDataClass"] | ||
ClassDictDataClass = ClassBase[dict[str, DataClass], "ClassDictDataClass"] | ||
ClassLiteral = ClassBase[Literal["my-literal"], "ClassLiteral"] | ||
ClassMultiLiteral = ClassBase[Literal[2, 3, 5, 7, 11], "ClassMultiLiteral"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters