Skip to content
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

Declare function mutators with inline comment #8332

Closed

Conversation

rmorshea
Copy link

@rmorshea rmorshea commented Feb 23, 2023

Preface: There may be a better way to do this, but this is my first contribution and I'm unfamiliar with the code base. I'm mostly hoping this can be a starting point for discussion on how this should actually be achieved. I'll update docs if/when this approach is given the go-ahead.

This addresses this comment requesting a way to mark decorators that mutate function signatures via a comment. This is vital for package authors since, if they create a mutative decorator, all their users would have to manually populate their function_mutators config.

I was able to allow mutative decorators to be marked with inline comments by creating an "error" called decorator-preserves-signature which, when disabled via a comment on a decorator definition, will disable function argument checks on calls to functions that have that decorator. I used an error to do this in order to consume the inline comment state via the linter.file_state._supression_mapping.

So for example, all functions decorated with the following decorator would not have their arguments checked

def mutative_decorator(fun):  # pylint: disable=signature-mutator
    """Yet another decorator that changes a function's signature"""
    def wrapper(*args, do_something=None, **kwargs):
        if do_something:
            return fun(*args, **kwargs)

        return None

    return wrapper

@mutative_decorator
def my_func(): pass

my_func(do_something='a-thing')  # OK

Type of Changes

Type
maybe 🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

Refs #2926 (comment)
Maybe closes this issue #5784

This is accomplised by defining an "error" called decorator-preserves-signature
which, when disabled via a comment on a decorator definition, will disable
function argument checks on calls to functions that have that decorator. We
use an error to do this in order to consume the inline comment state via the
linter.file_state._supression_mapping
@rmorshea rmorshea force-pushed the decorator-preserves-signature-option branch from 4d984e0 to 7f4b21f Compare February 23, 2023 09:43
@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Needs review 🔍 Needs to be reviewed by one or multiple more persons labels Feb 23, 2023
@antonymilne
Copy link

antonymilne commented Feb 23, 2023

I am not at all familiar with pylint's source code so can't comment on the implementation but this feature would be extremely helpful I think, so thank you very much @rmorshea for working on it!

Just two questions:

  1. Are there other examples in pylint where you can disable warnings at the point of function definition rather than point of call?
  2. If this effectively adds a function to signature-mutators, should the name of the "error" decorator-preserves-signature match that somehow? e.g. signature-mutated or signature-unmutated depending on which way round you want to call it.

@github-actions

This comment has been minimized.

@rmorshea rmorshea force-pushed the decorator-preserves-signature-option branch from 8280289 to 6af39bb Compare February 23, 2023 17:53
@rmorshea
Copy link
Author

rmorshea commented Feb 23, 2023

@AntonyMilneQB

  1. As far as I'm aware, the indirect effects of disabling this error are unique in PyLint. If it's not, I'd definitely want to copy whatever approach was taken previously.
  2. I renamed the error. The comment is now pylint: disable=signature-mutator.

@DanielNoord
Copy link
Collaborator

@Pierre-Sassoulas What do you think about this? I don't know if I prefer this approach but also don't have an immediate better solution yet..

@rmorshea
Copy link
Author

rmorshea commented Feb 23, 2023

@DanielNoord, I'm not sure if your hesitant about using errors in this way, or the level of indirection, but given that the source of the problem arises from the decorator definition, rather than from the call-site for functions that use that decorator, I think some level of indirection is required. As for this, admittedly somewhat hacky use of error statuses, I'd love some recommendations on a better way to implement this.

@rmorshea
Copy link
Author

rmorshea commented Feb 24, 2023

Wow, that's quite a list! Thanks for collecting that.

@rmorshea
Copy link
Author

rmorshea commented Feb 24, 2023

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the design proposal, this is thought provoking.

.gitignore Outdated Show resolved Hide resolved
@@ -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": (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"E1145": (
"E1145": (

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 function-mutator manually in the message ? This is not something that is done often, even less so if we do configuration template, this way it's done once per library and become very reasonable.

Copy link
Author

@rmorshea rmorshea Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not be an error imo, but I also think it should not be a message either.

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.

Maybe it would be simpler to suggest to add a value to function-mutator manually in the message?

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 signature-mutators list manually.

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.

Choose a reason for hiding this comment

The 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 disable could be somehow specified at point of function definition rather than at point of call. I'm developing a package that defines decorated functions that will unfortunately raise unexpected-keyword-arg when called. At the moment there's a burden on every user that uses pylint and calls my function to update their config; it would be much more maintainable if the burden fell on the package author instead.

Copy link
Member

Choose a reason for hiding this comment

The 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 (mark or disable-in-calling-code) instead of disable/enable). This is something we need to take into account because this might have a big impact (all comments are parsed).

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have function-mutator to handle this. Some functions are just too dynamic and cannot reasonably be parsed but that's not all functions and pylint would be crippled if we just refuse to analyses all decorators. Maybe we could suggest to populate function_mutator only on decorator we're not sure about. Maybe we can add more default values in function-mutator, for well known libs.

Copy link

@Archmonger Archmonger Feb 27, 2023

Choose a reason for hiding this comment

The 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, function_mutator is too cumbersome to be worth considering.

Co-authored-by: Pierre Sassoulas <[email protected]>
@codecov
Copy link

codecov bot commented Feb 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.46%. Comparing base (f54dfd1) to head (63f0747).
Report is 956 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8332   +/-   ##
=======================================
  Coverage   95.46%   95.46%           
=======================================
  Files         177      177           
  Lines       18703    18708    +5     
=======================================
+ Hits        17855    17860    +5     
  Misses        848      848           
Files with missing lines Coverage Δ
pylint/checkers/typecheck.py 96.37% <100.00%> (+0.01%) ⬆️

... and 1 file with indirect coverage changes

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 63f0747

@DanielNoord
Copy link
Collaborator

I don't think this proposal ever went anywhere. Personally I still don't really like using comments as it quickly allows for more complexity to seep into pylint. I'd suggest closing this as a nice thought provoking experiment but not something we will act on now.

@rmorshea
Copy link
Author

rmorshea commented Jun 3, 2024

Happy to close if maintainers don't see it going anywhere.

It might be better for users to just disable too-many-function-args, unexpected-keyword-arg, and no-value-for-parameter - I'd argue that other tools do a better job of performing those checks and that maintaining a function_mutators list is quite burdensome for users. Give all that, the complexity this adds to PyLint's internals doesn't seem worth it.

@Pierre-Sassoulas
Copy link
Member

I think we can close, but we should create a related issue because a lot of info are here only and would be lost otherwise. It feel like pylint need to understand function mutators better (easier said than done) and prefer false negative over false positive as long as it does not understand them.

@Pierre-Sassoulas
Copy link
Member

They were two existing "generic" issues about decorators, #3954 and #2578, closing after I added a comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs review 🔍 Needs to be reviewed by one or multiple more persons
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants