Skip to content

Commit e5c18c0

Browse files
committed
fix: Resolve type checking issues in deprecation utilities
- Use setattr() to add custom attributes to avoid type checker complaints - Add cast() for proper type hints on wrapper functions - Add noqa comments for intentional setattr usage - Import cast from typing module This ensures clean type checking while maintaining the deprecation metadata functionality.
1 parent 8675c67 commit e5c18c0

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/project_x_py/utils/deprecation.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import functools
1212
import warnings
1313
from collections.abc import Callable
14-
from typing import Any, TypeVar
14+
from typing import Any, TypeVar, cast
1515

1616
from deprecated import deprecated as _deprecated_decorator
1717

@@ -72,7 +72,10 @@ def decorator(func: F) -> F:
7272
# Use the deprecated package for IDE support
7373
if removal_version:
7474
func = _deprecated_decorator(
75-
reason=full_message, version=version, action="always", category=category
75+
reason=full_message,
76+
version=version or "",
77+
action="always",
78+
category=category,
7679
)(func)
7780

7881
@functools.wraps(func)
@@ -87,13 +90,15 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
8790
func.__doc__ = f"**DEPRECATED**: {full_message}"
8891

8992
# Store deprecation metadata
90-
wrapper.__deprecated__ = True
91-
wrapper.__deprecated_reason__ = reason
92-
wrapper.__deprecated_version__ = version
93-
wrapper.__deprecated_removal__ = removal_version
94-
wrapper.__deprecated_replacement__ = replacement
93+
# We use setattr here to add custom attributes to the function object
94+
# This avoids type checking issues while preserving the metadata
95+
setattr(wrapper, "__deprecated__", True) # noqa: B010
96+
setattr(wrapper, "__deprecated_reason__", reason) # noqa: B010
97+
setattr(wrapper, "__deprecated_version__", version) # noqa: B010
98+
setattr(wrapper, "__deprecated_removal__", removal_version) # noqa: B010
99+
setattr(wrapper, "__deprecated_replacement__", replacement) # noqa: B010
95100

96-
return wrapper # type: ignore
101+
return cast(F, wrapper)
97102

98103
return decorator
99104

@@ -150,7 +155,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
150155
warnings.warn(full_message, DeprecationWarning, stacklevel=2)
151156
return func(*args, **kwargs)
152157

153-
return wrapper # type: ignore
158+
return cast(F, wrapper)
154159

155160
return decorator
156161

@@ -218,12 +223,12 @@ def new_init(self: Any, *args: Any, **kwargs: Any) -> None:
218223
else:
219224
cls.__doc__ = f"**DEPRECATED**: {full_message}"
220225

221-
# Add deprecation metadata
222-
cls.__deprecated__ = True # type: ignore
223-
cls.__deprecated_reason__ = reason # type: ignore
224-
cls.__deprecated_version__ = version # type: ignore
225-
cls.__deprecated_removal__ = removal_version # type: ignore
226-
cls.__deprecated_replacement__ = replacement # type: ignore
226+
# Add deprecation metadata using setattr to avoid type issues
227+
setattr(cls, "__deprecated__", True) # noqa: B010
228+
setattr(cls, "__deprecated_reason__", reason) # noqa: B010
229+
setattr(cls, "__deprecated_version__", version) # noqa: B010
230+
setattr(cls, "__deprecated_removal__", removal_version) # noqa: B010
231+
setattr(cls, "__deprecated_replacement__", replacement) # noqa: B010
227232

228233
return cls
229234

0 commit comments

Comments
 (0)