A simple helper to read a JSON file with logging.
(
file: str, manager: BuildManager, log_success: str, log_error: str
)
| 1836 | |
| 1837 | |
| 1838 | def _load_json_file( |
| 1839 | file: str, manager: BuildManager, log_success: str, log_error: str |
| 1840 | ) -> dict[str, Any] | None: |
| 1841 | """A simple helper to read a JSON file with logging.""" |
| 1842 | t0 = time.time() |
| 1843 | try: |
| 1844 | data = manager.metastore.read(file) |
| 1845 | except OSError: |
| 1846 | manager.log(log_error + file) |
| 1847 | return None |
| 1848 | manager.add_stats(metastore_read_time=time.time() - t0) |
| 1849 | # Only bother to compute the log message if we are logging it, since it could be big |
| 1850 | if manager.verbosity() >= 2: |
| 1851 | manager.trace(log_success + data.rstrip().decode()) |
| 1852 | try: |
| 1853 | t1 = time.time() |
| 1854 | result = json_loads(data) |
| 1855 | manager.add_stats(data_file_load_time=time.time() - t1) |
| 1856 | except json.JSONDecodeError: |
| 1857 | manager.errors.set_file(file, None, manager.options) |
| 1858 | manager.error( |
| 1859 | None, |
| 1860 | "Error reading JSON file;" |
| 1861 | " you likely have a bad cache.\n" |
| 1862 | "Try removing the {cache_dir} directory" |
| 1863 | " and run mypy again.".format(cache_dir=manager.options.cache_dir), |
| 1864 | blocker=True, |
| 1865 | ) |
| 1866 | return None |
| 1867 | else: |
| 1868 | assert isinstance(result, dict) |
| 1869 | return result |
| 1870 | |
| 1871 | |
| 1872 | def _cache_dir_prefix(options: Options) -> str: |
no test coverage detected
searching dependent graphs…