-
Notifications
You must be signed in to change notification settings - Fork 34
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
Deprecate a function parameter — Contribution Guide #8
Comments
This looks pretty elegant: https://stackoverflow.com/questions/49802412/how-to-implement-deprecation-in-python-with-argument-alias: import functools
import warnings
def deprecated_alias(**aliases):
def deco(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
rename_kwargs(f.__name__, kwargs, aliases)
return f(*args, **kwargs)
return wrapper
return deco
def rename_kwargs(func_name, kwargs, aliases):
for alias, new in aliases.items():
if alias in kwargs:
if new in kwargs:
raise TypeError('{} received both {} and {}'.format(
func_name, alias, new))
warnings.warn('{} is deprecated; use {}'.format(alias, new),
DeprecationWarning)
kwargs[new] = kwargs.pop(alias)
class MyClass(object):
@deprecated_alias(object_id='id_object')
def __init__(self, id_object):
self.id = id_object |
It’s a good starting point. We also need to add a warning message. |
Since it looks like it's taking some time to discuss and implement this improvement, or find someone that could, can I suggest to introduce a function to emit properly a deprecation warning, to be used inside the code for complex situations that can only be represented using runtime checks by the programmer? Those runtime checks are probably already there. Something like: deprecation_warning(version=..., reason=...) That can be used in any place needed? |
Introduction
This article concerns the addition of one (or more) decorator(s) to deprecate the use of a function parameter. For example:
Such a decorator could be coded as follows:
Advantages and disadvantages of the decorator
Using a decorator to deprecate the use of a function parameter is interesting and offers some advantages:
Disadvantages:
Another important consideration:
Generalization:
In addition to the case of deprecating a parameter usage, a warning could also be issued in the following cases:
The use cases mentioned above fall into 3 categories:
The third case (modified parameter) is the most difficult to specify in general terms.
Examples of deprecations
(This section is incomplete)
Before starting a complex implementation, it would be necessary to study how it is done, on one hand in the Standard Library, and on the other hand in the popular libraries of the Open Source world such as: pip, urllib3, boto3, six, requests, setuptools, futures, Flask, Django…
At least two questions must be considered regarding deprecation:
Implementation
(This section is incomplete)
A parameter deprecation decorator implementation must be flexible. It is necessary to be as flexible as possible and to use what exists for
@deprecated.basic.deprecated
and@deprecated.sphinx.deprecated
. Especially, the use of Wrapt is a must-have.It is also necessary to define test cases that correspond to the most common scenarios. It is also likely that some atypical situations are clearly not implemented.
For now, I would like to continue supporting Python 2.7, as it is still used by 25% of users (according to the 2018 State of Developer Ecosystem survey).
The text was updated successfully, but these errors were encountered: