Skip to content

Mypy doesn't ensure self types are reasonable #2374

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
ddfisher opened this issue Oct 28, 2016 · 8 comments · Fixed by #2381
Closed

Mypy doesn't ensure self types are reasonable #2374

ddfisher opened this issue Oct 28, 2016 · 8 comments · Fixed by #2381

Comments

@ddfisher
Copy link
Collaborator

For example, this typechecks cleanly:

class C:
    def f(self: str) -> None:
        self.capitalize()

Related to #2193.

@ddfisher
Copy link
Collaborator Author

@elazarg

@elazarg
Copy link
Contributor

elazarg commented Oct 29, 2016

Based on the above issue, this seems like a good idea... :)

@JukkaL
Copy link
Collaborator

JukkaL commented 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:

class A:
    def f(x: str) -> int:   # ok
        return int(x)

    a = f('123')  # ok

A().f()  # error, f is not a method

#16 has more discussion.

@gvanrossum
Copy link
Member

gvanrossum commented Oct 31, 2016 via email

@elazarg
Copy link
Contributor

elazarg commented 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
Copy link
Member

gvanrossum commented Oct 31, 2016 via email

@elazarg
Copy link
Contributor

elazarg commented Nov 1, 2016

I was thinking about type: ignore... But of course it can be something else. And you can use @staticmethod I guess.

@gvanrossum
Copy link
Member

Static methods can't be called without a class.

>>> @staticmethod
... def f(): pass
...
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'staticmethod' object is not callable
>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants