Add warning on extra @inject

This commit is contained in:
ZipFile 2025-06-20 07:08:06 +00:00
parent e6cc12762f
commit 04b5907f21
3 changed files with 12 additions and 1 deletions

View File

@ -108,6 +108,7 @@ markers = [
"pydantic: Tests with Pydantic as a dependency", "pydantic: Tests with Pydantic as a dependency",
] ]
filterwarnings = [ filterwarnings = [
"ignore::dependency_injector.wiring.DIWiringWarning",
"ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\\.0\\.0:DeprecationWarning", "ignore:Module \"dependency_injector.ext.aiohttp\" is deprecated since version 4\\.0\\.0:DeprecationWarning",
"ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\\.0\\.0:DeprecationWarning", "ignore:Module \"dependency_injector.ext.flask\" is deprecated since version 4\\.0\\.0:DeprecationWarning",
"ignore:Please use \\`.*?\\` from the \\`scipy.*?\\`(.*?)namespace is deprecated\\.:DeprecationWarning", "ignore:Please use \\`.*?\\` from the \\`scipy.*?\\`(.*?)namespace is deprecated\\.:DeprecationWarning",

View File

@ -1,6 +1,6 @@
"""Top-level package.""" """Top-level package."""
__version__ = "4.48.0" __version__ = "4.48.1"
"""Version number. """Version number.
:type: str :type: str

View File

@ -24,6 +24,7 @@ from typing import (
Union, Union,
cast, cast,
) )
from warnings import warn
try: try:
from typing import Self from typing import Self
@ -130,6 +131,10 @@ else:
Container = Any Container = Any
class DIWiringWarning(RuntimeWarning):
"""Base class for all warnings raised by the wiring module."""
class PatchedRegistry: class PatchedRegistry:
def __init__(self) -> None: def __init__(self) -> None:
@ -520,6 +525,11 @@ def unwire( # noqa: C901
def inject(fn: F) -> F: def inject(fn: F) -> F:
"""Decorate callable with injecting decorator.""" """Decorate callable with injecting decorator."""
reference_injections, reference_closing = _fetch_reference_injections(fn) reference_injections, reference_closing = _fetch_reference_injections(fn)
if not reference_injections:
warn("@inject is not required here", DIWiringWarning, stacklevel=2)
return fn
patched = _get_patched(fn, reference_injections, reference_closing) patched = _get_patched(fn, reference_injections, reference_closing)
return cast(F, patched) return cast(F, patched)