-
Notifications
You must be signed in to change notification settings - Fork 769
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 makes type hints in Python unusable for me #5906
Comments
class Foo:
a: int
def build(some_class):
instance: some_class
return instance
instance = build(Foo)
instance. I am not sure what you meant by
did you mean
otherwise, if you change it to either anyway, both should work. just |
and why it works with |
I'll also note that |
Maybe I am just mocking python with my wish to get tips when I wish to instantiate an instance using some builder function with a class name only |
But ideally, screenshots show what I am trying to achieve. I haven't found any other reasonable way to help IDE to provide me with tips |
do something like this? from typing import Type, TypeVar
T = TypeVar("T")
class Foo:
a: int
class Bar:
b: int
def build(some_class: Type[T]) -> T:
instance: T = some_class()
return instance
foo_instance = build(Foo)
bar_instance = build(Bar)
foo_instance.
bar_instance. |
I don't think you have the right mental model of type hints, which explains why it's not working as you expect. You may find it useful to read this documentation. The Python typing spec may also provide some clarity. As @heejaechang mentioned, the code in your screen shot will not work at runtime. It's therefore unclear what your objective is. Do you intend to instantiate the class you pass to class Foo:
a: int
def build(some_class):
return some_class()
instance = build(Foo) Now when you type |
Yes, exactly. But I would like to build the instance based on the provided class and get type hints |
Ohh, well, thank you very much, sir! This is exactly what I was looking for ❤️ |
That solves my problem, thank you guys, very grateful |
The main problem was how to tell IDE that the returned instance should be of some generic type without necessarily having to call a constructor. And I did not know if it works with |
From Marketplace review:
The text was updated successfully, but these errors were encountered: