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

Pyright/Pylance fails to infer type of generic classmethod T as Unknown by design #40

Open
sunfkny opened this issue Apr 17, 2024 · 1 comment

Comments

@sunfkny
Copy link

sunfkny commented Apr 17, 2024

Pyright unable to infer the type of Generic classmethod microsoft/pyright#7721

This break these classmethod Option.Some, Option.maybe, Result.Ok, Result.Err but shortcut function Some, maybe, Ok, Err still works, documentation should mention this

@sunfkny
Copy link
Author

sunfkny commented Apr 18, 2024

Use staticmethod and another type argument also fix this Pyright Playground

from typing import Generic, TypeVar, reveal_type

T = TypeVar("T")
T2 = TypeVar("T2")

class Option(Generic[T]):
    def __init__(self, value: T):
        self._val = value

    @staticmethod
    def Some(val: T2) -> "Option[T2]":
        return Option(val)

reveal_type(Option.Some(2))

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

No branches or pull requests

1 participant