Repository navigation
Added caching - #136
Added caching#136
Conversation
Black will cache already formatted files using their file size and modification timestamp. The cache is per-user and will always be used unless Black is used with --diff or with code provided via standard input.
Pull Request Test Coverage Report for Build 234
💛 - Coveralls |
|
I'm not sure why 3.7-dev is failing. Running locally, both latest commit on 3.7 branch (7899153c1f9e6808c7729200f62ab2a70cfa9427) and v3.7.0b3 work for me. Also, please apologize for the messy second commit. I didn't realize flake8 was used and only ran mypy on black.py, not the tests. |
ambv
left a comment
There was a problem hiding this comment.
This is very nice! I love the improved code coverage.
On a general level, I'd like us to avoid using the word "cache" in messages and docs. A "cache" suggests that we're storing the output of formatting for future reuse. I gave you suggestions what I have in mind. Note that I'm fine using the name "cache" in the source since this is how the implementation sees it.
There's a few other things, see comments.
Thanks again for working on it, this is a nice architectural improvement.
| __version__ = "18.4a2" | ||
| DEFAULT_LINE_LENGTH = 88 | ||
| CACHE_DIR = Path(user_cache_dir("black", version=__version__)) | ||
| CACHE_FILE = CACHE_DIR / "cache.pkl" |
There was a problem hiding this comment.
- lib2to3 uses ".pickle", let's use that for the file extension, too.
- You might have noticed,
black.pyis grouping related global variables close to where they're used. With this approach, it would be nicer if you put CACHE_DIR and CACHE_FILE immediately beforeread_cache().
| write_back = WriteBack.DIFF | ||
| else: | ||
| write_back = WriteBack.YES | ||
|
|
There was a problem hiding this comment.
nit: In black.py we avoid vertical whitespace.
| ctx.exit(return_code) | ||
|
|
||
|
|
||
| def run_single_file_mode( |
There was a problem hiding this comment.
Because of the amount of context that you need to pass, I think run_single_file_mode() and run_multi_file_mode() are unnecessary. You aren't calling them otherwise. I'm not sure why you extracted those functions out?
There was a problem hiding this comment.
flake8 was complaining about main getting too complicated, which is why I split it into those functions. I did not plan to do so but that was the easiest way I could think of to reduce the complexity in a single function.
| else: | ||
| report.done(src, task.result()) | ||
|
|
||
| cache: Union[Cache, None] |
There was a problem hiding this comment.
- Just use
cache: Cache = {}here, then you don't need theelseclause below. - Instead of checking for
if cache is not None and formatted:in 334, state explicitly:if write_back != WriteBack.DIFF and formatted:. That removes the indirection that otherwise makes people ask "why would cache ever be None?".
(Same applies to the single-file case.)
| cancelled = [] | ||
| formatted = [] | ||
|
|
||
| if sources: |
There was a problem hiding this comment.
You don't really need that indent for the entire block. The only thing you need to wrap in if sources: is lines 310-313.
There was a problem hiding this comment.
It doesn't need to indent everything here, but why do the extra work if there's nothing to do? If sources is empty, none of those lines need to run.
| def filter_cached( | ||
| cache: Cache, sources: Iterable[Path] | ||
| ) -> Tuple[List[Path], List[Path]]: | ||
| """Splits a list of paths to format into a tuple of a list of paths that may still |
There was a problem hiding this comment.
PEP 257 and use the imperative form ("Split a list of paths into two.\n\nThe first list contains paths of files that modified on disk. The other list contains paths to non-modified files."")
| if src not in cache: | ||
| todo.append(src) | ||
| else: | ||
| if cache[src] != get_cache_info(src): |
There was a problem hiding this comment.
You can wind the if on 2512 into this one:
if cache.get(src) != get_cache_info(src):
| return todo, done | ||
|
|
||
|
|
||
| def write_cache(cache: Cache, sources: List[Path]) -> None: |
There was a problem hiding this comment.
The cache is a performance measure. If it fails to read or write, this should be transparent to the user. This function needs to be wrapped in try:except OSError: to make write failures invisible.
| master, this is also an option. | ||
|
|
||
|
|
||
| ## Caching |
There was a problem hiding this comment.
"Ignoring non-modified files"
|
|
||
| ## Caching | ||
|
|
||
| Black caches already formatted files, unless the `--diff` flag is used or |
There was a problem hiding this comment.
- In the README, we always use italics and a capital letter for the name of the project. So,
*Black*. - "Black remembers files it already formatted".
|
I'll look at that 3.7-dev failure later. |
|
You didn't annotate one function and I have some nits about the remaining changes but I can fix this forward faster. I'll merge as is for now. Thanks a lot! ✨ 🍰 ✨ |
This implements what was discussed in #109.
With this change, Black will cache already formatted files using their file size and modification timestamp. The cache is per-user and will always be used unless Black is used with --diff or with code provided via standard input.
Cache files are located at:
C:\\Users\<username>\AppData\Local\black\black\Cache\<version>\cache.pkl/Users/<username>/Library/Caches/black/<version>/cache.pkl/home/<username>/.cache/black/<version>/cache.pklThis is the first time that I've used pipenv and I'm not sure that the changes to the lockfile are correct. Please let me know how I can fix this if necessary.