]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Make source handling use sets instead of lists
authorŁukasz Langa <lukasz@langa.pl>
Mon, 4 Jun 2018 19:50:24 +0000 (12:50 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Mon, 4 Jun 2018 22:53:37 +0000 (15:53 -0700)
Also, sort cached file output to be (more) deterministic.

black.py
tests/test_black.py

index c8c381c2d30ff3fba658299c49de961542490d68..7df98b79cf3440979d64f9e1221d060ed0314611 100644 (file)
--- a/black.py
+++ b/black.py
@@ -278,7 +278,7 @@ def main(
         py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization
     )
     report = Report(check=check, quiet=quiet, verbose=verbose)
-    sources: List[Path] = []
+    sources: Set[Path] = set()
     try:
         include_regex = re.compile(include)
     except re.error:
@@ -293,12 +293,12 @@ def main(
     for s in src:
         p = Path(s)
         if p.is_dir():
-            sources.extend(
+            sources.update(
                 gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
             )
         elif p.is_file() or s == "-":
             # if a file was explicitly given, we don't care about its extension
-            sources.append(p)
+            sources.add(p)
         else:
             err(f"invalid path: {s}")
     if len(sources) == 0:
@@ -309,7 +309,7 @@ def main(
 
     elif len(sources) == 1:
         reformat_one(
-            src=sources[0],
+            src=sources.pop(),
             line_length=line_length,
             fast=fast,
             write_back=write_back,
@@ -384,7 +384,7 @@ def reformat_one(
 
 
 async def schedule_formatting(
-    sources: List[Path],
+    sources: Set[Path],
     line_length: int,
     fast: bool,
     write_back: WriteBack,
@@ -404,7 +404,7 @@ async def schedule_formatting(
     if write_back != WriteBack.DIFF:
         cache = read_cache(line_length, mode)
         sources, cached = filter_cached(cache, sources)
-        for src in cached:
+        for src in sorted(cached):
             report.done(src, Changed.CACHED)
     cancelled = []
     formatted = []
@@ -3304,26 +3304,24 @@ def get_cache_info(path: Path) -> CacheInfo:
     return stat.st_mtime, stat.st_size
 
 
-def filter_cached(
-    cache: Cache, sources: Iterable[Path]
-) -> Tuple[List[Path], List[Path]]:
-    """Split a list of paths into two.
+def filter_cached(cache: Cache, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]]:
+    """Split an iterable of paths in `sources` into two sets.
 
-    The first list contains paths of files that modified on disk or are not in the
-    cache. The other list contains paths to non-modified files.
+    The first contains paths of files that modified on disk or are not in the
+    cache. The other contains paths to non-modified files.
     """
-    todo, done = [], []
+    todo, done = set(), set()
     for src in sources:
         src = src.resolve()
         if cache.get(src) != get_cache_info(src):
-            todo.append(src)
+            todo.add(src)
         else:
-            done.append(src)
+            done.add(src)
     return todo, done
 
 
 def write_cache(
-    cache: Cache, sources: List[Path], line_length: int, mode: FileMode
+    cache: Cache, sources: Iterable[Path], line_length: int, mode: FileMode
 ) -> None:
     """Update the cache file."""
     cache_file = get_cache_file(line_length, mode)
index 1f93e6afdf7c2172621db8924b24e2d598b11b04..06455255a2cab14a31de2bfcab633cf177b68f07 100644 (file)
@@ -856,10 +856,10 @@ class BlackTestCase(unittest.TestCase):
             cached_but_changed.touch()
             cache = {cached: black.get_cache_info(cached), cached_but_changed: (0.0, 0)}
             todo, done = black.filter_cached(
-                cache, [uncached, cached, cached_but_changed]
+                cache, {uncached, cached, cached_but_changed}
             )
-            self.assertEqual(todo, [uncached, cached_but_changed])
-            self.assertEqual(done, [cached])
+            self.assertEqual(todo, {uncached, cached_but_changed})
+            self.assertEqual(done, {cached})
 
     def test_write_cache_creates_directory_if_needed(self) -> None:
         mode = black.FileMode.AUTO_DETECT