Skip to content

Commit 7ea4fa5

Browse files
committed
[API-Compat] Resolved merge conflicts.
1 parent 9380862 commit 7ea4fa5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

python/paddle/utils/decorator_utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,31 @@ def wrapper(*args, **kwargs):
165165
return wrapper
166166

167167
return decorator
168+
169+
170+
class ForbidKeywordsDecorator(DecoratorBase):
171+
"""A decorator that hints users to use the correct `compat` functions, when erroneous keyword arguments are detected"""
172+
173+
def __init__(
174+
self, illegal_keys: set[str], func_name: str, correct_name: str
175+
) -> None:
176+
super().__init__()
177+
self.illegal_keys = illegal_keys
178+
self.func_name = func_name
179+
self.correct_name = correct_name
180+
181+
def process(
182+
self, args: tuple[Any, ...], kwargs: dict[str, Any]
183+
) -> tuple[tuple[Any, ...], dict[str, Any]]:
184+
found_keys = [key for key in self.illegal_keys if key in kwargs]
185+
186+
if found_keys:
187+
found_keys.sort()
188+
keys_str = ", ".join(f"'{key}'" for key in found_keys)
189+
plural = "s" if len(found_keys) > 1 else ""
190+
191+
raise TypeError(
192+
f"{self.func_name}() received unexpected keyword argument{plural} {keys_str}. "
193+
f"\nDid you mean to use {self.correct_name}() instead?"
194+
)
195+
return args, kwargs

0 commit comments

Comments
 (0)