Make python a type inference language

Hi forum,

Can Python work like this:

  • If there are type annotations found in python code, type inference takes effect.
  • If there is not type annotation, old style dynamic type takes effect.

In type inference python code, the compiler knows variable or function types and does optimization for the code at compile time.

# example 1: parameter annotation
def f1(num: int):
    ...

# example 2: return annotation
def f2(num) -> bool:
    ...

# example 3: variable annotation
animal: str = 'snake'
var animal2: str = 'elephant'  # new keyword var like Swift's

Hello, @ljh. I think, you should change the post-tag as IDEAS to get some feedback and comments about your idea.

1 Like

Yup, just like @sandraC said, your topic fits better to the #ideas category, so it can be considered as a proposal to the Python language, and you can get better feedbacks :wink:

I was going to change the category to ideas, but this was already mostly addressed in @ljh 's other thread:

and the core devs and typing people have been very clear that making the core Python language statically rather than dynamically typed is not realistically going to happen anytime soon, and there are many great existing tools to do this already with Python code, e.g. Cython and mypyc.

2 Likes