'How to deal with unknown types of functions/methods from imported modules
I'm wondering what's the best approach to deal with unknown types of functions/methods associated with other modules. Note that I'm using strict
mode
For example, I have the following:
rooms: List[str] = list(mongo_client.devices.distinct("room"))
mongo_client
is just an instance of a MongoClient
imported from pymongo
. VSCode screams that it doesn't know the type of the distinct
method:
Type of "distinct" is partially unknown
Type of "distinct" is "(key: Unknown, filter: Unknown = None, session: Unknown = None, **kwargs: Unknown) -> Unknown"PylancereportUnknownMemberType
Argument type is unknown
Argument corresponds to parameter "iterable" in function "__init__"PylancereportUnknownArgumentType
What I can do:
- Add
reportUnknownMemberType
topyrightconfig.json
; however, while this removes the previous warning, it's also going to disable warnings that I probably really want - Add
# type: ignore
on the line with that distinct call; I usually hate having flying ignore-comments like this and I don't think it "fixes" anything - Create a stub file myself
What would you do? Should I not use strict
mode? Most of the project was written with the strict mode activated to make sure I'm not missing anything. Is these some cast
trick I can do?
Thanks!
Solution 1:[1]
This isn't an ideal solution since it ruins the import's IntelliSense, but it avoids disabling type checks you might want elsewhere and makes it so you don't have to write your own stubs.
from typing import Any, cast
from somewhere import Something # type: ignore
Something = cast(Any, Something)
Now you can use Something
freely.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Grant Gryczan |