-
Notifications
You must be signed in to change notification settings - Fork 133
Give a diagnostic when creating a function in a class and not specifying the first argument as self #1279
Conversation
I don't think we should merge this one, at least not for now. Although calling the first argument "self" is convention, it's not a runtime-requirement and code which doesn't say (Arguably, we should be trying to rely on the name |
It is an error per http://pylint-messages.wikidot.com/messages:e0213. As in big VS, one can always suppress it via settings/pragmas. By default though, VS shows violations or common conventions although said violations may not always translate to crashes. https://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments |
WIP until I can add no class argument and no method argument. |
Not using If this being done as a stylistic thing then I would leave to a style linter like Pylint. If the worry is someone mis-typed |
Per links above it is considered an error in pylint which then refers to https://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments. Tests may be useful, but in C# I's rather see that I mistyped Besides, some people never write tests and that is their choice. We can't really insist on 'write tests and that will catch the error'. The question is a) how to classify the message. By default it is a warning, not an error. And b) how to let people to suppress categories easily. Like in VS I have 600 info messages from C# and Resharper which I suppress with a single click. |
/// <returns></returns> | ||
private bool SelfIsMetaclass() { | ||
// Just allow all specialized types in Mro to avoid false positives | ||
return _self.Mro.Any(b => b.IsSpecialized); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to check for Type in the Mro, don't know an easy way besides this
public static bool HasValidDecorators(this IPythonFunctionType f, IExpressionEvaluator eval) { | ||
bool valid = true; | ||
// If function is abstract, allow all decorators because it will be overridden | ||
if (f.IsAbstract) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at this for the abstract check
return valid; | ||
} | ||
|
||
private static void ReportInvalidDecorator(NameExpression name, string errorMsg, IExpressionEvaluator eval) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very similar to the one in PythonFunctionExtensions
, any idea how to remove the duplication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing type, pass actual flags. I.e. HasValidDecorators(bool isAbstract, bool isClassMethod, ...)
Diagnostics on function params:Gives diagnostics on:
No diagnostics on:
What I don't handle: I could add something that checks if there are any unknown decorators on the function, don't give any diagnostics on the parameters. Diagnostics on decorator combinations:Properties:
Functions:
Note: Important to note that if there are any errors found in evaluating decorator combinations, no diagnostics are given on the parameters of the given property or function. |
This reverts commit db76f2e.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check #1401, it has a null ref around similar __init__
check.
@@ -37,8 +37,5 @@ public static bool IsGeneric(this IPythonType value) | |||
dst.Location = src.Location; | |||
} | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing because was unused and does not check for declaration type and null check for name.
Waiting on #1273 before I can merge this one.
e.g