From ff66108e986e803f9c7721e6ae9849f127f89239 Mon Sep 17 00:00:00 2001 From: kbaikov Date: Thu, 7 Aug 2025 19:58:08 +0200 Subject: [PATCH] Fix mypy untyped decorators error This was straight forward using legacy ParamSpec syntax: https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators --- mypy.ini | 2 ++ pygit2/callbacks.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/mypy.ini b/mypy.ini index 719fad28..ea5a4ae1 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4,6 +4,8 @@ warn_unused_configs = True warn_redundant_casts = True warn_unused_ignores = True no_implicit_reexport = True +disallow_subclassing_any = True +disallow_untyped_decorators = True [mypy-test.*] disallow_untyped_defs = True diff --git a/pygit2/callbacks.py b/pygit2/callbacks.py index 3423e2db..adcfca52 100644 --- a/pygit2/callbacks.py +++ b/pygit2/callbacks.py @@ -66,7 +66,7 @@ from collections.abc import Callable, Generator from contextlib import contextmanager from functools import wraps -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, ParamSpec, TypeVar # pygit2 from ._pygit2 import DiffFile, Oid @@ -490,8 +490,11 @@ def git_remote_callbacks(payload): # exception. # +P = ParamSpec('P') +T = TypeVar('T') -def libgit2_callback(f): + +def libgit2_callback(f: Callable[P, T]) -> Callable[P, T]: @wraps(f) def wrapper(*args): data = ffi.from_handle(args[-1]) @@ -509,10 +512,10 @@ def wrapper(*args): data._stored_exception = e return C.GIT_EUSER - return ffi.def_extern()(wrapper) + return ffi.def_extern()(wrapper) # type: ignore[attr-defined] -def libgit2_callback_void(f): +def libgit2_callback_void(f: Callable[P, T]) -> Callable[P, T]: @wraps(f) def wrapper(*args): data = ffi.from_handle(args[-1]) @@ -529,7 +532,7 @@ def wrapper(*args): data._stored_exception = e pass # Function returns void, so we can't do much here. - return ffi.def_extern()(wrapper) + return ffi.def_extern()(wrapper) # type: ignore[attr-defined] @libgit2_callback