Closed wvolkov closed 3 years ago
This seems like a bug in some async handling, at least in the hover; repeat the same thing and I get:
Making setup
return something lets me see:
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:
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())
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.
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.)
First, there's a type error in the sample above. The expression
DBClient(1)
isn't valid because the constructor forDBClient
accepts astr
, not anint
. 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:
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?
@wvolkov Type checking isn't enabled by default in Pylance; you need to set python.analysis.typeCheckingMode
or set it in a pyrightconfig.json
.
Ah, so fixing the type error fixes this. That's interesting.
I'm not really sure why that would be the case, if reveal_type gets the right answer, and it works here:
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.
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
Environment data
Code Snippet / Additional information
Assume class
DecisionEngine
, which creates an instance ofDBClient
, passing aninteger
instead ofstring
as specified below:Expected behaviour
at setup method of
DecisionEngine
class it is possible to drill down intosetup
method ofdb
object:Actual behaviour
it interprets
db
object asAny
type despite the fact that it is correctly recognize the type ofdb
object at upper line of code