Deserialize a JSON file containing runtime collected types. The input JSON is expected to to have a list of RawEntry items.
(path)
| 94 | |
| 95 | |
| 96 | def 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 | |
| 136 | class Token(object): |