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 makes type hints in Python unusable for me #5906

Closed
debonte opened this issue May 22, 2024 · 14 comments
Closed

Pylance makes type hints in Python unusable for me #5906

debonte opened this issue May 22, 2024 · 14 comments
Assignees

Comments

@debonte
Copy link
Contributor

debonte commented May 22, 2024

From Marketplace review:

Andy Yermakov
Makes type hints in Python unusable for me. When turned off, type hints work again

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label May 22, 2024
@debonte debonte added the waiting for user response Requires more information from user label May 22, 2024
@yermandy
Copy link

Hello. This is what I meant:

With Pylance enabled I see no hints anymore in the following use case:

Screenshot 2024-05-22 at 19 51 48

When disabled:

Screenshot 2024-05-22 at 19 52 16

@heejaechang
Copy link
Contributor

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

instance: some_class

did you mean

instance = some_class() or instance = some_class

otherwise, instance: some_class, instance is uninitialized. so it has no value.

if you change it to either instance = some_class() - instance of some_class type, or instance = some_class - instance is type alias for some_class, and instance. will either show a as instance member or class member.

anyway, both should work. just instance: some_class doesn't work since it is invalid code. (if you run the code, python will throw)

@heejaechang
Copy link
Contributor

and why it works with pylance disabled, probably jedi has more relaxed heuristic to show something even if code is wrong.

@debonte debonte assigned heejaechang and unassigned debonte May 22, 2024
@rchiodo
Copy link
Contributor

rchiodo commented May 22, 2024

Pylance will show an error here if you turn on 'basic' typecheckingmode:

image

@erictraut
Copy link
Contributor

I'll also note that some_class here is a variable, and variables are not allowed in type annotations. The Python typing spec is very clear on this point. If you want to use type annotations ("hints") in your code, you need to use them in a valid manner. If you don't, tools like pylance that conform to the Python typing standard won't understand what you're trying to do.

@yermandy
Copy link

yermandy commented May 22, 2024

It seems that I was not clear enough. The point was rather that by using instance: some_class, I say to IDE that the thing that is returned from this function will have a type (class) that is passed as an argument to the function. Example (this code is not supposed to work, it just shows how IDE helps):

class Foo:
    a: int


class Bar:
    b: int


def build(some_class):
    instance: some_class = ... instantiate(some_class) # some method that will instantiate the class
    # `instantiate` method has no typing information so I provide the type hint as this: ": some_class"

    return instance


foo_instance = build(Foo)
bar_instance = build(Bar)

foo_instance.
bar_instance.

Then, I have hints:

Screenshot 2024-05-22 at 21 05 13 Screenshot 2024-05-22 at 21 05 18

@yermandy
Copy link

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

@yermandy
Copy link

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

@heejaechang
Copy link
Contributor

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.

@erictraut
Copy link
Contributor

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 build? If so, then the code should look like this:

class Foo:
    a: int

def build(some_class):
    return some_class()

instance = build(Foo)

Now when you type instance., you'll see that pylance offers a as a suggestion.

@yermandy
Copy link

Do you intend to instantiate the class you pass to build?

Yes, exactly. But I would like to build the instance based on the provided class and get type hints

@yermandy
Copy link

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.

Ohh, well, thank you very much, sir! This is exactly what I was looking for ❤️

@yermandy
Copy link

That solves my problem, thank you guys, very grateful

@debonte debonte closed this as completed May 22, 2024
@debonte debonte added by design and removed waiting for user response Requires more information from user needs repro Issue has not been reproduced yet labels May 22, 2024
@yermandy
Copy link

yermandy commented May 22, 2024

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 T = TypeVar("T") and then def build(some_class: Type[T]) -> T:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants