Ver código fonte

chore: refactor code quality issues

Aksh Gupta 4 anos atrás
pai
commit
d6fc2e465e

+ 12 - 0
.deepsource.toml

@@ -0,0 +1,12 @@
+version = 1
+
+test_patterns = ["dulwich/**test_*.py"]
+
+exclude_patterns = ["examples/**"]
+
+[[analyzers]]
+name = "python"
+enabled = true
+
+  [analyzers.meta]
+  runtime_version = "3.x.x"

+ 0 - 2
dulwich/cli.py

@@ -388,7 +388,6 @@ class cmd_daemon(Command):
             gitdir = args[0]
         else:
             gitdir = "."
-        from dulwich import porcelain
 
         porcelain.daemon(gitdir, address=options.listen_address, port=options.port)
 
@@ -420,7 +419,6 @@ class cmd_web_daemon(Command):
             gitdir = args[0]
         else:
             gitdir = "."
-        from dulwich import porcelain
 
         porcelain.web_daemon(gitdir, address=options.listen_address, port=options.port)
 

+ 1 - 1
dulwich/client.py

@@ -954,7 +954,7 @@ class TraditionalGitClient(GitClient):
                 proto.write_pkt_line(None)
                 return SendPackResult(old_refs, agent=agent, ref_status={})
 
-            if len(new_refs) == 0 and len(orig_new_refs):
+            if len(new_refs) == 0 and orig_new_refs:
                 # NOOP - Original new refs filtered out by policy
                 proto.write_pkt_line(None)
                 if report_status_parser is not None:

+ 2 - 2
dulwich/contrib/swift.py

@@ -121,7 +121,7 @@ cache_length = 20
 
 class PackInfoObjectStoreIterator(GreenThreadsObjectStoreIterator):
     def __len__(self):
-        while len(self.finder.objects_to_send):
+        while self.finder.objects_to_send:
             for _ in range(0, len(self.finder.objects_to_send)):
                 sha = self.finder.next()
                 self._shas.append(sha)
@@ -735,7 +735,7 @@ class SwiftObjectStore(PackBasedObjectStore):
             f.seek(0)
             pack = PackData(file=f, filename="")
             entries = pack.sorted_entries()
-            if len(entries):
+            if entries:
                 basename = posixpath.join(
                     self.pack_dir,
                     "pack-%s" % iter_sha1(entry[0] for entry in entries),

+ 1 - 1
dulwich/contrib/test_swift.py

@@ -403,7 +403,7 @@ class TestSwiftConnector(TestCase):
                 "geventhttpclient.HTTPClient.request",
                 lambda *args: Response(status=404),
             ):
