MCPcopy Create free account
hub / github.com/dropbox/pyannotate / parse_json

Function parse_json

pyannotate_tools/annotations/parse.py:96–133  ·  view source on GitHub ↗

Deserialize a JSON file containing runtime collected types. The input JSON is expected to to have a list of RawEntry items.

(path)

Source from the content-addressed store, hash-verified

94
95
96def parse_json(path):
97 # type: (str) -> List[FunctionInfo]
98 """Deserialize a JSON file containing runtime collected types.
99
100 The input JSON is expected to to have a list of RawEntry items.
101 """
102 with open(path) as f:
103 data = json.load(f) # type: List[RawEntry]
104 result = []
105
106 def assert_type(value, typ):
107 # type: (object, type) -> None
108 assert isinstance(value, typ), '%s: Unexpected type %r' % (path, type(value).__name__)
109
110 def assert_dict_item(dictionary, key, typ):
111 # type: (Mapping[Any, Any], str, type) -> None
112 assert key in dictionary, '%s: Missing dictionary key %r' % (path, key)
113 value = dictionary[key]
114 assert isinstance(value, typ), '%s: Unexpected type %r for key %r' % (
115 path, type(value).__name__, key)
116
117 assert_type(data, list)
118 for item in data:
119 assert_type(item, dict)
120 assert_dict_item(item, 'path', Text)
121 assert_dict_item(item, 'line', int)
122 assert_dict_item(item, 'func_name', Text)
123 assert_dict_item(item, 'type_comments', list)
124 for comment in item['type_comments']:
125 assert_type(comment, Text)
126 assert_type(item['samples'], int)
127 info = FunctionInfo(encode(item['path']),
128 item['line'],
129 encode(item['func_name']),
130 [encode(comment) for comment in item['type_comments']],
131 item['samples'])
132 result.append(info)
133 return result
134
135
136class Token(object):

Callers 2

test_parse_jsonMethod · 0.90

Calls 4

assert_typeFunction · 0.85
assert_dict_itemFunction · 0.85
FunctionInfoClass · 0.85
encodeFunction · 0.85

Tested by 1

test_parse_jsonMethod · 0.72