Gradual Typing Concise and very good explanation
https://mypy-play.net/?mypy=latest&python=3.11 python
def foo(x: int):
y: str = x # NG
error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]import typing
def foo(x: typing.Any):
y: str = x # OK
python
def foo(x: int):
y: typing.Any = x # OK
def foo(x: int):
y: object = x # OK
python
def foo(x: object):
y: str = x # NG
error: Incompatible types in assignment (expression has type "object", variable has type "str") [assignment]
Unlike Any, object is just a top type, so trying to assign a value of type object to a value of type str (implicit downcast) is not allowed.
No guessing the return type. python
def foo(x: int):
return x
print(typing.reveal_type(foo))
note: Revealed type is "def (x: builtins.int) -> Any"def foo(x: int) -> int:
return x
print(typing.reveal_type(foo))
note: Revealed type is "def (x: builtins.int) -> builtins.int"This page is auto-translated from /nishio/漸進的型付け using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.