Skip to content

Commit fbcdc02

Browse files
Michael0x2agvanrossum
authored andcommitted
Make cache files compressed by default (#2082)
This commit modifies the cache writing process so that the JSON generated for the cache files are not indented by default for performance improvements. This commit also adds a hidden `--debug-cache` flag. When the flag is present, mypy will begin indenting the JSON cache data again to make it easier for developers to debug.
1 parent 7b3ad9d commit fbcdc02

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

mypy/build.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ def select_options_affecting_cache(options: Options) -> Mapping[str, bool]:
797797
"disallow_untyped_calls",
798798
"disallow_untyped_defs",
799799
"check_untyped_defs",
800+
"debug_cache",
800801
]
801802

802803

@@ -849,7 +850,10 @@ def write_cache(id: str, path: str, tree: MypyFile,
849850

850851
# Serialize data and analyze interface
851852
data = tree.serialize()
852-
data_str = json.dumps(data, indent=2, sort_keys=True)
853+
if manager.options.debug_cache:
854+
data_str = json.dumps(data, indent=2, sort_keys=True)
855+
else:
856+
data_str = json.dumps(data, sort_keys=True)
853857
interface_hash = compute_hash(data_str)
854858

855859
# Write data cache file, if applicable
@@ -886,8 +890,10 @@ def write_cache(id: str, path: str, tree: MypyFile,
886890

887891
# Write meta cache file
888892
with open(meta_json_tmp, 'w') as f:
889-
json.dump(meta, f, sort_keys=True)
890-
f.write('\n')
893+
if manager.options.debug_cache:
894+
json.dump(meta, f, indent=2, sort_keys=True)
895+
else:
896+
json.dump(meta, f)
891897
os.replace(meta_json_tmp, meta_json)
892898

893899
return interface_hash

mypy/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ def process_options(args: List[str],
194194
# (e.g. by adding a call to reveal_type).
195195
parser.add_argument('--shadow-file', metavar='PATH', nargs=2, dest='shadow_file',
196196
help=argparse.SUPPRESS)
197+
# --debug-cache will disable any cache-related compressions/optimizations,
198+
# which will make the cache writing process output pretty-printed JSON (which
199+
# is easier to debug).
200+
parser.add_argument('--debug-cache', action='store_true', help=argparse.SUPPRESS)
197201
# deprecated options
198202
parser.add_argument('--silent', action='store_true', dest='special-opts:silent',
199203
help=argparse.SUPPRESS)

mypy/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self) -> None:
6262
self.fast_parser = False
6363
self.incremental = False
6464
self.cache_dir = defaults.MYPY_CACHE
65+
self.debug_cache = False
6566
self.suppress_error_context = False # Suppress "note: In function "foo":" messages.
6667
self.shadow_file = None # type: Optional[Tuple[str, str]]
6768

0 commit comments

Comments
 (0)