Reputation: 35
I have this code
class BaseCompareFeatureWithDefaults(recordlinkage.base.BaseCompareFeature):
def __init__(self, labels_left="", labels_right="", *args, **kwargs) -> None:
super().__init__(labels_left, labels_right, *args, **kwargs)
And I have this pylint error W1113: Keyword argument before variable positional arguments list in the definition of __init__ function (keyword-arg-before-vararg)
. It means that pylint wants *args, **kwargs
after self
. I don't know how to resolve this error and should I? Might be I could skip it/disable it?
I tried to change the order to self, *args, **kwargs, labels_left="", labels_right=""
but if I do it I have this message in VS code: Parameter cannot follow "**" parameter Pylance
and code does not work.
Upvotes: 1
Views: 1361
Reputation: 66
You can safely ignore the Pylint error - in fact it is nonsense in this case!
For a start, you are defining parameters, not arguments (they are the things that are passed when the method is called). Your labels_left
and labels_right
parameters are declared optional by specifying default values, but they are not 'keyword arguments' (or any other kind of arguments).
It is true that optional parameters should follow mandatory parameters, but *args and **kwargs are optional too.
It is also true that the signature for your __init__
method may conceivably confuse some users - for example they might think they can omit labels_left
and labels_right
arguments and carry straight on with stuff intended for *args, which they can't. They also can't supply labels_left
and labels_right
as keyword arguments and then fill *args.
Changing the signature to this might clarify things for your users (v3.8+):
def __init__(self, labels_left="", labels_right="", /, *args, **kwargs) -> None:
This makes it explicit that you expect positional arguments for labels_left
and labels_right
.
Otherwise, if this is how you want your code, just tell Pylint to keep quiet! (No offence Pylint - you're very useful indeed, but sometimes we know what we want to write!)
Upvotes: 0
Reputation: 48
Named keyword arguments should come after positional varargs but before keyword varargs, i.e.:
def __init__(self, *args, labels_left="", labels_right="", **kwargs) -> None:
Upvotes: 2