you can use an annotation
def funk() → list[int]:
return [1,2,3]
and use them as hints to the compiler, something like static typing, but if you don’t use annotations, it will be applied automatically to variables, you can apply
x: int = 123
What compiler?
I’m moving this to ‘Help’ because it lacks sufficient detail for meaningful discussion in ‘Ideas’.
You may be interested in Cython and mypyc. Both of these tools make use of type hints to help compile Python code into faster C extension modules.
I’m a little confused, I’m used to static typing in C++ or compiled languages
It’s not clear what you are asking. Is it why something like
x: str = 123
doesn’t cause an error at runtime? Python is still a dynamically typed language, but type hints let you describe your intent which a tool like mypy
can check. The above still creates an int
-valued variable at runtime, but mypy
will assume you meant for x
to be a str
and flag the assignment as an error. This kind of check doesn’t require the code to be executed, so it can be both faster and safer to let mypy
catch it instead of relying on a unit test.