classification
Title: Get the type from a variable
Type: enhancement Stage:
Components: Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: FFY00, wlf100220
Priority: normal Keywords:

Created on 2020-12-09 13:39 by wlf100220, last changed 2020-12-11 21:48 by FFY00.

Messages (2)
msg382792 - (view) Author: dimitri.wei (wlf100220) Date: 2020-12-09 13:39
**Feature**
A similar feature in typescript
```ts
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.
```py
from typing import Type
foo: int = 1
Foo = Type[foo]  # equivalent to Foo = int

def bar(x: string) -> None :
    ...
Bar = Type[bar]  # equivalent to Bar = Callable[[str], None]
```
msg382891 - (view) Author: Filipe LaĆ­ns (FFY00) * (Python triager) Date: 2020-12-11 21:48
There is type() to get the runtime type.

https://docs.python.org/3/library/functions.html#type

```python
>>> foo = 1
>>> type(foo)
<class 'int'>
```

Keep in mind that in Python using type hints does not enforce anything at runtime. You can say `foo: int = 1` but `int` there is just some extra information for type checkers, like mypy, to use.

If you want the type hints, you can use typing.get_type_hints() but this is only available for modules, classes or functions, not variables.
History
Date User Action Args
2020-12-11 21:48:17FFY00setnosy: + FFY00
messages: + msg382891
2020-12-09 13:48:09wlf100220settitle: Get the type from a var -> Get the type from a variable
2020-12-09 13:47:53wlf100220settitle: Get the type of from a var -> Get the type from a var
2020-12-09 13:41:15wlf100220setcomponents: - Demos and Tools
2020-12-09 13:39:46wlf100220create