@@ -26,21 +26,37 @@ def __hash__(self):
2626 return hash (self .id_ )
2727
2828
29- class CustomAnnotationNode (AnnotationNode ):
29+ class ConcreteAnnotationNode (AnnotationNode ):
3030 def __init__ (self , symbol_node ):
3131 if isinstance (symbol_node , mypy .nodes .TypeInfo ):
32- if symbol_node .is_protocol :
33- annotation_type = "Protocol"
34- else :
35- annotation_type = "Concrete"
36- super ().__init__ (annotation_type , id (symbol_node ))
32+ assert not symbol_node .is_protocol
33+ super ().__init__ ("Concrete" , id (symbol_node ))
3734 self .module = symbol_node .module_name
3835 self .simple_name = symbol_node ._fullname .split ('.' )[- 1 ]
3936 else :
4037 assert False , "Some SymbolNode wasn't considered"
4138
4239 def encode (self ):
43- return dict (super ().encode (), ** {"module" : self .module , "simpleName" : self .simple_name })
40+ superclass_dict = super ().encode ()
41+ subclass_dict = {"module" : self .module , "simpleName" : self .simple_name }
42+ return dict (superclass_dict , ** subclass_dict )
43+
44+
45+ class ProtocolAnnotationNode (AnnotationNode ):
46+ def __init__ (self , symbol_node ):
47+ if isinstance (symbol_node , mypy .nodes .TypeInfo ):
48+ assert symbol_node .is_protocol
49+ super ().__init__ ("Protocol" , id (symbol_node ))
50+ self .module = symbol_node .module_name
51+ self .simple_name = symbol_node ._fullname .split ('.' )[- 1 ]
52+ self .members = symbol_node .protocol_members
53+ else :
54+ assert False , "Some SymbolNode wasn't considered"
55+
56+ def encode (self ):
57+ superclass_dict = super ().encode ()
58+ subclass_dict = {"module" : self .module , "simpleName" : self .simple_name , "members" : self .members }
59+ return dict (superclass_dict , ** subclass_dict )
4460
4561
4662class Annotation :
@@ -68,8 +84,10 @@ def get_annotation(mypy_type) -> Annotation:
6884 cur_node = AnnotationNode ("List" , id (mypy_type .type ))
6985 elif mypy_type .type ._fullname == "builtins.dict" :
7086 cur_node = AnnotationNode ("Dict" , id (mypy_type .type ))
87+ elif mypy_type .type .is_protocol :
88+ cur_node = ProtocolAnnotationNode (mypy_type .type ) # mypy_type.type: mypy.nodes.TypeInfo
7189 else :
72- cur_node = CustomAnnotationNode (mypy_type .type ) # mypy_type.type: mypy.nodes.TypeInfo
90+ cur_node = ConcreteAnnotationNode (mypy_type .type ) # mypy_type.type: mypy.nodes.TypeInfo
7391
7492 annotation_node_set .add (cur_node )
7593 return Annotation (cur_node .id_ , children )
0 commit comments