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

N804 and N805 apply ignore-names to function, rather than parameter names (self, cls) #12465

Closed
njzjz opened this issue Jul 22, 2024 · 7 comments · Fixed by #12497
Closed

N804 and N805 apply ignore-names to function, rather than parameter names (self, cls) #12465

njzjz opened this issue Jul 22, 2024 · 7 comments · Fixed by #12497
Assignees
Labels
needs-decision Awaiting a decision from a maintainer

Comments

@njzjz
Copy link

njzjz commented Jul 22, 2024

I want to use N804 to correct def setUpClass(self): in my repository. However, I found that when the class method name is setUpClass, no error is reported.

ruff v0.5.4
https://play.ruff.rs/f96de0de-0667-4e6b-88c1-fe229ad7ca5a

import unittest

class TestXXX(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        # no error reported
        pass


class TestXXX:
    @classmethod
    def setUpClss(self):
        # N804: First argument of a class method should be named `cls`
        pass

I don't understand why N804 ignores setUpClass. There is no documentation for this behavior.

@njzjz
Copy link
Author

njzjz commented Jul 22, 2024

Oh, I just understand that the default ignore-names contains setUpClass. However, N804 does not rename setUpClass. It just renames self to cls.

@charliermarsh
Copy link
Member

Is the request that setUpClass be renamed to set_up_class?

@njzjz
Copy link
Author

njzjz commented Jul 22, 2024

Is the request that setUpClass be renamed to set_up_class?

No, N804 should just rename

@classmethod
def setUpClass(self):

to

@classmethod
def setUpClass(cls):

@charliermarsh
Copy link
Member

charliermarsh commented Jul 22, 2024

Ok, I think I understand what you're saying: setUpClass should not be ignored by N804 despite being in ignore-names, because the rule is about the parameter (self) rather than the function (setUpClass). We've had this behavior from the start (#2855), so it may not even be intentional.

It'd be reasonable to change, but it also means that there's no way to ignore methods that do deviate from the pattern in that way.

I'd be nice to get more opinions on this. Maybe @AlexWaygood? What behavior would you expect here?

@charliermarsh charliermarsh added the needs-decision Awaiting a decision from a maintainer label Jul 22, 2024
@charliermarsh charliermarsh changed the title N804 ignores setUpClass methods N804 and N805 apply ignore-names to function, rather than parameter names (self, cls) Jul 22, 2024
@AlexWaygood
Copy link
Member

This is an interesting question! I'd be curious to see the ecosystem results on an experimental PR that ignored ignore-names for N804. I don't feel like I have any sense of how many users might be overriding ignore-names in their config to systematically ignore all N804 violations for all methods that have a specific name across a codebase.

It does feel a bit silly to me that we ignore N804 violations by default if the method is called setUpClass, because that's obviously not the motivation for including that entry in the ignore-names default list. But I can also see @charliermarsh's point that if N804 just ignores the ignore-names setting entirely then there's no setting available to users that they can use to tweak how N804 works.

Possibly we could start ignoring the setting for N804 in preview mode only, and see if anybody complains. If we do get any complaints, we could add a new dedicated setting that could be separately configured for determining the behaviour of rules like N804 that complain about parameter names rather than class or method names.

@charliermarsh
Copy link
Member

Yeah the current behavior is strange. Though it's also strange to imagine putting self or cls in ignore-names, since it's effectively disabling the rules. We should change it though and see if the ecosystem checks look good.

@AlexWaygood
Copy link
Member

Right, I don't think it makes sense to have N804 be disabled if N804 sees cls or self in ignore-names either. What would make sense to me would be if we had a separate setting -- ignore-weird-parameter-names-for-these-methods (bikeshedding permitted, I thought about it for 0 seconds ;) -- that could be used to configure N804 separately to the N* rules that are configured via ignore-names. But I just don't really know if users actually need to configure N804 in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants