Skip to content

Commit 2eb02bd

Browse files
committed
Fixed Protocol serialization
1 parent 6f760b1 commit 2eb02bd

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

utbot-python/src/main/kotlin/org/utbot/python/typing/PythonAnnotations.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ open class ConcreteAnnotation(
8080
}
8181

8282
open class Protocol(
83-
val methods: List<String>,
83+
val members: List<String>,
8484
module: String,
8585
simpleName: String,
8686
type: AnnotationType = AnnotationType.Protocol,
@@ -177,7 +177,7 @@ fun main() {
177177
println(obj3.annotations[0].normalizedRepr)
178178

179179
val test4 = """
180-
{"nodeStorage": {"140501235228384": {"type": "Concrete", "module": "builtins", "simpleName": "int"}, "140501242115552": {"type": "NoneType"}, "140501250114464": {"type": "Union"}}, "annotations": [{"nodeId": "140501250114464", "args": [{"nodeId": "140501235228384"}, {"nodeId": "140501242115552"}]}]}
180+
{"nodeStorage": {"139974471573216": {"type": "Concrete", "module": "builtins", "simpleName": "int"}, "139974471470816": {"type": "Protocol", "module": "typing", "simpleName": "Iterable", "members": ["__iter__"]}, "139974486047872": {"type": "Any"}}, "annotations": [{"nodeId": "139974471470816", "args": [{"nodeId": "139974471573216"}]}, {"nodeId": "139974486047872"}]}
181181
""".trimIndent()
182182
val obj4 = jsonAdapter.fromJson(test4)!!
183183
println(obj4)

utbot-python/src/main/resources/new_normalize_annotation_from_project.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4662
class 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

Comments
 (0)