-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Check if decorator returns use keyword (unexpected-keyword-arg)
#5547
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
Pierre-Sassoulas
merged 5 commits into
pylint-dev:main
from
DanielNoord:keyword-decorator
Dec 18, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66c2e0c
Check if decorator returns use keyword (``unexpected-keyword-arg``)
DanielNoord 0fb82e8
Improve coverage
DanielNoord d0c38cb
Remove unnecessary declaration
DanielNoord c3bbef6
Change spacing
DanielNoord 27991a3
Merge branch 'main' of https://github.com/PyCQA/pylint into keyword-d…
DanielNoord 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """Tests for unexpected-keyword-arg""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests are on point ! |
||
| # pylint: disable=undefined-variable, too-few-public-methods, missing-function-docstring, missing-class-docstring | ||
|
|
||
|
|
||
| def non_param_decorator(func): | ||
| """Decorator without a parameter""" | ||
|
|
||
| def new_func(): | ||
| func() | ||
|
|
||
| return new_func | ||
|
|
||
|
|
||
| def param_decorator(func): | ||
| """Decorator with a parameter""" | ||
|
|
||
| def new_func(internal_arg=3): | ||
| func(junk=internal_arg) | ||
|
|
||
| return new_func | ||
|
|
||
|
|
||
| def kwargs_decorator(func): | ||
| """Decorator with kwargs. | ||
| The if ... else makes the double decoration with param_decorator valid. | ||
| """ | ||
|
|
||
| def new_func(**kwargs): | ||
| if "internal_arg" in kwargs: | ||
| func(junk=kwargs["internal_arg"]) | ||
| else: | ||
| func(junk=kwargs["junk"]) | ||
|
|
||
| return new_func | ||
|
|
||
|
|
||
| @non_param_decorator | ||
| def do_something(junk=None): | ||
| """A decorated function. This should not be passed a keyword argument""" | ||
| print(junk) | ||
|
|
||
|
|
||
| do_something(internal_arg=2) # [unexpected-keyword-arg] | ||
|
|
||
|
|
||
| @param_decorator | ||
| def do_something_decorated(junk=None): | ||
| """A decorated function. This should be passed a keyword argument""" | ||
| print(junk) | ||
|
|
||
|
|
||
| do_something_decorated(internal_arg=2) | ||
|
|
||
|
|
||
| @kwargs_decorator | ||
| def do_something_decorated_too(junk=None): | ||
| """A decorated function. This should be passed a keyword argument""" | ||
| print(junk) | ||
|
|
||
|
|
||
| do_something_decorated_too(internal_arg=2) | ||
|
|
||
|
|
||
| @non_param_decorator | ||
| @kwargs_decorator | ||
| def do_something_double_decorated(junk=None): | ||
| """A decorated function. This should not be passed a keyword argument. | ||
| non_param_decorator will raise an exception if a keyword argument is passed. | ||
| """ | ||
| print(junk) | ||
|
|
||
|
|
||
| do_something_double_decorated(internal_arg=2) # [unexpected-keyword-arg] | ||
|
|
||
|
|
||
| @param_decorator | ||
| @kwargs_decorator | ||
| def do_something_double_decorated_correct(junk=None): | ||
| """A decorated function. This should be passed a keyword argument""" | ||
| print(junk) | ||
|
|
||
|
|
||
| do_something_double_decorated_correct(internal_arg=2) | ||
|
|
||
|
|
||
| # Test that we don't crash on Class decoration | ||
| class DecoratorClass: | ||
| pass | ||
|
|
||
|
|
||
| @DecoratorClass | ||
| def crash_test(): | ||
| pass | ||
|
|
||
|
|
||
| crash_test(internal_arg=2) # [unexpected-keyword-arg] | ||
|
|
||
|
|
||
| # Test that we don't emit a false positive for uninferable decorators | ||
| @unknown_decorator | ||
| def crash_test_two(): | ||
| pass | ||
|
|
||
|
|
||
| crash_test_two(internal_arg=2) | ||
|
|
||
|
|
||
| # Test that we don't crash on decorators that don't return anything | ||
| def no_return_decorator(func): | ||
| print(func) | ||
|
|
||
|
|
||
| @no_return_decorator | ||
| def test_no_return(): | ||
| pass | ||
|
|
||
|
|
||
| test_no_return(internal_arg=2) # [unexpected-keyword-arg] | ||
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,4 @@ | ||
| unexpected-keyword-arg:43:0:43:28::Unexpected keyword argument 'internal_arg' in function call:UNDEFINED | ||
| unexpected-keyword-arg:73:0:73:45::Unexpected keyword argument 'internal_arg' in function call:UNDEFINED | ||
| unexpected-keyword-arg:96:0:96:26::Unexpected keyword argument 'internal_arg' in function call:UNDEFINED | ||
| unexpected-keyword-arg:118:0:118:30::Unexpected keyword argument 'internal_arg' in function call:UNDEFINED |
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.
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.
Not really related to this MR but if we have to handle confidence level, this kind of return where we do not exactly know if inference was used or not in the calling code will be a headache later. Makes me thing that implementing confidence properly will be an enormous work, maybe not even worth it.
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.
Thankfully we have
UNDEFINED😄