From ddceff9b7709867b4d85cf43b018106bcd511ca2 Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 5 Aug 2025 21:50:46 +0200 Subject: [PATCH 1/3] Fix the first stage of mypy stricter errors Following this doc: https://mypy.readthedocs.io/en/stable/existing_code.html#getting-to-strict Total 13 error fixed. --- pygit2/blame.py | 2 +- pygit2/callbacks.py | 2 +- pygit2/ffi.py | 2 +- pygit2/repository.py | 2 +- test/test_index.py | 4 ++-- test/test_tag.py | 2 +- test/test_tree.py | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pygit2/blame.py b/pygit2/blame.py index c5e982b3..02685e12 100644 --- a/pygit2/blame.py +++ b/pygit2/blame.py @@ -127,7 +127,7 @@ def __del__(self) -> None: C.git_blame_free(self._blame) def __len__(self) -> int: - return C.git_blame_get_hunk_count(self._blame) # type: ignore[no-any-return] + return C.git_blame_get_hunk_count(self._blame) def __getitem__(self, index: int) -> BlameHunk: chunk = C.git_blame_get_hunk_byindex(self._blame, index) diff --git a/pygit2/callbacks.py b/pygit2/callbacks.py index 3261ce60..3e365ebc 100644 --- a/pygit2/callbacks.py +++ b/pygit2/callbacks.py @@ -414,7 +414,7 @@ def git_proxy_options( elif type(proxy) is str: opts.type = C.GIT_PROXY_SPECIFIED # Keep url in memory, otherwise memory is freed and bad things happen - payload.__proxy_url = ffi.new('char[]', to_bytes(proxy)) # type: ignore[attr-defined, no-untyped-call] + payload.__proxy_url = ffi.new('char[]', to_bytes(proxy)) # type: ignore[attr-defined] opts.url = payload.__proxy_url # type: ignore[attr-defined] else: raise TypeError('Proxy must be None, True, or a string') diff --git a/pygit2/ffi.py b/pygit2/ffi.py index 41ebec4c..166227e1 100644 --- a/pygit2/ffi.py +++ b/pygit2/ffi.py @@ -24,5 +24,5 @@ # Boston, MA 02110-1301, USA. # Import from pygit2 -from ._libgit2 import ffi # type: ignore # noqa: F401 +from ._libgit2 import ffi # noqa: F401 from ._libgit2 import lib as C # type: ignore # noqa: F401 diff --git a/pygit2/repository.py b/pygit2/repository.py index 94e8e091..dfb96e32 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -615,7 +615,7 @@ def diff( # Case 2: Index to workdir elif a is None and b is None: - return self.index.diff_to_workdir(**options) # type: ignore[arg-type] + return self.index.diff_to_workdir(**options) # Case 3: Diff tree to index or workdir elif isinstance(a, Tree) and b is None: diff --git a/test/test_index.py b/test/test_index.py index 0fa2e540..01b04417 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -49,8 +49,8 @@ def test_read(testrepo: Repository) -> None: assert len(index) == 2 with pytest.raises(TypeError): - index[()] # type: ignore - utils.assertRaisesWithArg(ValueError, -4, lambda: index[-4]) # type: ignore + index[()] + utils.assertRaisesWithArg(ValueError, -4, lambda: index[-4]) utils.assertRaisesWithArg(KeyError, 'abc', lambda: index['abc']) sha = 'a520c24d85fbfc815d385957eed41406ca5a860b' diff --git a/test/test_tag.py b/test/test_tag.py index e4f017b1..a3be72c2 100644 --- a/test/test_tag.py +++ b/test/test_tag.py @@ -59,7 +59,7 @@ def test_new_tag(barerepo: Repository) -> None: target_prefix = target[:5] too_short_prefix = target[:3] with pytest.raises(ValueError): - barerepo.create_tag(name, too_short_prefix, ObjectType.BLOB, tagger, message) # type: ignore + barerepo.create_tag(name, too_short_prefix, ObjectType.BLOB, tagger, message) sha = barerepo.create_tag(name, target_prefix, ObjectType.BLOB, tagger, message) tag = barerepo[sha] diff --git a/test/test_tree.py b/test/test_tree.py index c5954004..556c3751 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -99,7 +99,7 @@ def test_sorting(barerepo: Repository) -> None: tree_a = barerepo['18e2d2e9db075f9eb43bcb2daa65a2867d29a15e'] assert isinstance(tree_a, Tree) assert list(tree_a) == sorted(reversed(list(tree_a)), key=pygit2.tree_entry_key) - assert list(tree_a) != reversed(list(tree_a)) # type: ignore[comparison-overlap] + assert list(tree_a) != reversed(list(tree_a)) def test_read_subtree(barerepo: Repository) -> None: From 0947a213e0924a891e089dcdd15aa528d88f339b Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 5 Aug 2025 21:43:05 +0200 Subject: [PATCH 2/3] Add ignores to setup.py Don't want to mess with setup.py and types since they are not that important here. --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 1ad61821..7506d623 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,8 @@ # the Free Software Foundation, 51 Franklin Street, Fifth Floor, # Boston, MA 02110-1301, USA. +# mypy: disable-error-code="import-not-found, import-untyped" + # Import setuptools before distutils to avoid user warning import os import sys From 2a57752a5bf66019e78644dc01602af487d8147f Mon Sep 17 00:00:00 2001 From: kbaikov Date: Tue, 5 Aug 2025 21:42:19 +0200 Subject: [PATCH 3/3] Introduce stricter mypy options This is a first step according to the documentation https://mypy.readthedocs.io/en/stable/existing_code.html#getting-to-strict --- mypy.ini | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mypy.ini b/mypy.ini index e9f046b0..4431c8ff 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,5 +1,9 @@ [mypy] +warn_unused_configs = True +warn_redundant_casts = True +warn_unused_ignores = True + [mypy-test.*] disallow_untyped_defs = True disallow_untyped_calls = True