GithubHelp home page GithubHelp logo

Comments (6)

JukkaL avatar JukkaL commented on July 28, 2024

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

from mypy.

ashleyh avatar ashleyh commented on July 28, 2024

@JukkaL: I can do this one.

from mypy.

rwbarton avatar rwbarton commented on July 28, 2024

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

from mypy.

gvanrossum avatar gvanrossum commented on July 28, 2024

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)

from mypy.

elazarg avatar elazarg commented on July 28, 2024

I think we can close this issue.

from mypy.

gvanrossum avatar gvanrossum commented on July 28, 2024

Whoops! Indeed.

from mypy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.