-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Declare function mutators with inline comment #8332
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,3 +27,4 @@ build-stamp | |
| .pytest_cache/ | ||
| .mypy_cache/ | ||
| .benchmarks/ | ||
| venv | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -381,6 +381,12 @@ def _missing_member_hint( | |||||
| "Used when a slice step is 0 and the object doesn't implement " | ||||||
| "a custom __getitem__ method.", | ||||||
| ), | ||||||
| "E1145": ( | ||||||
|
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.
Suggested change
Should not be an error imo, but I also think it should not be a message either. Or at least some prior design is in order. Isn't the goal to auto-populate the function-mutator option based on comment by the lib authors directly in their code ? I like this the end goal, but this would be something new in pylint, with the expected associated maintenance cost. Maybe a hack like this is using the existing message control framework is good, maybe it would be simpler to suggest to add a value to
Author
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.
Agreed. I just needed a simple way to hook into the inline comment system. It feels like this comment is meant to "mark" decorator definitions as being signature-mutators. Thus, a more ideal comment might read: def my_decorator(func): # pylint: mark=signature-mutator
...I'm not really sure how to do this though.
If I understand this correctly you're suggesting that, if PyLint discovers invalid arguments, and the function in question has a decorator, that PyLint report the error, but also recommend adding the decorator to the If so, I think this could be problematic. Signature mutating decorators are a fairly advanced concept and users might add decorators to that list, on the recommendation of this message without fully understanding what they're doing just to make the error go away. I foresee bug reports to PyLint from people who have done this and are getting false negatives for invalid function calls. Further, as a lib author, even with this addition to the error message, I'm still probably going to get bug reports from people complaining about this PyLint false positive because, they either don't understand the suggestion, or because the find it inconvenient. Also, if I ever decide to refactor and change the name of the offending decorator, my users' PyLint configs will need to be updated. Ultimately, a message would be a mild improvement in some situations and possibly a detriment in others. 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. I don't have any useful suggestions on implementation (sorry), but just to add a bit of weight to the cause... I'm coming from a completely unrelated project from @rmorshea and was independently looking for a solution to the exact same problem. I agree with everything he's said here. I understand there's no other patterns like this in pylint at the moment, but it would be much, much more convenient if this
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. Right. though I can see most library maintainers refusing to add it because it's not their job to fix pylint's bad code comprehension (What if all linter needed something like that ? It becomes unreasonable pretty fast, and add a coupling I would not want myself). It then become the same situation than previously for users but we now also have to maintain the more complicated system on top of it. Also wouldn't it raise a useless-suppression in the original lib code if we don't add a new keyword ? I'm not sure of the performance implication of adding a new pylint keyword in the message control ( 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. Perhaps rather than creating a new pattern, should pylint consider disabling this error on all decorated functions? In theory, this change should only be a temporary until a method is determined on how to properly parse and analyze these functions.
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. We have 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. Brutal opinion here: If I need to start populating a list of decorated functions within pylintrc, I'd likely remove pylint from my workflow instead. At least to me, |
||||||
| "Decorator does not preserve function signature", | ||||||
| "signature-mutator", | ||||||
| "Emitted when a decorator does not preserve the signature " | ||||||
| "of the functions is takes as inputs.", | ||||||
| ), | ||||||
| "W1113": ( | ||||||
| "Keyword argument before variable positional arguments list " | ||||||
| "in the definition of %s function", | ||||||
|
|
@@ -1454,10 +1460,18 @@ def visit_call(self, node: nodes.Call) -> None: | |||||
| return | ||||||
|
|
||||||
| # Has the function signature changed in ways we cannot reliably detect? | ||||||
| if hasattr(called, "decorators") and decorated_with( | ||||||
| called, self.linter.config.signature_mutators | ||||||
| ): | ||||||
| return | ||||||
| if getattr(called, "decorators", None): | ||||||
| if decorated_with(called, self.linter.config.signature_mutators): | ||||||
| return | ||||||
|
|
||||||
| called_decorator: astroid.NodeNG | ||||||
| for called_decorator in filter( | ||||||
| None, map(safe_infer, called.decorators.nodes) | ||||||
| ): | ||||||
| if not self.linter.file_state._module_msgs_state.get("E1145", {}).get( | ||||||
| called_decorator.lineno, True | ||||||
| ): | ||||||
| return | ||||||
|
|
||||||
| num_positional_args = len(call_site.positional_arguments) | ||||||
| keyword_args = list(call_site.keyword_arguments.keys()) | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.