-                self.assertRaises(swift.SwiftException, lambda: self.conn.create_root())
+                self.assertRaises(swift.SwiftException, self.conn.create_root)
 
     def test_get_container_objects(self):
         with patch(

+ 1 - 1
dulwich/diff_tree.py

@@ -591,7 +591,7 @@ class RenameDetector(object):
             return
 
         modifies = {}
-        delete_map = dict((d.old.path, d) for d in self._deletes)
+        delete_map = {d.old.path: d for d in self._deletes}
         for add in self._adds:
             path = add.new.path
             delete = delete_map.get(path)

+ 1 - 1
dulwich/greenthreads.py

@@ -135,7 +135,7 @@ class GreenThreadsObjectStoreIterator(ObjectStoreIterator):
     def __len__(self):
         if len(self._shas) > 0:
             return len(self._shas)
-        while len(self.finder.objects_to_send):
+        while self.finder.objects_to_send:
             jobs = []
             for _ in range(0, len(self.finder.objects_to_send)):
                 jobs.append(self.p.spawn(self.finder.next))

+ 1 - 1
dulwich/lru_cache.py

@@ -196,7 +196,7 @@ class LRUCache(object):
 
     def items(self):
         """Get the key:value pairs as a dict."""
-        return dict((k, n.value) for k, n in self._cache.items())
+        return {k: n.value for k, n in self._cache.items()}
 
     def cleanup(self):
         """Clear the cache until it shrinks to the requested size.

+ 1 - 1
dulwich/protocol.py

@@ -121,7 +121,7 @@ def capability_symref(from_ref, to_ref):
 
 
 def extract_capability_names(capabilities):
-    return set(parse_capability(c)[0] for c in capabilities)
+    return {parse_capability(c)[0] for c in capabilities}
 
 
 def parse_capability(capability):

+ 1 - 2
dulwich/repo.py

@@ -684,7 +684,7 @@ class BaseRepo(object):
         if f is None:
             return set()
         with f:
-            return set(line.strip() for line in f)
+            return {line.strip() for line in f}
 
     def update_shallow(self, new_shallow, new_unshallow):
         """Update the list of shallow objects.
@@ -889,7 +889,6 @@ class BaseRepo(object):
         Returns:
           New commit SHA1
         """
-        import time
 
         c = Commit()
         if tree is None:

+ 4 - 4
dulwich/tests/compat/test_pack.py

@@ -76,7 +76,7 @@ class TestPack(PackTests):
             pack_path = os.path.join(self._tempdir, "Elch")
             write_pack(pack_path, origpack.pack_tuples())
             output = run_git_or_fail(["verify-pack", "-v", pack_path])
-            orig_shas = set(o.id for o in origpack.iterobjects())
+            orig_shas = {o.id for o in origpack.iterobjects()}
             self.assertEqual(orig_shas, _git_verify_pack_object_list(output))
 
     def test_deltas_work(self):
@@ -89,7 +89,7 @@ class TestPack(PackTests):
         write_pack(pack_path, all_to_pack, deltify=True)
         output = run_git_or_fail(["verify-pack", "-v", pack_path])
         self.assertEqual(
-            set(x[0].id for x in all_to_pack),
+            {x[0].id for x in all_to_pack},
             _git_verify_pack_object_list(output),
         )
         # We specifically made a new blob that should be a delta
@@ -119,7 +119,7 @@ class TestPack(PackTests):
         write_pack(pack_path, all_to_pack, deltify=True)
         output = run_git_or_fail(["verify-pack", "-v", pack_path])
         self.assertEqual(
-            set(x[0].id for x in all_to_pack),
+            {x[0].id for x in all_to_pack},
             _git_verify_pack_object_list(output),
         )
         # We specifically made a new blob that should be a delta
@@ -158,7 +158,7 @@ class TestPack(PackTests):
         write_pack(pack_path, all_to_pack, deltify=True)
         output = run_git_or_fail(["verify-pack", "-v", pack_path])
         self.assertEqual(
-            set(x[0].id for x in all_to_pack),
+            {x[0].id for x in all_to_pack},
             _git_verify_pack_object_list(output),
         )
         # We specifically made a new blob that should be a delta

+ 1 - 1
dulwich/tests/compat/test_repository.py

@@ -63,7 +63,7 @@ class ObjectStoreTestCase(CompatTestCase):
         return refs
 
     def _parse_objects(self, output):
-        return set(s.rstrip(b"\n").split(b" ")[0] for s in BytesIO(output))
+        return {s.rstrip(b"\n").split(b" ")[0] for s in BytesIO(output)}
 
     def test_bare(self):
         self.assertTrue(self._repo.bare)

+ 4 - 4
dulwich/tests/test_pack.py

@@ -308,7 +308,7 @@ class TestPackData(PackTests):
 
     def test_iterentries(self):
         with self.get_pack_data(pack1_sha) as p:
-            entries = set((sha_to_hex(s), o, c) for s, o, c in p.iterentries())
+            entries = {(sha_to_hex(s), o, c) for s, o, c in p.iterentries()}
             self.assertEqual(
                 set(
                     [
@@ -501,7 +501,7 @@ class TestPack(PackTests):
             bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
             self.assertRaises(AssertionError, lambda: bad_pack.data)
             self.assertRaises(
-                AssertionError, lambda: bad_pack.check_length_and_checksum()
+                AssertionError, bad_pack.check_length_and_checksum
             )
 
     def test_checksum_mismatch(self):
@@ -515,12 +515,12 @@ class TestPack(PackTests):
             bad_pack = Pack.from_lazy_objects(lambda: bad_data, lambda: index)
             self.assertRaises(ChecksumMismatch, lambda: bad_pack.data)
             self.assertRaises(
-                ChecksumMismatch, lambda: bad_pack.check_length_and_checksum()
+                ChecksumMismatch, bad_pack.check_length_and_checksum
             )
 
     def test_iterobjects_2(self):
         with self.get_pack(pack1_sha) as p:
-            objs = dict((o.id, o) for o in p.iterobjects())
+            objs = {o.id: o for o in p.iterobjects()}
             self.assertEqual(3, len(objs))
             self.assertEqual(sorted(objs), sorted(p.index))
             self.assertTrue(isinstance(objs[a_sha], Blob))

+ 1 - 1
dulwich/tests/test_walk.py

@@ -344,7 +344,7 @@ class WalkerTest(TestCase):
         blob = make_object(Blob, data=b"blob")
         names = [b"a", b"a", b"b", b"b", b"c", b"c"]
 
-        trees = dict((i + 1, [(n, blob, F)]) for i, n in enumerate(names))
+        trees = {i + 1: [(n, blob, F)] for i, n in enumerate(names)}
         c1, c2, c3, c4, c5, c6 = self.make_linear_commits(6, trees=trees)
         self.assertWalkYields([c5], [c6.id], paths=[b"c"])
 

+ 1 - 1
setup.py

@@ -60,7 +60,7 @@ if sys.platform == 'darwin' and os.path.exists('/usr/bin/xcodebuild'):
 tests_require = ['fastimport']
 
 
-if '__pypy__' not in sys.modules and not sys.platform == 'win32':
+if '__pypy__' not in sys.modules and sys.platform != 'win32':
     tests_require.extend([
         'gevent', 'geventhttpclient', 'setuptools>=17.1'])