microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.71k stars 767 forks source link

Pylance intellisense does not highlight object methods in case of its creation with different arg types #1869

Closed wvolkov closed 3 years ago

wvolkov commented 3 years ago

Environment data

Code Snippet / Additional information

Assume class DecisionEngine, which creates an instance of DBClient, passing an integer instead of string as specified below:

class DBClient:

    def __init__(self, param: str):
        self.param = param

    async def setup(self):
        pass

class DecisionEngine:

    def __init__(self):
        self.db: DBClient = None

    async def setup(self):
        self.db = DBClient(1)
        await self.db.setup()

Expected behaviour

at setup method of DecisionEngine class it is possible to drill down into setup method of db object: image

Actual behaviour

it interprets db object as Any type image despite the fact that it is correctly recognize the type of db object at upper line of code image

jakebailey commented 3 years ago

This seems like a bug in some async handling, at least in the hover; repeat the same thing and I get:

image

Making setup return something lets me see:

image

erictraut commented 3 years ago

First, there's a type error in the sample above. The expression DBClient(1) isn't valid because the constructor for DBClient accepts a str, not an int. If you enable type checking, this error will be flagged.

Second, I'm not able to repro the behavior you're seeing @jakebailey. I can't see all of your code, so perhaps there are differences? Where is the value of 1234 coming from? Here's what I see:

Screen Shot 2021-09-24 at 1 38 20 PM

from typing import Optional

class DBClient:
    def __init__(self, param: str):
        self.param = param

    async def setup(self):
        pass

class DecisionEngine:
    def __init__(self):
        self.db: Optional[DBClient] = None

    async def setup(self):
        self.db = DBClient("1")
        reveal_type(await self.db.setup())
jakebailey commented 3 years ago

The code I have is this:

class DBClient:
    def __init__(self, param: str):
        self.param = param

    async def setup(self):
        return 1234

class DecisionEngine:
    def __init__(self):
        self.db: DBClient = None

    async def setup(self):
        self.db = DBClient(1)

        self.db.setup()
        await self.db.setup()

        reveal_type(self.db.setup())
        reveal_type(await self.db.setup())

Revealing the type produces the right result, but try hovering on db inside of the await and compare it to outside of the await. Hovering on it within reveal_type also says it's Any.

Similarly on setup in each of the uses inside the engine class.

jakebailey commented 3 years ago

I did a bit of debugging, and our hover code sees that it's a name node (it being db), then asks for declarations from the type evaluator, but when it does getType, it returns undefined. (That's as far as I went to debug before seeing it was going to be an evaluator thing.)

wvolkov commented 3 years ago

First, there's a type error in the sample above. The expression DBClient(1) isn't valid because the constructor for DBClient accepts a str, not an int. If you enable type checking, this error will be flagged.

Second, I'm not able to repro the behavior you're seeing @jakebailey. I can't see all of your code, so perhaps there are differences? Where is the value of 1234 coming from? Here's what I see:

Screen Shot 2021-09-24 at 1 38 20 PM

from typing import Optional

class DBClient:
    def __init__(self, param: str):
        self.param = param

    async def setup(self):
        pass

class DecisionEngine:
    def __init__(self):
        self.db: Optional[DBClient] = None

    async def setup(self):
        self.db = DBClient("1")
        reveal_type(await self.db.setup())

I didn't see any type error at VS Code, why?

image

jakebailey commented 3 years ago

@wvolkov Type checking isn't enabled by default in Pylance; you need to set python.analysis.typeCheckingMode or set it in a pyrightconfig.json.

jakebailey commented 3 years ago

Ah, so fixing the type error fixes this. That's interesting.

image

image

I'm not really sure why that would be the case, if reveal_type gets the right answer, and it works here:

image

erictraut commented 3 years ago

Yes, good catch. There is a bug here that causes the hover type for self.db to appears as Unknown even though it is eventually resolved to DBClient. The problem is that the logic that evaluates the type for the await statement was not honoring the "incomplete" flag and was marking the type evaluation for itself as completely resolved. A fix for this bug will be included in the next release.

jakebailey commented 3 years ago

This issue has been fixed in version 2021.9.4, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202194-29-september-2021