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

a way to get the type of a variable #915

Closed
DetachHead opened this issue Nov 2, 2021 · 2 comments
Closed

a way to get the type of a variable #915

DetachHead opened this issue Nov 2, 2021 · 2 comments
Labels
resolution: duplicate The idea/problem was already reported topic: feature Discussions about new features for Python's type annotations

Comments

@DetachHead
Copy link

DetachHead commented Nov 2, 2021

in my use case if have a class with several generics, i want a convenient way to get the type of an instance, but it doesn't seem to be possible in any type checkers

from typing import Generic, TypeVar

T = TypeVar("T")

class Foo(Generic[T]):
    ...

foo = Foo[int]()
bar = Foo[str]()

baz: type[foo] | type[bar] = ...

mypy errors:

main.py:11: error: Variable "__main__.foo" is not valid as a type
main.py:11: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
main.py:11: error: Variable "__main__.bar" is not valid as a type

the alternative is to duplicate the types (which is annoying if the type annotation is large) or create a type alias, which is a pain

in typescript you can use typeof

interface Foo<T> {}

declare const foo: Foo<number>
declare const bar: Foo<string>

declare const baz: typeof foo | typeof bar

PS: i tried baz: reveal_type(foo) = foo, but no luck image

@erictraut
Copy link
Collaborator

I think you've already identified the workarounds that are available in Python today:

  1. Specify the type explicitly within the annotation
  2. Define a type alias and use that in the annotation

I wouldn't expect type[foo] to work. The equivalent of TypeScript's typeof foo would be type(foo). Currently that's not allowed because call expressions are not permitted within type annotation expressions. There are good reasons for that limitation, and (speaking as a type checker maintainer) I'd recommend that we leave that limitation in place and not create an exception.

This use case is not common, so it's reasonable that you create a type alias in this circumstance.

@srittau srittau added topic: feature Discussions about new features for Python's type annotations resolution: duplicate The idea/problem was already reported labels Nov 4, 2021
@srittau
Copy link
Collaborator

srittau commented Nov 4, 2021

I believe this is a duplicate of #769.

@srittau srittau closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution: duplicate The idea/problem was already reported topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

3 participants