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

Get the type of a var #9788

Closed
Dimitri-WEI-Lingfeng opened this issue Dec 9, 2020 · 2 comments
Closed

Get the type of a var #9788

Dimitri-WEI-Lingfeng opened this issue Dec 9, 2020 · 2 comments
Labels

Comments

@Dimitri-WEI-Lingfeng
Copy link

Dimitri-WEI-Lingfeng commented Dec 9, 2020

Feature
A similar feature in typescript

const foo: number = 1
type Foo = typeof foo  // type Foo = number

function bar(x: string): void {

}

type Bar = typeof bar // type Bar = (x: string) => void

Pitch
The expected way in future python.
use the predefined method type is not possible because type(foo) returns string.
so we need to introduce an additinal method for example typing.typeof:

from typing import typeof
foo: int = 1
Foo = typeof(foo)  # equivalent to Foo = int

def bar(x: string) -> None :
    ...
Bar = typeof(bar)  # equivalent to Bar = Callable[[str], None]

or make typing.Type callable:

from typing import Type
foo: int = 1
Foo = Type(foo)  # equivalent to Foo = int
@danr
Copy link

danr commented Mar 25, 2021

Why was this closed? Is there a way to do TypeScript's typeof in mypy?

@Dimitri-WEI-Lingfeng
Copy link
Author

Dimitri-WEI-Lingfeng commented Mar 25, 2021

@danr I opened another one here in python/typing repo

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

No branches or pull requests

2 participants