Skip to content
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

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

Closed
wvolkov opened this issue Sep 24, 2021 · 9 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@wvolkov
Copy link

wvolkov commented Sep 24, 2021

Environment data

  • Language Server version: 2021.9.3 (pyright d2771b18)
  • OS and version: Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7

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

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
Copy link
Contributor

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

jakebailey commented Sep 24, 2021

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

jakebailey commented Sep 24, 2021

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
Copy link
Author

wvolkov commented Sep 24, 2021

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

@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
Copy link
Member

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

@jakebailey jakebailey added the needs investigation Could be an issue - needs investigation label Sep 24, 2021
@github-actions github-actions bot removed the triage label Sep 24, 2021
@erictraut
Copy link
Contributor

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 jakebailey added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Sep 27, 2021
@jakebailey
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants