@@ -578,13 +578,12 @@ def _unpatch_attribute(patched: PatchedAttribute) -> None:
578
578
579
579
580
580
def _extract_marker (parameter : inspect .Parameter ) -> Optional ["_Marker" ]:
581
- is_annotated = (
582
- isinstance (annotation , type (Annotated ))
583
- and get_origin (annotation ) is not None
584
- and get_origin (annotation ) is get_origin (Annotated )
585
- )
586
- if is_annotated :
587
- marker = get_args (annotation )[1 ]
581
+ if get_origin (parameter .annotation ) is Annotated :
582
+ args = get_args (parameter .annotation )
583
+ if len (args ) > 1 :
584
+ marker = args [1 ]
585
+ else :
586
+ marker = None
588
587
else :
589
588
marker = parameter .default
590
589
@@ -1032,19 +1031,18 @@ def _patched(*args, **kwargs):
1032
1031
return cast (F , _patched )
1033
1032
1034
1033
1034
+ def _get_annotations (obj : Any ) -> Dict [str , Any ]:
1035
+ if sys .version_info >= (3 , 10 ):
1036
+ return inspect .get_annotations (obj )
1037
+ else :
1038
+ return getattr (obj , "__annotations__" , {})
1039
+
1040
+
1035
1041
def _get_members_and_annotated (obj : Any ) -> Iterable [Tuple [str , Any ]]:
1036
1042
members = inspect .getmembers (obj )
1037
- try :
1038
- annotations = inspect .get_annotations (obj )
1039
- except Exception :
1040
- annotations = {}
1043
+ annotations = _get_annotations (obj )
1041
1044
for annotation_name , annotation in annotations .items ():
1042
- is_annotated = (
1043
- isinstance (annotation , type (Annotated ))
1044
- and get_origin (annotation ) is not None
1045
- and get_origin (annotation ) is get_origin (Annotated )
1046
- )
1047
- if is_annotated :
1045
+ if get_origin (annotation ) is Annotated :
1048
1046
args = get_args (annotation )
1049
1047
if len (args ) > 1 :
1050
1048
member = args [1 ]
0 commit comments