Skip to content

Report error if self has explicit type #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JukkaL opened this issue Dec 12, 2012 · 6 comments
Closed

Report error if self has explicit type #16

JukkaL opened this issue Dec 12, 2012 · 6 comments
Labels
bug mypy got something wrong needs discussion

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 12, 2012

For example, the following program should not be accepted:

class A:
    def f(self: Any): pass
@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 11, 2013

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).

@ashleyh
Copy link
Contributor

ashleyh commented Feb 27, 2016

@JukkaL: I can do this one.

@rwbarton
Copy link
Contributor

rwbarton commented Jun 3, 2016

Given the situation with #1586 I'm going to retag this as "needs discussion".

@gvanrossum
Copy link
Member

I wonder if we could do something like this:

  • 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)

@elazarg
Copy link
Contributor

elazarg commented Dec 8, 2016

I think we can close this issue.

@gvanrossum
Copy link
Member

Whoops! Indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong needs discussion
Projects
None yet
Development

No branches or pull requests

5 participants