<p>In Python, everything is treated as an object. This means that CPython interpreter will decide on the fly, what is the type of each variables or the function return type depending on the current state. This results in significant performance hits when compared to languages like C++ where the variables and functions are declared with a type explicitly. However, we also have an option to define type hints for the variables and functions in Python. However this is gladly ignored by the interpreter, and is only useful during linting. My question is can't the type hints be used to infer the types, and making the execution faster?</p>

<implementation><static-typing><python><reference-request>
Why does Python ignore type hints?
<p>Python's typing model is intended for <em>outside</em> checkers to check python code. Pretty much any expression can go in a type hint:</p>
<pre class="lang-python prettyprint-override"><code>x: print(&quot;hi&quot;) = 1
print(x)
</code></pre>
<p>produces</p>
<pre><code>hi
1
</code></pre>
<p>Obviously any typechecker will reject that as bogus, but the Python interpreter <em>has</em> to support it in order to fulfil the Python standard.</p>
<p>Leading on from that, there's no guarantee that the type hints are going to be correct, because by the Python standard, they don't have to be. This is also perfectly valid <em>to an interpreter</em>:</p>
<pre class="lang-python prettyprint-override"><code>x: str = 1
</code></pre>
<p>As for why the Python standard has been designed this way, it's a very long story that I don't know enough of to recount here, but it comes down to &quot;it doesn't fit Python's ideals as a language&quot;.</p>
<p>In terms of using types to speed something up, check out <code>mypyc</code> - it uses the fact that the code has to be correctly checked by <code>mypy</code> before it compiles code in order to speed up typed python code significantly.
For example, this would be sped up in exactly the manner you've described by <code>mypyc</code>, but it can't be by a normal Python interpreter:</p>
<pre class="lang-python prettyprint-override"><code>def fib(n: int) -&gt; int:
    if n &lt;= 1:
        return n
    return fib(n - 1) + fib(n - 2)
</code></pre>

CC BY-SA 4.0
2023-06-02T05:06:29.257Z
2023-06-02T06:29:31.517Z
2023-06-02T06:29:31.517Z
blueberry
CC BY-SA 4.0
2023-06-02T05:09:44.393Z
I agree that type hints do not have to be correct. But for someone who comes from a statically typed language, this choice always seemed odd that people who write the code would not know enough to give proper types. I will check definitely ```mypyc```. Thanks for the suggestion!
Hemanth Haridas
<p>Python is a dynamically typed language, and neither its developers nor most of its users want it to become a statically typed language instead. It's true that there are possible positions between &quot;type annotations are ignored by the interpreter&quot; and &quot;all expressions have a statically-known type&quot;. But any point along that spectrum other than &quot;type annotations are totally ignored&quot; requires the language itself to have <em>some</em> kind of static type system, in order for those type annotations to have any meaning.</p>
<p>So Python ignores type hints because the Python language isn't supposed to have a static type system at all. This is very reasonable for a language which started without having type annotations at all; there are many decisions to make when designing a static type system, but unlike designing for a new language, there are already users who would be upset about a type system that doesn't work the way they would want it to.</p>
<p>For example, a new language can decide to have <a href="https://en.wikipedia.org/wiki/Nominal_type_system" rel="noreferrer">nominal types</a> or <a href="https://en.wikipedia.org/wiki/Structural_type_system" rel="noreferrer">structural types</a>, and users who don't like the choice can just not use that language; but if you take an existing dynamically-typed language and give it a structural static type system, the users who would want nominal types instead will be upset.</p>
<p>The natural solution is to leave the design of type systems to third parties. If one group of users want nominal types, and another wants structural types, they can just use different type checkers and everyone is happy. If the Python language itself privileged one of those type systems by having types affect runtime behaviour, then not everyone would be happy.</p>
<hr />
<p>Another consideration is the release schedule. Think about Typescript, which likewise has optional type annotations which aren't checked at runtime and don't affect the behaviour of code at runtime; Typescript is continually developed, and puts out minor versions more often than Python. However, Typescript doesn't follow the convention of &quot;no breaking changes in minor versions&quot;, because too many improvements to Typescript are expected to be breaking changes. Every time the type-checking algorithm is improved, that means that some mistakes will be caught which weren't before, and so some existing code which previously compiled without errors will now have errors.</p>
<p>On the other hand, Python mostly <em>does</em> avoid breaking changes in minor versions, so if the Python language included rules for checking type annotations, we'd probably be on Python 12 by now. Or (more realistically) the type system would have to be much more stable, with improvements made much more rarely than users want to receive them. This weighs in favour of the Python language and Python typecheckers being separate, third-party projects with their own release schedules.</p>

