@@ -653,23 +653,16 @@ def verify_funcitem(
653
653
yield Error (object_path , "is not present at runtime" , stub , runtime )
654
654
return
655
655
656
- if (
657
- not isinstance (runtime , (types .FunctionType , types .BuiltinFunctionType ))
658
- and not isinstance (runtime , (types .MethodType , types .BuiltinMethodType ))
659
- and not inspect .ismethoddescriptor (runtime )
660
- ):
656
+ if not is_probably_a_function (runtime ):
661
657
yield Error (object_path , "is not a function" , stub , runtime )
662
658
if not callable (runtime ):
663
659
return
664
660
665
661
for message in _verify_static_class_methods (stub , runtime , object_path ):
666
662
yield Error (object_path , "is inconsistent, " + message , stub , runtime )
667
663
668
- try :
669
- signature = inspect .signature (runtime )
670
- except (ValueError , RuntimeError ):
671
- # inspect.signature throws sometimes
672
- # catch RuntimeError because of https://bugs.python.org/issue39504
664
+ signature = safe_inspect_signature (runtime )
665
+ if not signature :
673
666
return
674
667
675
668
stub_sig = Signature .from_funcitem (stub )
@@ -737,21 +730,16 @@ def verify_overloadedfuncdef(
737
730
# We get here in cases of overloads from property.setter
738
731
return
739
732
740
- if (
741
- not isinstance (runtime , (types .FunctionType , types .BuiltinFunctionType ))
742
- and not isinstance (runtime , (types .MethodType , types .BuiltinMethodType ))
743
- and not inspect .ismethoddescriptor (runtime )
744
- ):
733
+ if not is_probably_a_function (runtime ):
745
734
yield Error (object_path , "is not a function" , stub , runtime )
746
735
if not callable (runtime ):
747
736
return
748
737
749
738
for message in _verify_static_class_methods (stub , runtime , object_path ):
750
739
yield Error (object_path , "is inconsistent, " + message , stub , runtime )
751
740
752
- try :
753
- signature = inspect .signature (runtime )
754
- except ValueError :
741
+ signature = safe_inspect_signature (runtime )
742
+ if not signature :
755
743
return
756
744
757
745
stub_sig = Signature .from_overloadedfuncdef (stub )
@@ -888,6 +876,24 @@ def is_dunder(name: str, exclude_special: bool = False) -> bool:
888
876
return name .startswith ("__" ) and name .endswith ("__" )
889
877
890
878
879
+ def is_probably_a_function (runtime : Any ) -> bool :
880
+ return (
881
+ isinstance (runtime , (types .FunctionType , types .BuiltinFunctionType ))
882
+ or isinstance (runtime , (types .MethodType , types .BuiltinMethodType ))
883
+ or (inspect .ismethoddescriptor (runtime ) and callable (runtime ))
884
+ )
885
+
886
+
887
+ def safe_inspect_signature (runtime : Any ) -> Optional [inspect .Signature ]:
888
+ try :
889
+ return inspect .signature (runtime )
890
+ except (ValueError , RuntimeError , TypeError ):
891
+ # inspect.signature throws sometimes
892
+ # catch RuntimeError because of https://bugs.python.org/issue39504
893
+ # catch TypeError because of https://github.com/python/typeshed/pull/5762
894
+ return None
895
+
896
+
891
897
def is_subtype_helper (left : mypy .types .Type , right : mypy .types .Type ) -> bool :
892
898
"""Checks whether ``left`` is a subtype of ``right``."""
893
899
left = mypy .types .get_proper_type (left )
@@ -930,8 +936,8 @@ def anytype() -> mypy.types.AnyType:
930
936
type_info = builtins .names ["function" ].node
931
937
assert isinstance (type_info , nodes .TypeInfo )
932
938
fallback = mypy .types .Instance (type_info , [anytype ()])
933
- try :
934
- signature = inspect . signature ( runtime )
939
+ signature = safe_inspect_signature ( runtime )
940
+ if signature :
935
941
arg_types = []
936
942
arg_kinds = []
937
943
arg_names = []
@@ -953,7 +959,7 @@ def anytype() -> mypy.types.AnyType:
953
959
arg_kinds .append (nodes .ARG_STAR2 )
954
960
else :
955
961
raise AssertionError
956
- except ValueError :
962
+ else :
957
963
arg_types = [anytype (), anytype ()]
958
964
arg_kinds = [nodes .ARG_STAR , nodes .ARG_STAR2 ]
959
965
arg_names = [None , None ]
0 commit comments