-
Notifications
You must be signed in to change notification settings - Fork 56
refactor: Update deprecate arguments. #4553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+556
−543
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
518222d
refactor: Update deprecate arguments.
prmukherj e3771b9
chore: adding changelog file 4553.miscellaneous.md [dependabot-skip]
pyansys-ci-bot 4ce6ca9
Sample Implementation.
prmukherj 15024cb
Update src/ansys/fluent/core/launcher/launcher.py
prmukherj 1e655f5
Update src/ansys/fluent/core/utils/deprecate_new.py
prmukherj a191e06
Refactor.
prmukherj 2c5555b
Update src/ansys/fluent/core/launcher/launcher.py
prmukherj 8919d88
Update signature.
prmukherj 532a912
Clean up.
prmukherj 1775fe3
Renaming
prmukherj 2ffa6e2
Merge branch 'main' into maint/deprecate_arguments_updated
prmukherj 5de5ed9
Graceful handling of TypeError.
prmukherj b89b2e9
Added tests and minor updates.
prmukherj e049092
Update logic.
prmukherj 7d508c3
Fix file session test.
prmukherj 82ea05e
Add licensing info.
prmukherj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Update deprecate arguments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prmukherj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """Deprecate Arguments.""" | ||
|
|
||
| import functools | ||
| from typing import Any, Callable | ||
| import warnings | ||
|
|
||
| from deprecated.sphinx import deprecated | ||
|
|
||
| from ansys.fluent.core.pyfluent_warnings import PyFluentDeprecationWarning | ||
|
|
||
|
|
||
| def deprecate_arguments( | ||
| old_arg_list: list[list[str]], | ||
| new_arg_list: list[list[str]], | ||
| version: str, | ||
| converter: Callable | None = None, | ||
| warning_cls: type[Warning] = PyFluentDeprecationWarning, | ||
| ) -> Callable: | ||
| """ | ||
| Deprecate multiple arguments (possibly grouped) and automatically replace them with new ones. | ||
| Parameters | ||
| ---------- | ||
| old_arg_list : list[list[str]] | ||
| Each inner list represents a group of old argument names being deprecated. | ||
| new_arg_list : list[list[str]] | ||
| Each inner list represents a corresponding group of new argument names. | ||
| version : str | ||
| The version in which the arguments were deprecated. | ||
| converter : callable, optional | ||
| Custom converter function that takes (kwargs, old_arg_list, new_arg_list) | ||
| and returns modified kwargs. | ||
| If not provided, a default converter is used. | ||
| warning_cls : warnings, optional | ||
prmukherj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| The warning class to use for deprecation warnings. | ||
| Raises | ||
| ------ | ||
| ValueError | ||
| For arguments mismatch. | ||
| """ | ||
|
|
||
| # Validation | ||
| if len(old_arg_list) != len(new_arg_list): | ||
| raise ValueError( | ||
| f"old_arg_list and new_arg_list must have the same number of groups. " | ||
| f"Got {len(old_arg_list)} vs {len(new_arg_list)}." | ||
| ) | ||
|
|
||
| # Default converter | ||
| def _default_converter( | ||
| kwargs: dict[str, Any], | ||
| old_param_list: list[list[str]], | ||
| new_param_list: list[list[str]], | ||
| ) -> dict[str, Any]: | ||
| """Default converter that maps all old args to new args one-to-one across groups.""" | ||
| for old_group, new_group in zip(old_param_list, new_param_list): | ||
| if len(old_group) > len(new_group): | ||
| raise ValueError( | ||
| f"Cannot automatically convert {old_group} → {new_group}: " | ||
| "too many old args. Provide a custom converter." | ||
| ) | ||
|
|
||
| for i, old_arg in enumerate(old_group): | ||
| if old_arg in kwargs: | ||
| old_val = kwargs.pop(old_arg) | ||
prmukherj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| target_arg = new_group[i] | ||
| if target_arg in kwargs: | ||
| warnings.warn( | ||
| f"Both deprecated argument '{old_arg}' and new argument '{target_arg}' were provided. " | ||
| f"Ignoring {old_arg}." | ||
| ) | ||
prmukherj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| kwargs[target_arg] = old_val | ||
| return kwargs | ||
|
|
||
| converter = converter or _default_converter | ||
|
|
||
| def _message(old: str, new: str) -> str: | ||
| if "," in old: | ||
| return f"Arguments {old} are deprecated; use {new} instead." | ||
| else: | ||
| return f"Argument {old} is deprecated; use {new} instead." | ||
|
|
||
| def build_warning_messages() -> list[str]: | ||
| messages = [] | ||
| for old_group, new_group in zip(old_arg_list, new_arg_list): | ||
| old_str = ", ".join(f"'{o}'" for o in old_group) | ||
| new_str = ", ".join(f"'{n}'" for n in new_group) | ||
| messages.append(_message(old_str, new_str)) | ||
| return messages | ||
|
|
||
| reason = " ".join(build_warning_messages()) | ||
|
|
||
| def decorator(func: Callable): | ||
| # Documentation | ||
prmukherj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| deprecated_func = deprecated(version=version, reason=reason)(func) | ||
|
|
||
| @functools.wraps(deprecated_func) | ||
| def wrapper(*args, **kwargs): | ||
| # Issue warnings for all relevant mappings | ||
| for old_group, new_group in zip(old_arg_list, new_arg_list): | ||
| if any(arg in kwargs for arg in old_group): | ||
| old_str = ", ".join(f"'{o}'" for o in old_group) | ||
| new_str = ", ".join(f"'{n}'" for n in new_group) | ||
| warnings.warn( | ||
| _message(old_str, new_str), | ||
| warning_cls, | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| # Perform conversion (default or custom) | ||
| kwargs = converter(kwargs, old_arg_list, new_arg_list) | ||
| return deprecated_func(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
| return decorator | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.