CC BY-SA 4.0
2023-06-02T12:57:55.520Z
2023-06-02T12:57:55.520Z
kaya3
<p>In both the design of the Python language, and the design of the CPython interpreter (effectively the default implementation of Python, but there are others), performance is not a major objective.</p>
<p>Python has a relatively simple language design that comes with a lot of openness: programs can inspect and modify objects at runtime. A lot of libraries take advantage of that. This precludes a lot of optimization unless you know that no library is doing any patching of some core classes.</p>
<p>Type annotations don't really help with that because there's no requirement for them to be sound. Your program is still supposed to run as written if the type annotations are wrong. (You'd surely prefer it to run as intended — but for that you need a language that's designed as type-safe from the start. That wouldn't be Python.)</p>
<p>CPython does not even try to perform much optimization because it values debuggability over performance. In Python, you should be able to trace the execution of a program line by line. That doesn't work with an optimizer that reorders code around.</p>
<p>If you want performance in Python, you write the performance-sensitive parts in a low-level language like Fortran or C, and the high-level parts in Python. For example, Python is commonly used for numerical computation (a performance-demanding field) with <a href="https://numpy.org/" rel="nofollow noreferrer">numpy</a> (not written in Python) providing a high-performance implementation of basic operation, and Python used to combine the high-performance building blocks.</p>

CC BY-SA 4.0
2023-06-02T06:46:26.547Z
2023-06-02T06:46:26.547Z
Gilles 'SO- stop being evil'
CC BY-SA 4.0
2023-06-28T15:35:47.537Z
And yet I've always found python program much harder to debug than programs in other languages!
Stef
<p>Type hinting is a <a href="https://peps.python.org/pep-0484/" rel="noreferrer">relatively recent addition to Python</a>, so older code, and still most code, doesn't use them at all. Annotated code still needs to work with that existing code, so Python is a <em>gradual</em> system where typed and untyped code mixes together in one program.</p>
<p>When they are used, for most types they are purely structural, saying that certain methods are available but not where they came from, but for some builtins at least (like <code>int</code>) they do convey concrete implementation aspects that could be optimised. There <em>are</em> cases where static type information could usefully help in optimising code execution, but perhaps not as many as you'd expect.</p>
<p>Relying on the annotations would also require that they be <em>correct</em>, which Python makes no guarantee of. Ensuring this correctness needs <em>sound</em> gradual typing, which requires run-time checking of types as well and <a href="https://languagedesign.stackexchange.com/q/604/133">carries a whole lot of run-time and semantic effects</a>. It would be a really significant change to Python semantics to start enforcing these type annotations in that way, so the informational content of an annotation is even a little bit lower than it looks. In any case, the costs of the dynamic checking to make it sound enough to rely on almost certainly outweigh the performance benefits in almost all cases.</p>
<p>For a language like Python where code making use of very dynamic features and metaprogramming is fairly common, <strong>the benefit for most programs likely isn't high enough to be worth the costs no matter what</strong>. Even the long-standing nominal typing elements in Python (like <code>collections.abc.Sequence</code>) doesn't convey much about the actual implementation, and mutating classes at run time is not uncommon inside common Python frameworks. These objects need to be usable with all the type-hinted code, so that code needs to deal with a big variety of object shapes. This is all very different to what's typical in C++, where these things might happen at (or before) <em>compile</em> time, if anything.</p>
<p>Instead, just-in-time compilation is a more realistic performance enhancement to Python, which is <em>also</em> not included in the CPython implementation, but is in some alternative versions. For high-performance Python code, as used in some scientific computation, there are both libraries like numpy that keep everything inside, and specialised JIT systems like <a href="https://numba.pydata.org/" rel="noreferrer">Numba</a> that optimise specified functions at run time. These are more practical performance targets for Python.</p>

CC BY-SA 4.0
2023-06-02T05:25:20.363Z
2023-06-02T05:25:20.363Z
Michael Homer
# Why does Python ignore type hints?
Posted by: username_0 on 2023-06-02
In Python, everything is treated as an object. This means that CPython interpreter will decide on the fly, what is the type of each variables or the function return type depending on the current state. This results in significant performance hits when compared to languages like C++ where the variables and functions are declared with a type explicitly. However, we also have an option to define type hints for the variables and functions in Python. However this is gladly ignored by the interpreter, and is only useful during linting. My question is can't the type hints be used to infer the types, and making the execution faster?
**Tags:** implementation static-typing python reference-request
---
## 4 answers
### Answer by username_5 on 2023-06-02. Score: 7 | Accepted answer
Type hinting is a [relatively recent addition to Python](https://peps.python.org/pep-0484/), so older code, and still most code, doesn't use them at all. Annotated code still needs to work with that existing code, so Python is a *gradual* system where typed and untyped code mixes together in one program.

When they are used, for most types they are purely structural, saying that certain methods are available but not where they came from, but for some builtins at least (like `int`) they do convey concrete implementation aspects that could be optimised. There *are* cases where static type information could usefully help in optimising code execution, but perhaps not as many as you'd expect.

Relying on the annotations would also require that they be *correct*, which Python makes no guarantee of. Ensuring this correctness needs *sound* gradual typing, which requires run-time checking of types as well and [carries a whole lot of run-time and semantic effects](https://languagedesign.stackexchange.com/q/604/133). It would be a really significant change to Python semantics to start enforcing these type annotations in that way, so the informational content of an annotation is even a little bit lower than it looks. In any case, the costs of the dynamic checking to make it sound enough to rely on almost certainly outweigh the performance benefits in almost all cases.

For a language like Python where code making use of very dynamic features and metaprogramming is fairly common, **the benefit for most programs likely isn't high enough to be worth the costs no matter what**. Even the long-standing nominal typing elements in Python (like `collections.abc.Sequence`) doesn't convey much about the actual implementation, and mutating classes at run time is not uncommon inside common Python frameworks. These objects need to be usable with all the type-hinted code, so that code needs to deal with a big variety of object shapes. This is all very different to what's typical in C++, where these things might happen at (or before) *compile* time, if anything.

Instead, just-in-time compilation is a more realistic performance enhancement to Python, which is *also* not included in the CPython implementation, but is in some alternative versions. For high-performance Python code, as used in some scientific computation, there are both libraries like numpy that keep everything inside, and specialised JIT systems like [Numba](https://numba.pydata.org/) that optimise specified functions at run time. These are more practical performance targets for Python.
---
### Answer by username_1 on 2023-06-02. Score: 10
Python's typing model is intended for outside checkers to check python code. Pretty much any expression can go in a type hint:
x: print("hi") = 1
print(x)

produces
hi
1

Obviously any typechecker will reject that as bogus, but the Python interpreter has to support it in order to fulfil the Python standard.
Leading on from that, there's no guarantee that the type hints are going to be correct, because by the Python standard, they don't have to be. This is also perfectly valid to an interpreter:
x: str = 1

As for why the Python standard has been designed this way, it's a very long story that I don't know enough of to recount here, but it comes down to "it doesn't fit Python's ideals as a language".
In terms of using types to speed something up, check out mypyc - it uses the fact that the code has to be correctly checked by mypy before it compiles code in order to speed up typed python code significantly.
For example, this would be sped up in exactly the manner you've described by mypyc, but it can't be by a normal Python interpreter:
def fib(n: int) -> int:
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)
> **Comments:**
> **username_0** (2023-06-02): I agree that type hints do not have to be correct. But for someone who comes from a statically typed language, this choice always seemed odd that people who write the code would not know enough to give proper types. I will check definitely ```mypyc```. Thanks for the suggestion!  
---
### Answer by username_2 on 2023-06-02. Score: 5
Python is a dynamically typed language, and neither its developers nor most of its users want it to become a statically typed language instead. It's true that there are possible positions between "type annotations are ignored by the interpreter" and "all expressions have a statically-known type". But any point along that spectrum other than "type annotations are totally ignored" requires the language itself to have *some* kind of static type system, in order for those type annotations to have any meaning.

So Python ignores type hints because the Python language isn't supposed to have a static type system at all. This is very reasonable for a language which started without having type annotations at all; there are many decisions to make when designing a static type system, but unlike designing for a new language, there are already users who would be upset about a type system that doesn't work the way they would want it to.

For example, a new language can decide to have [nominal types](https://en.wikipedia.org/wiki/Nominal_type_system) or [structural types](https://en.wikipedia.org/wiki/Structural_type_system), and users who don't like the choice can just not use that language; but if you take an existing dynamically-typed language and give it a structural static type system, the users who would want nominal types instead will be upset.

The natural solution is to leave the design of type systems to third parties. If one group of users want nominal types, and another wants structural types, they can just use different type checkers and everyone is happy. If the Python language itself privileged one of those type systems by having types affect runtime behaviour, then not everyone would be happy.

---

Another consideration is the release schedule. Think about Typescript, which likewise has optional type annotations which aren't checked at runtime and don't affect the behaviour of code at runtime; Typescript is continually developed, and puts out minor versions more often than Python. However, Typescript doesn't follow the convention of "no breaking changes in minor versions", because too many improvements to Typescript are expected to be breaking changes. Every time the type-checking algorithm is improved, that means that some mistakes will be caught which weren't before, and so some existing code which previously compiled without errors will now have errors.

On the other hand, Python mostly *does* avoid breaking changes in minor versions, so if the Python language included rules for checking type annotations, we'd probably be on Python 12 by now. Or (more realistically) the type system would have to be much more stable, with improvements made much more rarely than users want to receive them. This weighs in favour of the Python language and Python typecheckers being separate, third-party projects with their own release schedules.
---
### Answer by username_3 on 2023-06-02. Score: 4
In both the design of the Python language, and the design of the CPython interpreter (effectively the default implementation of Python, but there are others), performance is not a major objective.

Python has a relatively simple language design that comes with a lot of openness: programs can inspect and modify objects at runtime. A lot of libraries take advantage of that. This precludes a lot of optimization unless you know that no library is doing any patching of some core classes.

Type annotations don't really help with that because there's no requirement for them to be sound. Your program is still supposed to run as written if the type annotations are wrong. (You'd surely prefer it to run as intended — but for that you need a language that's designed as type-safe from the start. That wouldn't be Python.)

CPython does not even try to perform much optimization because it values debuggability over performance. In Python, you should be able to trace the execution of a program line by line. That doesn't work with an optimizer that reorders code around.

If you want performance in Python, you write the performance-sensitive parts in a low-level language like Fortran or C, and the high-level parts in Python. For example, Python is commonly used for numerical computation (a performance-demanding field) with [numpy](https://numpy.org/) (not written in Python) providing a high-performance implementation of basic operation, and Python used to combine the high-performance building blocks.
> **Comments:**
> **username_4** (2023-06-28): And yet I've always found python program much harder to debug than programs in other languages!  
---
