forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmypy_util.py
31 lines (23 loc) · 847 Bytes
/
dmypy_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Shared code between dmypy.py and dmypy_server.py.
This should be pretty lightweight and not depend on other mypy code (other than ipc).
"""
import json
from typing import Any
from typing_extensions import Final
from mypy.ipc import IPCBase
DEFAULT_STATUS_FILE: Final = ".dmypy.json"
def receive(connection: IPCBase) -> Any:
"""Receive JSON data from a connection until EOF.
Raise OSError if the data received is not valid JSON or if it is
not a dict.
"""
bdata = connection.read()
if not bdata:
raise OSError("No data received")
try:
data = json.loads(bdata.decode('utf8'))
except Exception as e:
raise OSError("Data received is not valid JSON") from e
if not isinstance(data, dict):
raise OSError("Data received is not a dict (%s)" % str(type(data)))
return data