4343 node_kinds ,
4444)
4545from mypy .types import (
46+ NOT_READY ,
4647 AnyType ,
4748 CallableType ,
49+ ExtraAttrs ,
4850 Instance ,
4951 LiteralType ,
5052 NoneType ,
6567 get_proper_type ,
6668)
6769
68- JsonDict : _TypeAlias = dict [str , Any ]
70+ Json : _TypeAlias = dict [str , Any ] | str
6971
7072
7173class Config :
7274 def __init__ (self , * , implicit_names : bool = True ) -> None :
7375 self .implicit_names = implicit_names
7476
7577
76- def convert_binary_cache_to_json (data : bytes , * , implicit_names : bool = True ) -> JsonDict :
78+ def convert_binary_cache_to_json (data : bytes , * , implicit_names : bool = True ) -> Json :
7779 tree = MypyFile .read (Buffer (data ))
7880 return convert_mypy_file_to_json (tree , Config (implicit_names = implicit_names ))
7981
8082
81- def convert_mypy_file_to_json (self : MypyFile , cfg : Config ) -> JsonDict :
83+ def convert_mypy_file_to_json (self : MypyFile , cfg : Config ) -> Json :
8284 return {
8385 ".class" : "MypyFile" ,
8486 "_fullname" : self ._fullname ,
@@ -90,8 +92,8 @@ def convert_mypy_file_to_json(self: MypyFile, cfg: Config) -> JsonDict:
9092 }
9193
9294
93- def convert_symbol_table (self : SymbolTable , cfg : Config ) -> JsonDict :
94- data : JsonDict = {".class" : "SymbolTable" }
95+ def convert_symbol_table (self : SymbolTable , cfg : Config ) -> Json :
96+ data : Json = {".class" : "SymbolTable" }
9597 for key , value in self .items ():
9698 # Skip __builtins__: it's a reference to the builtins
9799 # module that gets added to every module by
@@ -112,8 +114,8 @@ def convert_symbol_table(self: SymbolTable, cfg: Config) -> JsonDict:
112114 return data
113115
114116
115- def convert_symbol_table_node (self : SymbolTableNode , cfg : Config ) -> JsonDict :
116- data : JsonDict = {".class" : "SymbolTableNode" , "kind" : node_kinds [self .kind ]}
117+ def convert_symbol_table_node (self : SymbolTableNode , cfg : Config ) -> Json :
118+ data : Json = {".class" : "SymbolTableNode" , "kind" : node_kinds [self .kind ]}
117119 if self .module_hidden :
118120 data ["module_hidden" ] = True
119121 if not self .module_public :
@@ -129,7 +131,7 @@ def convert_symbol_table_node(self: SymbolTableNode, cfg: Config) -> JsonDict:
129131 return data
130132
131133
132- def convert_symbol_node (self : SymbolNode , cfg : Config ) -> JsonDict :
134+ def convert_symbol_node (self : SymbolNode , cfg : Config ) -> Json :
133135 if isinstance (self , FuncDef ):
134136 return convert_func_def (self )
135137 elif isinstance (self , OverloadedFuncDef ):
@@ -151,7 +153,7 @@ def convert_symbol_node(self: SymbolNode, cfg: Config) -> JsonDict:
151153 assert False , type (self )
152154
153155
154- def convert_func_def (self : FuncDef ) -> JsonDict :
156+ def convert_func_def (self : FuncDef ) -> Json :
155157 return {
156158 ".class" : "FuncDef" ,
157159 "name" : self ._name ,
@@ -172,7 +174,7 @@ def convert_func_def(self: FuncDef) -> JsonDict:
172174 }
173175
174176
175- def convert_dataclass_transform_spec (self : DataclassTransformSpec ) -> JsonDict :
177+ def convert_dataclass_transform_spec (self : DataclassTransformSpec ) -> Json :
176178 return {
177179 "eq_default" : self .eq_default ,
178180 "order_default" : self .order_default ,
@@ -182,7 +184,7 @@ def convert_dataclass_transform_spec(self: DataclassTransformSpec) -> JsonDict:
182184 }
183185
184186
185- def convert_overloaded_func_def (self : OverloadedFuncDef ) -> JsonDict :
187+ def convert_overloaded_func_def (self : OverloadedFuncDef ) -> Json :
186188 return {
187189 ".class" : "OverloadedFuncDef" ,
188190 "items" : [convert_overload_part (i ) for i in self .items ],
@@ -195,14 +197,14 @@ def convert_overloaded_func_def(self: OverloadedFuncDef) -> JsonDict:
195197 }
196198
197199
198- def convert_overload_part (self : OverloadPart ) -> JsonDict :
200+ def convert_overload_part (self : OverloadPart ) -> Json :
199201 if isinstance (self , FuncDef ):
200202 return convert_func_def (self )
201203 else :
202204 return convert_decorator (self )
203205
204206
205- def convert_decorator (self : Decorator ) -> JsonDict :
207+ def convert_decorator (self : Decorator ) -> Json :
206208 return {
207209 ".class" : "Decorator" ,
208210 "func" : convert_func_def (self .func ),
@@ -211,8 +213,8 @@ def convert_decorator(self: Decorator) -> JsonDict:
211213 }
212214
213215
214- def convert_var (self : Var ) -> JsonDict :
215- data : JsonDict = {
216+ def convert_var (self : Var ) -> Json :
217+ data : Json = {
216218 ".class" : "Var" ,
217219 "name" : self ._name ,
218220 "fullname" : self ._fullname ,
@@ -225,7 +227,7 @@ def convert_var(self: Var) -> JsonDict:
225227 return data
226228
227229
228- def convert_type_info (self : TypeInfo , cfg : Config ) -> JsonDict :
230+ def convert_type_info (self : TypeInfo , cfg : Config ) -> Json :
229231 data = {
230232 ".class" : "TypeInfo" ,
231233 "module_name" : self .module_name ,
@@ -264,7 +266,7 @@ def convert_type_info(self: TypeInfo, cfg: Config) -> JsonDict:
264266 return data
265267
266268
267- def convert_class_def (self : ClassDef ) -> JsonDict :
269+ def convert_class_def (self : ClassDef ) -> Json :
268270 return {
269271 ".class" : "ClassDef" ,
270272 "name" : self .name ,
@@ -273,8 +275,8 @@ def convert_class_def(self: ClassDef) -> JsonDict:
273275 }
274276
275277
276- def convert_type_alias (self : TypeAlias ) -> JsonDict :
277- data : JsonDict = {
278+ def convert_type_alias (self : TypeAlias ) -> Json :
279+ data : Json = {
278280 ".class" : "TypeAlias" ,
279281 "fullname" : self ._fullname ,
280282 "module" : self .module ,
@@ -287,7 +289,7 @@ def convert_type_alias(self: TypeAlias) -> JsonDict:
287289 return data
288290
289291
290- def convert_type_var_expr (self : TypeVarExpr ) -> JsonDict :
292+ def convert_type_var_expr (self : TypeVarExpr ) -> Json :
291293 return {
292294 ".class" : "TypeVarExpr" ,
293295 "name" : self ._name ,
@@ -299,7 +301,7 @@ def convert_type_var_expr(self: TypeVarExpr) -> JsonDict:
299301 }
300302
301303
302- def convert_param_spec_expr (self : ParamSpecExpr ) -> JsonDict :
304+ def convert_param_spec_expr (self : ParamSpecExpr ) -> Json :
303305 return {
304306 ".class" : "ParamSpecExpr" ,
305307 "name" : self ._name ,
@@ -310,7 +312,7 @@ def convert_param_spec_expr(self: ParamSpecExpr) -> JsonDict:
310312 }
311313
312314
313- def convert_type_var_tuple_expr (self : TypeVarTupleExpr ) -> JsonDict :
315+ def convert_type_var_tuple_expr (self : TypeVarTupleExpr ) -> Json :
314316 return {
315317 ".class" : "TypeVarTupleExpr" ,
316318 "name" : self ._name ,
@@ -322,7 +324,7 @@ def convert_type_var_tuple_expr(self: TypeVarTupleExpr) -> JsonDict:
322324 }
323325
324326
325- def convert_type (typ : Type ) -> JsonDict :
327+ def convert_type (typ : Type ) -> Json :
326328 if type (typ ) is TypeAliasType :
327329 return convert_type_alias_type (typ )
328330 typ = get_proper_type (typ )
@@ -363,25 +365,41 @@ def convert_type(typ: Type) -> JsonDict:
363365 assert False , type (typ )
364366
365367
366- def convert_instance (self : Instance ) -> JsonDict :
367- data : JsonDict = {
368+ def convert_instance (self : Instance ) -> Json :
369+ ready = self .type is not NOT_READY
370+ if not self .args and not self .last_known_value and not self .extra_attrs :
371+ return self .type .fullname if ready else self .type_ref
372+
373+ data : Json = {
368374 ".class" : "Instance" ,
369- "type_ref" : self .type_ref ,
375+ "type_ref" : self .type . fullname if ready else self . type_ref ,
370376 "args" : [convert_type (arg ) for arg in self .args ],
371377 }
378+ if self .last_known_value is not None :
379+ data ["last_known_value" ] = convert_type (self .last_known_value )
380+ data ["extra_attrs" ] = convert_extra_attrs (self .extra_attrs ) if self .extra_attrs else None
372381 return data
373382
374383
375- def convert_type_alias_type (self : TypeAliasType ) -> JsonDict :
376- data : JsonDict = {
384+ def convert_extra_attrs (self : ExtraAttrs ) -> Json :
385+ return {
386+ ".class" : "ExtraAttrs" ,
387+ "attrs" : {k : convert_type (v ) for k , v in self .attrs .items ()},
388+ "immutable" : sorted (self .immutable ),
389+ "mod_name" : self .mod_name ,
390+ }
391+
392+
393+ def convert_type_alias_type (self : TypeAliasType ) -> Json :
394+ data : Json = {
377395 ".class" : "TypeAliasType" ,
378396 "type_ref" : self .type_ref ,
379397 "args" : [convert_type (arg ) for arg in self .args ],
380398 }
381399 return data
382400
383401
384- def convert_any_type (self : AnyType ) -> JsonDict :
402+ def convert_any_type (self : AnyType ) -> Json :
385403 return {
386404 ".class" : "AnyType" ,
387405 "type_of_any" : self .type_of_any ,
@@ -390,19 +408,19 @@ def convert_any_type(self: AnyType) -> JsonDict:
390408 }
391409
392410
393- def convert_none_type (self : NoneType ) -> JsonDict :
411+ def convert_none_type (self : NoneType ) -> Json :
394412 return {".class" : "NoneType" }
395413
396414
397- def convert_union_type (self : UnionType ) -> JsonDict :
415+ def convert_union_type (self : UnionType ) -> Json :
398416 return {
399417 ".class" : "UnionType" ,
400418 "items" : [convert_type (t ) for t in self .items ],
401419 "uses_pep604_syntax" : self .uses_pep604_syntax ,
402420 }
403421
404422
405- def convert_tuple_type (self : TupleType ) -> JsonDict :
423+ def convert_tuple_type (self : TupleType ) -> Json :
406424 return {
407425 ".class" : "TupleType" ,
408426 "items" : [convert_type (t ) for t in self .items ],
@@ -411,11 +429,11 @@ def convert_tuple_type(self: TupleType) -> JsonDict:
411429 }
412430
413431
414- def convert_literal_type (self : LiteralType ) -> JsonDict :
432+ def convert_literal_type (self : LiteralType ) -> Json :
415433 return {".class" : "LiteralType" , "value" : self .value , "fallback" : convert_type (self .fallback )}
416434
417435
418- def convert_type_var_type (self : TypeVarType ) -> JsonDict :
436+ def convert_type_var_type (self : TypeVarType ) -> Json :
419437 assert not self .id .is_meta_var ()
420438 return {
421439 ".class" : "TypeVarType" ,
@@ -430,7 +448,7 @@ def convert_type_var_type(self: TypeVarType) -> JsonDict:
430448 }
431449
432450
433- def convert_callable_type (self : CallableType ) -> JsonDict :
451+ def convert_callable_type (self : CallableType ) -> Json :
434452 return {
435453 ".class" : "CallableType" ,
436454 "arg_types" : [convert_type (t ) for t in self .arg_types ],
@@ -452,23 +470,23 @@ def convert_callable_type(self: CallableType) -> JsonDict:
452470 }
453471
454472
455- def convert_overloaded (self : Overloaded ) -> JsonDict :
473+ def convert_overloaded (self : Overloaded ) -> Json :
456474 return {".class" : "Overloaded" , "items" : [convert_type (t ) for t in self .items ]}
457475
458476
459- def convert_type_type (self : TypeType ) -> JsonDict :
477+ def convert_type_type (self : TypeType ) -> Json :
460478 return {".class" : "TypeType" , "item" : convert_type (self .item )}
461479
462480
463- def convert_uninhabited_type (self : UninhabitedType ) -> JsonDict :
481+ def convert_uninhabited_type (self : UninhabitedType ) -> Json :
464482 return {".class" : "UninhabitedType" }
465483
466484
467- def convert_unpack_type (self : UnpackType ) -> JsonDict :
485+ def convert_unpack_type (self : UnpackType ) -> Json :
468486 return {".class" : "UnpackType" , "type" : convert_type (self .type )}
469487
470488
471- def convert_param_spec_type (self : ParamSpecType ) -> JsonDict :
489+ def convert_param_spec_type (self : ParamSpecType ) -> Json :
472490 assert not self .id .is_meta_var ()
473491 return {
474492 ".class" : "ParamSpecType" ,
@@ -483,7 +501,7 @@ def convert_param_spec_type(self: ParamSpecType) -> JsonDict:
483501 }
484502
485503
486- def convert_type_var_tuple_type (self : TypeVarTupleType ) -> JsonDict :
504+ def convert_type_var_tuple_type (self : TypeVarTupleType ) -> Json :
487505 assert not self .id .is_meta_var ()
488506 return {
489507 ".class" : "TypeVarTupleType" ,
@@ -498,7 +516,7 @@ def convert_type_var_tuple_type(self: TypeVarTupleType) -> JsonDict:
498516 }
499517
500518
501- def convert_parameters (self : Parameters ) -> JsonDict :
519+ def convert_parameters (self : Parameters ) -> Json :
502520 return {
503521 ".class" : "Parameters" ,
504522 "arg_types" : [convert_type (t ) for t in self .arg_types ],
@@ -509,7 +527,7 @@ def convert_parameters(self: Parameters) -> JsonDict:
509527 }
510528
511529
512- def convert_typeddict_type (self : TypedDictType ) -> JsonDict :
530+ def convert_typeddict_type (self : TypedDictType ) -> Json :
513531 return {
514532 ".class" : "TypedDictType" ,
515533 "items" : [[n , convert_type (t )] for (n , t ) in self .items .items ()],
@@ -519,7 +537,7 @@ def convert_typeddict_type(self: TypedDictType) -> JsonDict:
519537 }
520538
521539
522- def convert_unbound_type (self : UnboundType ) -> JsonDict :
540+ def convert_unbound_type (self : UnboundType ) -> Json :
523541 return {
524542 ".class" : "UnboundType" ,
525543 "name" : self .name ,
0 commit comments