Git Product home page Git Product logo

Comments (6)

erictraut avatar erictraut commented on June 15, 2024

Pyright (the type checker upon which pylance is built) is working correctly here. This isn't a bug.

You have defined two instance methods get_bar and get_a_bar. Instance methods must have a self parameter. If your intent is for these to be static methods, you should add a @staticmethod decorator.

    @staticmethod
    def get_bar() -> bar:
        return foo.bar()

    @staticmethod
    def get_a_bar(a: int = 3) -> bar:
        return foo.bar(a=a)

from pylance-release.

dtlnor avatar dtlnor commented on June 15, 2024

@erictraut
I understand. However, in my case, I will never instantiate the foo class. Therefore, it's acceptable to call get_a_bar() as foo.get_a_bar() without using the @staticmethod decorator.

More importantly, in the previous version of Pylance (or Pyright?), this approach worked just fine.

from pylance-release.

erictraut avatar erictraut commented on June 15, 2024

If you want a static type analyzer to understand your code, you will need to decorate these as @staticmethod.

The recent changes were made to increase pyright's conformance to the typing spec. The new behavior is correct, and we don't have plans to revert to the old (non-conformant) behavior.

from pylance-release.

dtlnor avatar dtlnor commented on June 15, 2024

@erictraut
If this behavior is expected, I still believe it is non-conformant.
At least in my opinion, these type hints should either not function at all, or work perfectly. But not partially working.

This means:

  • test1 should not be hinted as a bar() object.
  • Neither foo.get_bar() nor foo.get_a_bar() should hint their return type as a bar() object.

Alternatively,

  • test2 should be hinted as a bar() object, which is what I expect.

from pylance-release.

erictraut avatar erictraut commented on June 15, 2024

Add a @staticmethod decorator if you want this to work with pyright.

from pylance-release.

dtlnor avatar dtlnor commented on June 15, 2024

@erictraut
We should not merely avoid triggering bugs as a solution; instead, we should address and resolve the underlying issues that cause the bugs, right?

from pylance-release.

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.