forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MAINT]: Decorator to warn about changed parameter names & allow for …
…deprecation period (pvlib#2237) * Add renamed_kwarg_warning decorator * Example at solarposition.hour_angle() * another day, another test * Flake8 strikes again * Fix no warning test Co-Authored-By: Kevin Anderson <[email protected]> * Add test to remember removing the deprecation decorator * test-driven-development for the win: unchanged remain properties Co-Authored-By: Kevin Anderson <[email protected]> * Update docs * Update fail_on_pvlib_version test comment * Rename&deprecate solarposition.sun_rise_set_transit_spa * flake8 * flake8 no more * date -> time in solarposition.sun_rise_set_transit_spa * Change to ..versionchanged, same format. * Apply suggestions from Adam Co-authored-by: Adam R. Jensen <[email protected]> * Apply Adam review x2 Co-Authored-By: Adam R. Jensen <[email protected]> * Update solarposition.py * Update solarposition.py * Revert solarposition and test_solarposition changes * Had to use main instead of master xd * 😭 --------- Co-authored-by: Kevin Anderson <[email protected]> Co-authored-by: Adam R. Jensen <[email protected]>
- Loading branch information
1 parent
d9acdba
commit b6ac5a1
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,51 @@ | ||
""" | ||
Test the _deprecation module. | ||
""" | ||
|
||
import pytest | ||
|
||
from pvlib import _deprecation | ||
|
||
import warnings | ||
|
||
|
||
@pytest.fixture | ||
def renamed_kwarg_func(): | ||
"""Returns a function decorated by renamed_kwarg_warning. | ||
This function is called 'func' and has a docstring equal to 'docstring'. | ||
""" | ||
|
||
@_deprecation.renamed_kwarg_warning( | ||
"0.1.0", "old_kwarg", "new_kwarg", "0.2.0" | ||
) | ||
def func(new_kwarg): | ||
"""docstring""" | ||
return new_kwarg | ||
|
||
return func | ||
|
||
|
||
def test_renamed_kwarg_warning(renamed_kwarg_func): | ||
# assert decorated function name and docstring are unchanged | ||
assert renamed_kwarg_func.__name__ == "func" | ||
assert renamed_kwarg_func.__doc__ == "docstring" | ||
|
||
# assert no warning is raised when using the new kwarg | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
assert renamed_kwarg_func(new_kwarg=1) == 1 # as keyword argument | ||
assert renamed_kwarg_func(1) == 1 # as positional argument | ||
|
||
# assert a warning is raised when using the old kwarg | ||
with pytest.warns(Warning, match="Parameter 'old_kwarg' has been renamed"): | ||
assert renamed_kwarg_func(old_kwarg=1) == 1 | ||
|
||
# assert an error is raised when using both the old and new kwarg | ||
with pytest.raises(ValueError, match="they refer to the same parameter."): | ||
renamed_kwarg_func(old_kwarg=1, new_kwarg=2) | ||
|
||
# assert when not providing any of them | ||
with pytest.raises( | ||
TypeError, match="missing 1 required positional argument" | ||
): | ||
renamed_kwarg_func() |