Skip to content

Commit 308025b

Browse files
authored
Merge pull request #16 from desultory/dev
Get type hints when a forward reference is used
2 parents bd9ee5e + e4c792d commit 308025b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "zenlib"
7-
version = "3.1.4"
7+
version = "3.1.5"
88
authors = [
99
{ name="Desultory", email="[email protected]" },
1010
]

src/zenlib/types/validated_dataclass.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from dataclasses import dataclass
2+
from typing import ForwardRef, Union, get_args, get_origin, get_type_hints
23

34

45
def validatedDataclass(cls):
56
from zenlib.logging import loggify
67
from zenlib.util import merge_class
78

8-
99
cls = loggify(dataclass(cls))
1010
base_annotations = {}
1111
for base in cls.__mro__:
@@ -28,6 +28,9 @@ def _validate_attribute(self, attribute, value):
2828
expected_type = self.__class__.__annotations__.get(attribute)
2929
if not expected_type:
3030
return value # No type hint, so we can't validate it
31+
if get_origin(expected_type) is Union and isinstance(get_args(expected_type)[0], ForwardRef):
32+
expected_type = get_type_hints(self.__class__)[attribute]
33+
3134
if not isinstance(value, expected_type):
3235
try:
3336
value = expected_type(value)

0 commit comments

Comments
 (0)