dmypy_util.py 875 B

1234567891011121314151617181920212223242526272829303132
  1. """Shared code between dmypy.py and dmypy_server.py.
  2. This should be pretty lightweight and not depend on other mypy code (other than ipc).
  3. """
  4. from __future__ import annotations
  5. import json
  6. from typing import Any
  7. from typing_extensions import Final
  8. from mypy.ipc import IPCBase
  9. DEFAULT_STATUS_FILE: Final = ".dmypy.json"
  10. def receive(connection: IPCBase) -> Any:
  11. """Receive JSON data from a connection until EOF.
  12. Raise OSError if the data received is not valid JSON or if it is
  13. not a dict.
  14. """
  15. bdata = connection.read()
  16. if not bdata:
  17. raise OSError("No data received")
  18. try:
  19. data = json.loads(bdata.decode("utf8"))
  20. except Exception as e:
  21. raise OSError("Data received is not valid JSON") from e
  22. if not isinstance(data, dict):
  23. raise OSError(f"Data received is not a dict ({type(data)})")
  24. return data