pep8-naming icon indicating copy to clipboard operation
pep8-naming copied to clipboard

Should be cls in metaclass

Open Dreamsorcerer opened this issue 3 years ago • 3 comments

class Aioresponses(type):
    def __new__(metacls, classname, bases, class_dict):
        ...

This produces: ./tests/unit/test_auth.py:33:18: N804 first argument of a classmethod should be named 'cls'

But, it's a metaclass, and seems to directly contradict B902 from bugbear: ./tests/unit/test_auth.py:33:17: B902 Invalid first argument 'cls' used for metaclass class method. Use the canonical first argument name in methods, i.e. metacls.

Dreamsorcerer avatar Mar 25 '21 15:03 Dreamsorcerer

We implement PEP 8's naming guidance. In particular https://www.python.org/dev/peps/pep-0008/#function-and-method-arguments

I'm not convinced metacls is actually canonical in this case.

sigmavirus24 avatar Mar 25 '21 17:03 sigmavirus24

Hmm, it doesn't mention metaclasses at all. It looks like in cpython the most common use is mcls: https://github.com/python/cpython/blob/master/Lib/abc.py#L84

But, there are also cases of metacls and cls. I think it'd be safest to not validate the name in a metaclass at all, as it seems like it's not covered by PEP 8, and lacks a clear consensus on the appropriate name.

Dreamsorcerer avatar Mar 25 '21 20:03 Dreamsorcerer

It doesn't explicitly mention it, no, but it does mention implicitly class methods (which I think __new__ technically is).

sigmavirus24 avatar Mar 26 '21 16:03 sigmavirus24