13
13
from mypy import experiments
14
14
from mypy .nodes import (
15
15
INVARIANT , SymbolNode , ARG_POS , ARG_OPT , ARG_STAR , ARG_STAR2 , ARG_NAMED , ARG_NAMED_OPT ,
16
+ FuncDef
16
17
)
17
18
from mypy .sharedparse import argument_elide_name
18
19
from mypy .util import IdMapper
@@ -676,6 +677,8 @@ class CallableType(FunctionLike):
676
677
# instantiation?
677
678
'bound_args' , # Bound type args, mostly unused but may be useful for
678
679
# tools that consume mypy ASTs
680
+ 'def_extras' , # Information about original definition we want to serialize.
681
+ # This is used for more detailed error messages.
679
682
)
680
683
681
684
def __init__ (self ,
@@ -695,6 +698,7 @@ def __init__(self,
695
698
special_sig : Optional [str ] = None ,
696
699
from_type_type : bool = False ,
697
700
bound_args : Sequence [Optional [Type ]] = (),
701
+ def_extras : Optional [Dict [str , Any ]] = None ,
698
702
) -> None :
699
703
super ().__init__ (line , column )
700
704
assert len (arg_types ) == len (arg_kinds ) == len (arg_names )
@@ -721,6 +725,18 @@ def __init__(self,
721
725
if not bound_args :
722
726
bound_args = ()
723
727
self .bound_args = bound_args
728
+ if def_extras :
729
+ self .def_extras = def_extras
730
+ elif isinstance (definition , FuncDef ):
731
+ # This information would be lost if we don't have definition
732
+ # after serialization, but it is useful in error messages.
733
+ # TODO: decide how to add more info here (file, line, column)
734
+ # without changing interface hash.
735
+ self .def_extras = {'first_arg' : definition .arg_names [0 ]
736
+ if definition .arg_names and definition .info and
737
+ not definition .is_static else None }
738
+ else :
739
+ self .def_extras = {}
724
740
725
741
def copy_modified (self ,
726
742
arg_types : List [Type ] = _dummy ,
@@ -736,7 +752,8 @@ def copy_modified(self,
736
752
is_ellipsis_args : bool = _dummy ,
737
753
special_sig : Optional [str ] = _dummy ,
738
754
from_type_type : bool = _dummy ,
739
- bound_args : List [Optional [Type ]] = _dummy ) -> 'CallableType' :
755
+ bound_args : List [Optional [Type ]] = _dummy ,
756
+ def_extras : Dict [str , Any ] = _dummy ) -> 'CallableType' :
740
757
return CallableType (
741
758
arg_types = arg_types if arg_types is not _dummy else self .arg_types ,
742
759
arg_kinds = arg_kinds if arg_kinds is not _dummy else self .arg_kinds ,
@@ -755,6 +772,7 @@ def copy_modified(self,
755
772
special_sig = special_sig if special_sig is not _dummy else self .special_sig ,
756
773
from_type_type = from_type_type if from_type_type is not _dummy else self .from_type_type ,
757
774
bound_args = bound_args if bound_args is not _dummy else self .bound_args ,
775
+ def_extras = def_extras if def_extras is not _dummy else dict (self .def_extras ),
758
776
)
759
777
760
778
def is_type_obj (self ) -> bool :
@@ -907,6 +925,7 @@ def serialize(self) -> JsonDict:
907
925
'is_classmethod_class' : self .is_classmethod_class ,
908
926
'bound_args' : [(None if t is None else t .serialize ())
909
927
for t in self .bound_args ],
928
+ 'def_extras' : dict (self .def_extras ),
910
929
}
911
930
912
931
@classmethod
@@ -925,6 +944,7 @@ def deserialize(cls, data: JsonDict) -> 'CallableType':
925
944
is_classmethod_class = data ['is_classmethod_class' ],
926
945
bound_args = [(None if t is None else deserialize_type (t ))
927
946
for t in data ['bound_args' ]],
947
+ def_extras = data ['def_extras' ]
928
948
)
929
949
930
950
0 commit comments