-
Notifications
You must be signed in to change notification settings - Fork 182
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
NF: Add keyword_only decorator to enforce keyword-only arguments #888
Conversation
Hi @skoudoro! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @WassCodeur,
Thank you for this first version. See below some comments
fury/tests/test_decorators.py
Outdated
|
||
def test_keyword_only(): | ||
@keyword_only | ||
def f(*, a, b): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, the function should be :
def f(a, b, *, c=1, d=2):
return a+b+c+d
fury/decorators.py
Outdated
raise TypeError( | ||
( | ||
"{}() takes 0 positional arguments but {} were given\n" | ||
"Usage: {}({})\n" | ||
"Please Provide keyword-only arguments: {}" | ||
).format( | ||
func.__name__, | ||
len(args), | ||
func.__name__, | ||
params_sample_str, | ||
params_sample_str, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want to raise TypeError
. maximum a warning
fury/decorators.py
Outdated
) | ||
else: | ||
if unexpected_params: | ||
raise TypeError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want to raise TypeError
. maximum a warning
fury/decorators.py
Outdated
) | ||
|
||
elif missing_params: | ||
raise TypeError( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not want to raise TypeError
. maximum a warning
fury/tests/test_decorators.py
Outdated
return a + b | ||
|
||
npt.assert_equal(f(a=1, b=2), 3) | ||
npt.assert_raises(TypeError, f, a=1, b=2, c=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case should not raise typeError or warnings, this is a good case
fury/tests/test_decorators.py
Outdated
|
||
npt.assert_equal(f(a=1, b=2), 3) | ||
npt.assert_raises(TypeError, f, a=1, b=2, c=2) | ||
npt.assert_raises(TypeError, f, 1, 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should generate a warning and not a typeError
fury/tests/test_decorators.py
Outdated
npt.assert_equal(f(a=1, b=2), 3) | ||
npt.assert_raises(TypeError, f, a=1, b=2, c=2) | ||
npt.assert_raises(TypeError, f, 1, 2) | ||
npt.assert_raises(TypeError, f, 1, b=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should generate a warning and not a typeError
fury/tests/test_decorators.py
Outdated
npt.assert_raises(TypeError, f, a=1, b=2, c=2) | ||
npt.assert_raises(TypeError, f, 1, 2) | ||
npt.assert_raises(TypeError, f, 1, b=2) | ||
npt.assert_raises(TypeError, f, a=1, b=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case is good and should generate nothing
fury/tests/test_decorators.py
Outdated
npt.assert_raises(TypeError, f, 1, 2) | ||
npt.assert_raises(TypeError, f, 1, b=2) | ||
npt.assert_raises(TypeError, f, a=1, b=2) | ||
npt.assert_raises( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this case should not exist
Also, You do not need to close and open a new PR. a PR can be updated. When, it is open, it is better to update it. it is very confusing to open and close multiple time the same PR. Let me know if you need more explanation |
So please, reopen this one and try to update it |
Okay, I've already reopened it. Thank you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the functionality is correct,
Docstrings are okay, examples are given, and the test cases are sufficient.
In the CI, it looks like there is a problem when building for macos-latest
for the Python versions 3.8 (ERROR: No matching distribution found for vtk>=9.1.0
) and 3.10 (Error: Codecov token not found. Please provide Codecov token with -t flag.
maybe something related to the workflow .yaml configuration file).
For ubuntu-latest
also in Python 3.8, the test test_elements.py::test_ui_line_slider_2d_horizontal_top
is not passing.
fury/tests/test_decorators.py
Outdated
def f(a, b, *, c, d=4, e=5): | ||
return a + b + c + d + e | ||
|
||
npt.assert_equal(f(1, 2, c=3, d=4, e=5), 15) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think tests are now correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to look deeper at this review, but for now, can you fix all warnings and rebase this PR. Thank you @WassCodeur
Your decorator is raising the Warnings correctly, where the keyword-only functions are not being called only with keywords. You can adjust the function calls as the warning suggests to do, so you get rid of the warnings. Similar to what @deka27 did in his decorator PR. |
Hi @skoudoro, @itellaetxe ! Thank you for your comments. I'm working on it. |
Hi @WassCodeur, What is the status of this PR? Can you also rebase this PR to apply the last changes. It would be great to have all this update as soon as possible |
Hi @skoudoro ! Okay. I'm making the last improvements. I'll push it today. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @WassCodeur,
Can you rebase your PR, I see old work on this branch and we can not see the difference between your work and previous work.
Thank you
fury/decorators.py
Outdated
@@ -43,3 +46,95 @@ def doctest_skip_parser(func): | |||
new_lines.append(code) | |||
func.__doc__ = "\n".join(new_lines) | |||
return func | |||
|
|||
|
|||
def keyword_only(func): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename warn_on_args_to_kwargs
also, this function needs parameter like from_version
and until_version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All right, thanks for your feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reminder here
author WassCodeur <[email protected]> 1716302141 +0000 committer WassCodeur <[email protected]> 1717549770 +0000 NF: Add keyword_only decorator to enforce keyword-only arguments RF: Refactor keyword-only code and Add keyword-only arguments to functions TEST: Add test for keyword_only" decorator Refactor code to use keyword-only arguments in several functions NF: remove some doctest and add a short comment on line 111 to explain what the loop does in fury/decorators.py RF: Refactor keyword-only code and Add keyword-only arguments to functions Refactor code to use keyword-only arguments in several functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @WassCodeur,
reminder to change the decorator name.
Also, this warnings is still here and this is the last one it seems:
fury/animation/tests/test_interpolators.py::test_color_interpolators
/home/runner/work/fury/fury/fury/animation/interpolator.py:310: UserWarning: Here's how to call the Function rgb2lab: rgb2lab(rgb_value, illuminant='value', observer='value')
return color_interpolator(keyframes, rgb2lab, lab2rgb)
Please, check the failing CI to understand what is going on. Some are normal errors, some are not.
Thank you for the future update
fury/decorators.py
Outdated
@@ -43,3 +46,95 @@ def doctest_skip_parser(func): | |||
new_lines.append(code) | |||
func.__doc__ = "\n".join(new_lines) | |||
return func | |||
|
|||
|
|||
def keyword_only(func): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reminder here
What is the status of this PR, we need to move forward @WassCodeur. |
- Renamed `keyword-only` decorator to `warn_on_args_to_kwargs` for greater clarity. - Updated `warn_on_args_to_kwargs` to include version parameters `from_version` and `until_version`. - Added logic to raise a RuntimeError if the current version of FURY_VERSION is greater than `until_version`. - Moved `__version__` definition to a new `fury/version.py` module to avoid circular import problems. - Updated all functions using the decorator to reflect the new name and parameters. - Adjusted import declarations and ensured compatibility across the code base.
…the warn_on_args_to_kwargs decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extensive job modifying both the function definitions and function calls to match the requirements of your decorator. Just a small comment on a commented test function.
Maybe you could comment the test cases of the decorator a bit so it is clearer?
On the other hand, is something like deprecates_from_version
being implemented? I might have overlooked it, but I could not see it.
All in all, good. GJ Wachiou
fury/tests/test_decorators.py
Outdated
npt.assert_raises(TypeError, func, 1) | ||
|
||
|
||
# def test_warn_on_args_to_kwargs_with_versions(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this function stay commented? or is it WIP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's WIP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will u check different test states for different versions as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it'll be one test for every version.
Thank you @itellaetxe for your constructive feedback! Yes, you're right, I have to comment on it to make it clearer. For |
…gs arguments to functions in window.py and actor.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work Wachiou :)
Please see the below suggestions.
@@ -49,3 +49,29 @@ def f(): | |||
del HAVE_AMODULE | |||
f.__doc__ = docstring | |||
npt.assert_raises(NameError, doctest_skip_parser, f) | |||
|
|||
|
|||
def test_warn_on_args_to_kwargs(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if tests to check from_version
and until_version
are trivial. It'll be good if they are possible.
I think we could do something like fury.__version__
to simulate the situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @robinroy03 thanks for your comment, this pull request for the warn_on_args_to_kwargs
decorator. I will focus on this with another dedicated pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @WassCodeur,
See below additional comments. Please, look at @deka27 to get some inspiration concerning the versioning.
Looking forward for the update
fury/decorators.py
Outdated
from fury import __version__ as FURY_VERSION | ||
|
||
if version.parse(FURY_VERSION) > version.parse(until_version): | ||
raise TypeError(e) from e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the other cases ? Look and review @deka27 PR.
if version.parse(FURY_VERSION) < version.parse(from_version)
, no warnings no errors
@@ -32,7 +33,8 @@ | |||
|
|||
|
|||
class glTF: | |||
def __init__(self, filename, apply_normals=False): | |||
@warn_on_args_to_kwargs() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parenthesis not needed everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if I don't put a parenthesis it raises a TypeError
.
Hi @skoudoro Thank you for your comments, I will take into account all your remarks and fix them too. |
…s and update the unittest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Job! thank you for this. merging
…y-gl#888) * parent 4baa99d author WassCodeur <[email protected]> 1716302141 +0000 committer WassCodeur <[email protected]> 1717549770 +0000 NF: Add keyword_only decorator to enforce keyword-only arguments RF: Refactor keyword-only code and Add keyword-only arguments to functions TEST: Add test for keyword_only" decorator Refactor code to use keyword-only arguments in several functions NF: remove some doctest and add a short comment on line 111 to explain what the loop does in fury/decorators.py RF: Refactor keyword-only code and Add keyword-only arguments to functions Refactor code to use keyword-only arguments in several functions * NF: Add keyword_only decorator to enforce keyword-only arguments * NF: Add keyword_only decorator to enforce keyword-only arguments * RF: Refactor keyword-only code and Add keyword-only arguments to functions * TEST: Add test for keyword_only" decorator * Refactor interactor.py and molecular.py * RF: Rename and update decorator for version checks - Renamed `keyword-only` decorator to `warn_on_args_to_kwargs` for greater clarity. - Updated `warn_on_args_to_kwargs` to include version parameters `from_version` and `until_version`. - Added logic to raise a RuntimeError if the current version of FURY_VERSION is greater than `until_version`. - Moved `__version__` definition to a new `fury/version.py` module to avoid circular import problems. - Updated all functions using the decorator to reflect the new name and parameters. - Adjusted import declarations and ensured compatibility across the code base. * RF: Refactor the code by removing version constraints in the call to the warn_on_args_to_kwargs decorator * RF: Refactor warn_on_args_to_kwargs code and Add warn_on_args_to_kwargs arguments to functions in window.py and actor.py * RF: update the version checks in the decorator: warn_on_args_to_kwargs and update the unittest * RF: warn_on_args_to_kwargs decorator parent 4baa99d author WassCodeur <[email protected]> 1716302141 +0000 committer WassCodeur <[email protected]> 1717549770 +0000 NF: Add keyword_only decorator to enforce keyword-only arguments RF: Refactor keyword-only code and Add keyword-only arguments to functions TEST: Add test for keyword_only" decorator Refactor code to use keyword-only arguments in several functions NF: remove some doctest and add a short comment on line 111 to explain what the loop does in fury/decorators.py RF: Refactor keyword-only code and Add keyword-only arguments to functions Refactor code to use keyword-only arguments in several functions NF: Add keyword_only decorator to enforce keyword-only arguments RF: Refactor keyword-only code and Add keyword-only arguments to functions Refactor interactor.py and molecular.py RF: Rename and update decorator for version checks - Renamed `keyword-only` decorator to `warn_on_args_to_kwargs` for greater clarity. - Updated `warn_on_args_to_kwargs` to include version parameters `from_version` and `until_version`. - Added logic to raise a RuntimeError if the current version of FURY_VERSION is greater than `until_version`. - Moved `__version__` definition to a new `fury/version.py` module to avoid circular import problems. - Updated all functions using the decorator to reflect the new name and parameters. - Adjusted import declarations and ensured compatibility across the code base. RF: update the version checks in the decorator: warn_on_args_to_kwargs and update the unittest RF: warn_on_args_to_kwargs decorator
Description:
Added @keyword_only decorator to enforce keyword-only arguments in project functions.
Modification details:
This contribution represents the first step towards applying the @keyword_only decorator to the appropriate functions in the project. Current modifications include the creation of the decorator, associated documentation and unit tests. The next step will be to identify and apply the decorator to relevant functions throughout the project.