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
{{ message }}
Repository navigation
Mypy doesn't ensure self types are reasonable #2374
This looks like a dupe of #16, but I'll leave this open since there's a PR that references this.
One thought from the discussion at #16: It may be reasonable to have a first argument (at least one not spelled as self) with an arbitrary annotation, but that function will not be a proper instance method and should only be callable within the class body. Example:
class A:
def f(x: str) -> int: # ok
return int(x)
a = f('123') # ok
A().f() # error, f is not a method
Hm. Having fixed a bunch of issues the latest master found in both of our
internal repos and typeshed, I would say that the fear of the pattern
(functions without 'self' in classes) is exaggerated. The only thing we
found was an interface (IMetadataProvider) in pkg_resources.py, which was
meant for documentation only.
IMO, ideally we would only allow use of such a function from within the class' scope. We would report an error for functions that are used from the outside (as suggested by Jukka), and also for functions that are unused from the inside.
However, personally I would vote for requiring an explicit hint for such functions, keeping the policy and implementation simpler.
Activity
ddfisher commentedon Oct 28, 2016
@elazarg
elazarg commentedon Oct 29, 2016
Based on the above issue, this seems like a good idea... :)
JukkaL commentedon Oct 31, 2016
This looks like a dupe of #16, but I'll leave this open since there's a PR that references this.
One thought from the discussion at #16: It may be reasonable to have a first argument (at least one not spelled as
self) with an arbitrary annotation, but that function will not be a proper instance method and should only be callable within the class body. Example:#16 has more discussion.
gvanrossum commentedon Oct 31, 2016
elazarg commentedon Oct 31, 2016
IMO, ideally we would only allow use of such a function from within the class' scope. We would report an error for functions that are used from the outside (as suggested by Jukka), and also for functions that are unused from the inside.
However, personally I would vote for requiring an explicit hint for such functions, keeping the policy and implementation simpler.
gvanrossum commentedon Oct 31, 2016
elazarg commentedon Nov 1, 2016
I was thinking about
type: ignore... But of course it can be something else. And you can use@staticmethodI guess.gvanrossum commentedon Nov 1, 2016
Static methods can't be called without a class.
Sanity checks for declared selftype (#2381)