Closed DylanLukes closed 1 month ago
Thanks for the suggestion.
This is one of the oldest parts of the pyright code, and it contains a number of inconsistencies. For example, it allows all of the names you've listed above for __new__
methods within metaclasses, but it uses a different (more restricted) list for class methods. I've reworked the logic so it applies these checks consistently. The names in your list above work fine now.
This change will be included in the next release.
This is addressed in pyright 1.1.376.
When implementing metaclasses, it is relatively commonplace in methods such as
__new__
and@classmethod
's such as__prepare__
to name the first positional argumentmcs
,mcls
ormetacls
.This has a few benefits. In particular, it allows for the name
cls
to be used for an actual class, such as in:This is a good bit clearer than naming the first parameter
cls
and requiring a different name to be used in the body. The current inspection, I think, forces either:This seems like such a common naming pattern that it
Usage in the Wild
While not ubiquitous, these naming choices are certainly common in many large and old projects, and besides that the intent behind them are immediately clear (except perhaps
mcs
).mcls
in the descriptor guide.metacls
inLib/enum.py
.metacls
intype.prepare
as well asEnumMeta.__new__
andEnumMeta.__prepare__
. Additionally,mcls
and__mcls
are used inABCMeta.__new__
.mcs
indjango.forms.{forms, widgets, models}
as well as one usage ofmetacls
indjango.db.models.enums
.mcs
.Related to a Problem?
Not really, just an inconvenience. Disabling this inspection project-wide is a sledgehammer for a nail. Ditto for an entire (meta)class. Disabling it per method or per line adds unnecessary noise, especially if there are
@overload
s in play.Proposed Solution
Relax
reportSelfClsParameterName
when the containing class inherits fromtype
, both for@classmethod
's in general as well as for__new__
to allow formcs
,mcls
andmetacls
.