You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Actually, having a self argument with an Any type should be accepted. This can be useful when adapting dynamically typed programs. However, the following are bad at least stylistically, and it would be useful to report them somewhow:
explicit, non-Any type for the self argument
missing self argument
Since the name of self is up to the programmer, we can't just complain if the first argument is not spelled 'self'. So this might be a good compromise: give a warning if the first argument of a method has an explicit type that is not Any.
Now we can also diagnose some cases of forgetting to include self, without having to assume that it is spelled self:
class A:
def f(x: int) -> None: ... # Explicit type, not any => warning
This does not strictly have to be an error. For example, consider the above example: if the function is only called as A.f the code could arguably be considered valid (though probably of questionable style).
If the first arg is named self we assume it's going to be called as an instance method (assuming there's no @classmethod or @staticmethod) and the type of self should be compatible with the class (whatever that means)
If the first arg has another name, we're not sure; we allow any explicit type, but if there's no type we still assume it's an instance method (assuming the method is annotated at all)
If the method is not annotated we assume nothing, except if --check-untyped-defs is used; then we use the same heuristic (whether the first arg is named self)
Activity
JukkaL commentedon Feb 11, 2013
Actually, having a self argument with an
Anytype should be accepted. This can be useful when adapting dynamically typed programs. However, the following are bad at least stylistically, and it would be useful to report them somewhow:Since the name of self is up to the programmer, we can't just complain if the first argument is not spelled 'self'. So this might be a good compromise: give a warning if the first argument of a method has an explicit type that is not
Any.Now we can also diagnose some cases of forgetting to include
self, without having to assume that it is spelledself:This does not strictly have to be an error. For example, consider the above example: if the function is only called as
A.fthe code could arguably be considered valid (though probably of questionable style).ashleyh commentedon Feb 27, 2016
@JukkaL: I can do this one.
rwbarton commentedon Jun 3, 2016
Given the situation with #1586 I'm going to retag this as "needs discussion".
gvanrossum commentedon Jun 3, 2016
I wonder if we could do something like this:
selfwe assume it's going to be called as an instance method (assuming there's no@classmethodor@staticmethod) and the type of self should be compatible with the class (whatever that means)--check-untyped-defsis used; then we use the same heuristic (whether the first arg is namedself)elazarg commentedon Dec 8, 2016
I think we can close this issue.
gvanrossum commentedon Dec 8, 2016
Whoops! Indeed.