References
codecrumbs._argument.argument_renamed(old_name, new_name, *, since=None)
argument_renamed
is an decorator which can be used to rename argument names on the calling side of a method or function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
old_name |
str |
the old argument name which should not be used anymore |
required |
new_name |
str |
the new argument name which should be used from now one |
required |
Example problem:
Refactoring:
>>> @argument_renamed("old", "new")
... def function(new):
... print(new)
...
>>> function(old=5)
file.py:1: DeprecationWarning: argument name "old=" should be replaced with "new=" (fixable with codecrumbs)
5
Exceptions:
Type | Description |
---|---|
TypeError |
If the old named argument is still present in the signature. |
codecrumbs._attribute.renamed_attribute(new_name, *, since=None)
Specifies that all read and write accesses of an attribute should be renamed.
Usage:
>>> class Point:
... data_x = renamed_attribute("x")
... data_y = renamed_attribute("y")
... def __init__(self, x, y):
... self.data_x = x
... self.data_y = y
...
>>> p = Point(1, 2)
file.py:5: DeprecationWarning: ".data_x" should be replaced with ".x" (fixable with codecrumbs)
file.py:6: DeprecationWarning: ".data_y" should be replaced with ".y" (fixable with codecrumbs)
>>> p.data_x
file.py:1: DeprecationWarning: ".data_x" should be replaced with ".x" (fixable with codecrumbs)
1
An access to the old attribute results in a deprecation warning and the calling code is memorized for refactoring.
It renames also has/get/set/delattr()
calls if the argument is a literal string:
>>> class Test:
... old_attribute = renamed_attribute("new_attribute")
...
>>> t = Test()
>>> assert not hasattr(t, "old_attribute")
file.py:1: DeprecationWarning: hasattr(..., "old_attribute") should be replaced with hasattr(..., "new_attribute") (fixable with codecrumbs)
Read access to class attributes can also be renamed:
>>> class Test:
... old_attribute = renamed_attribute("new_attribute")
... new_attribute = 5
...
>>> Test.old_attribute
file.py:1: DeprecationWarning: ".old_attribute" should be replaced with ".new_attribute" (fixable with codecrumbs)
5
It can also be used to rename methods: