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

Upcasting #1261

Closed
dbrattli opened this issue Dec 10, 2020 · 3 comments
Closed

Upcasting #1261

dbrattli opened this issue Dec 10, 2020 · 3 comments
Labels
as designed Not a bug, working as intended

Comments

@dbrattli
Copy link

Describe the bug

Would it be possible to get upcasting using a function to type check its arguments? Or more generally that pyright checks that arguments with Type annotation of a generic type e.g Type[T] are compatible with other arguments where the generic type T is also used.

To Reproduce

from typing import TypeVar, Type
T = TypeVar("T")

class Base:
    pass

class Derived(Base):
    pass

class Other:
    pass

def upcast(type: Type[T], expr: T) -> T:
    return expr

d = Derived()
x = upcast(Other, d)

Would it be possible to get pyright to type check the last line and give error? Currently it will not detect that the function upcast is given two incompatible arguments. Here is how it looks in vscode today:

upcast(Base, d)  # should not show error
upcast(Other, d)  # should show error

Currently no error will show even if Other is not a super type of the type of d:

Screenshot 2020-12-10 at 08 17 56

VS Code extension or command-line

vscode extension: 1.1.94

@erictraut
Copy link
Collaborator

Pyright is doing the right thing here. It's correctly "solving" for the type of TypeVar T, and there are no type violations, so it's not displaying any errors.

I might be able to suggest a solution if I better understood what you're trying to do here. A derived class is always compatible with its base class, so there's no need to explicitly upcast.

If you want the static type checker to test whether an expression is of a type that is derived from Base, just do the following:

# Will not show an error
y: Base = d

# Will show an error
z: Other = d

Does that address your use case?

@dbrattli
Copy link
Author

dbrattli commented Dec 10, 2020

I was trying to implement the upcast and downcast from F#. Upcast is perhaps not needed as you say, but good for symmetry. After reading python/typing#565 I thought it should be possible to implement this using type annotations, and sort of expected pyright to give an error for upcast(Other, d) when d is not derived from Other.

@erictraut
Copy link
Collaborator

Yeah, there's no need for an upcast call. Upcasting is implicit in Python, as it is in most languages.

@erictraut erictraut added the as designed Not a bug, working as intended label Dec 10, 2020
heejaechang pushed a commit to heejaechang/pyright that referenced this issue Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants