@@ -29,7 +29,23 @@ class ModelConfiguration:
2929 def _validate_types (self ) -> None :
3030 for field in fields (type (self )):
3131 try :
32- typeguard .check_type (getattr (self , field .name ), field .type )
32+ if isinstance (field .type , str ):
33+ # Later Python versions use forward references,
34+ # and then field.type is a str object.
35+ # This needs to be resolved to the type itself.
36+ # At the moment is does not look like there is typeguard support for it, thus manual fixes.
37+ # C.f. https://github.com/agronholm/typeguard/issues/358
38+ from typeguard ._functions import check_type_internal
39+ from typeguard ._memo import TypeCheckMemo
40+ from typing import ForwardRef
41+ import sys
42+
43+ cls_globals = vars (sys .modules [self .__class__ .__module__ ])
44+ memo = TypeCheckMemo (globals = cls_globals , locals = cls_globals )
45+ t = ForwardRef (field .type )
46+ check_type_internal (getattr (self , field .name ), t , memo )
47+ else :
48+ typeguard .check_type (getattr (self , field .name ), field .type )
3349 except typeguard .TypeCheckError as exc :
3450 raise typeguard .TypeCheckError (f'In field "{ field .name } " of "{ type (self )} ": { exc } ' )
3551
0 commit comments