1- # ignoring mypy on the import as it catches ForwardRef as invalid, and we are using it
2- # for introspection https://docs.python.org/3/library/typing.html#typing.ForwardRef
3- from typing import Any , Dict , ForwardRef , List , Mapping # type: ignore
1+ import typing
2+ from typing import Any , Dict , List , Mapping
43
54from .exceptions import InvalidRequest
65
@@ -57,7 +56,7 @@ def _field_to_type(field: Any, key: str, classes: Dict[str, Any]) -> Any:
5756 if field in [int , float , str , bool ]:
5857 return field
5958 # If it's a ForwardRef we need to find base type
60- if isinstance (field , ForwardRef ):
59+ if isinstance (field , get_forward_ref_type () ):
6160 # Assuming codegen added an _ as a prefix, removing it and then gettting the
6261 # class from model classes
6362 return classes [field .__forward_arg__ [1 :]]
@@ -77,11 +76,21 @@ def _field_to_type(field: Any, key: str, classes: Dict[str, Any]) -> Any:
7776 if field in [int , float , str , bool ]:
7877 return field
7978 # If it's a ForwardRef we need to find base type
80- if isinstance (field , ForwardRef ):
79+ if isinstance (field , get_forward_ref_type () ):
8180 # Assuming codegen added an _ as a prefix, removing it and then gettting the
8281 # class from model classes
8382 return classes [field .__forward_arg__ [1 :]]
8483 # If it's not a type we don't know how to handle we bail
85- if field . _name not in [ " Sequence"]: # pylint: disable=protected-access
86- raise InvalidRequest (f"Cannot process type { field . __repr__ () } for field { key } " )
84+ if not str ( field ). startswith ( "typing. Sequence"):
85+ raise InvalidRequest (f"Cannot process type { field } for field { key } " )
8786 return _field_to_type (field .__args__ [0 ], key , classes )
87+
88+
89+ # pylint: disable=protected-access,no-member
90+ def get_forward_ref_type () -> Any :
91+ # ignoring mypy on the import as it catches (_)ForwardRef as invalid, use for
92+ # introspection is valid:
93+ # https://docs.python.org/3/library/typing.html#typing.ForwardRef
94+ if "ForwardRef" in dir (typing ):
95+ return typing .ForwardRef # type: ignore
96+ return typing ._ForwardRef # type: ignore
0 commit comments