<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Type_annotations</id>
	<title>Type annotations - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Type_annotations"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Type_annotations&amp;action=history"/>
	<updated>2026-06-02T19:30:22Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Type_annotations&amp;diff=324&amp;oldid=prev</id>
		<title>Davrot at 16:10, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Type_annotations&amp;diff=324&amp;oldid=prev"/>
		<updated>2025-10-17T16:10:48Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:10, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== Goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We want to use static type checking and type annotations in our Python code for detecting errors we made. We will use the mypy extension in VS code for that.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int = 0&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We want to use static type checking and type annotations in our Python code for detecting errors we made. We will use the mypy extension in VS code for that.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int = 0&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;b: float = 0.0&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;b: float = 0.0&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l14&quot;&gt;Line 14:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 14:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[…]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[…]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.&amp;lt;/blockquote&amp;gt;I would redefine this list a bit: * It is a part of your automatic documentation (like with meaningful variable names). If another person gets your source code they understand it easier. * You editor might thank you. Do to some new features in Python 3.10, the modern editors that do syntax highlighting and error checking have a harder time to infer what you mean. The more it need to think about what you mean, the slower your editor might get or even fail to show you syntax highlighting. * Static code analysis is really helpful. It showed me any problems ahead that I would have figured out the hard way otherwise. * Packages like the just-in-time compiler numba can produce better results if you can tell it what the variables are.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.&amp;lt;/blockquote&amp;gt;I would redefine this list a bit:  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* It is a part of your automatic documentation (like with meaningful variable names). If another person gets your source code they understand it easier.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* You editor might thank you. Do to some new features in Python 3.10, the modern editors that do syntax highlighting and error checking have a harder time to infer what you mean. The more it need to think about what you mean, the slower your editor might get or even fail to show you syntax highlighting.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Static code analysis is really helpful. It showed me any problems ahead that I would have figured out the hard way otherwise.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Packages like the just-in-time compiler numba can produce better results if you can tell it what the variables are.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== How do we do it? ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== How do we do it? ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki:diff:1.41:old-151:rev-324:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Type_annotations&amp;diff=151&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot; == Goal == We want to use static type checking and type annotations in our Python code for detecting errors we made. We will use the mypy extension in VS code for that.&lt;syntaxhighlight lang=&quot;python&quot;&gt;a: int = 0 b: float = 0.0 a = b     Incompatible types in assignment (expression has type &quot;float&quot;, variable has type &quot;int&quot;)&lt;/syntaxhighlight&gt;Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == Why [https://peps.python.org/pep-0484/ Type hints]? == Why we got type...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Type_annotations&amp;diff=151&amp;oldid=prev"/>
		<updated>2025-10-15T13:25:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; == Goal == We want to use static type checking and type annotations in our Python code for detecting errors we made. We will use the mypy extension in VS code for that.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int = 0 b: float = 0.0 a = b     Incompatible types in assignment (expression has type &amp;quot;float&amp;quot;, variable has type &amp;quot;int&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == Why [https://peps.python.org/pep-0484/ Type hints]? == Why we got type...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
== Goal ==&lt;br /&gt;
We want to use static type checking and type annotations in our Python code for detecting errors we made. We will use the mypy extension in VS code for that.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int = 0&lt;br /&gt;
b: float = 0.0&lt;br /&gt;
a = b     Incompatible types in assignment (expression has type &amp;quot;float&amp;quot;, variable has type &amp;quot;int&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
== Why [https://peps.python.org/pep-0484/ Type hints]? ==&lt;br /&gt;
Why we got type hints according PEP 484 – Type Hints (29-Sep-2014):&amp;lt;blockquote&amp;gt;[https://www.python.org/dev/peps/pep-0484/#id11 Rationale and Goals]&lt;br /&gt;
&lt;br /&gt;
This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information.&lt;br /&gt;
&lt;br /&gt;
Of these goals, static analysis is the most important. This includes support for off-line type checkers such as mypy, as well as providing a standard notation that can be used by IDEs for code completion and refactoring.&lt;br /&gt;
&lt;br /&gt;
[…]&lt;br /&gt;
&lt;br /&gt;
It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.&amp;lt;/blockquote&amp;gt;I would redefine this list a bit: * It is a part of your automatic documentation (like with meaningful variable names). If another person gets your source code they understand it easier. * You editor might thank you. Do to some new features in Python 3.10, the modern editors that do syntax highlighting and error checking have a harder time to infer what you mean. The more it need to think about what you mean, the slower your editor might get or even fail to show you syntax highlighting. * Static code analysis is really helpful. It showed me any problems ahead that I would have figured out the hard way otherwise. * Packages like the just-in-time compiler numba can produce better results if you can tell it what the variables are.&lt;br /&gt;
&lt;br /&gt;
== How do we do it? ==&lt;br /&gt;
Variables are assigned to a type the &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;first&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; time when used or can be defined even before use:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int&lt;br /&gt;
b: int = 0&amp;lt;/syntaxhighlight&amp;gt;You are allowed to connect a variable &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;once and only once&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; to a type. If you assign a type a second time to a variable then you will get an error and have to remove the second assignment.&lt;br /&gt;
&lt;br /&gt;
For functions it looks a bit different because we have to handle the type of the return value with the &amp;#039;&amp;#039;&amp;#039;-&amp;amp;#x3E;&amp;#039;&amp;#039;&amp;#039; construct:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;def this_is_a_function() -&amp;gt; None:&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
def this_is_a_function() -&amp;gt; int:&lt;br /&gt;
    return 5&lt;br /&gt;
&lt;br /&gt;
def this_is_a_function(a: int) -&amp;gt; int:&lt;br /&gt;
    return a&lt;br /&gt;
&lt;br /&gt;
def this_is_a_function(a: int, b: int = 8) -&amp;gt; int:&lt;br /&gt;
    return a + b&lt;br /&gt;
&lt;br /&gt;
def this_is_a_function(a: int, b: int = 8) -&amp;gt; tuple[int, int]:&lt;br /&gt;
    return a, b&amp;lt;/syntaxhighlight&amp;gt;Please note, that there is a difference how type annotations worked for older version. I will cover only Python 3.10 and newer. The official documentation can be found [https://docs.python.org/3/library/typing.html here].&lt;br /&gt;
&lt;br /&gt;
== Ignore a line ==&lt;br /&gt;
You can force MyPy to ignore a line if you add the comment # type: ignore to that line.&lt;br /&gt;
&lt;br /&gt;
Please check [https://mypy.readthedocs.io/en/stable/common_issues.html Common issues and solutions] if you run in strange problems.&lt;br /&gt;
&lt;br /&gt;
== MyPy (command line) ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;shell&amp;quot;&amp;gt;pip install mypy&amp;lt;/syntaxhighlight&amp;gt;If you don’t vs code then you can use the mypy command line tool:&amp;lt;syntaxhighlight lang=&amp;quot;shell&amp;quot;&amp;gt;mypy test2.py &amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;shell&amp;quot;&amp;gt;test2.py:3: error: Incompatible types in assignment (expression has type &amp;quot;float&amp;quot;, variable has type &amp;quot;int&amp;quot;)&lt;br /&gt;
Found 1 error in 1 file (checked 1 source file)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== MyPy under VS Code ==&lt;br /&gt;
Go under Extensions and install the &amp;#039;&amp;#039;&amp;#039;Mypy Type Checker&amp;#039;&amp;#039;&amp;#039; from &amp;#039;&amp;#039;&amp;#039;Microsoft&amp;#039;&amp;#039;&amp;#039;.&amp;lt;div class=&amp;quot;figure&amp;quot;&amp;gt;&lt;br /&gt;
[[File:9087234 image0.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://mypy.readthedocs.io/en/latest/builtin_types.html Built-in types] ==&lt;br /&gt;
&lt;br /&gt;
* If the type starts with an upper letter then you might import it from the typing module like&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from typing import Any&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* If you have no clue what type something has, well use &amp;#039;&amp;#039;&amp;#039;type()&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import torch&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def func() -&amp;gt; None:&lt;br /&gt;
    return&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
a = 0&lt;br /&gt;
b = np.zeros((10,))&lt;br /&gt;
c = torch.zeros((10, 1))&lt;br /&gt;
d = func&lt;br /&gt;
&lt;br /&gt;
print(type(a))&lt;br /&gt;
print(type(b))&lt;br /&gt;
print(type(c))&lt;br /&gt;
print(type(d))&amp;lt;/syntaxhighlight&amp;gt;Output:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;torch.Tensor&amp;#039;&amp;gt;&lt;br /&gt;
&amp;lt;class &amp;#039;function&amp;#039;&amp;gt;&amp;lt;/syntaxhighlight&amp;gt;The correct typing would have been:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import torch&lt;br /&gt;
from typing import Callable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def func() -&amp;gt; None:&lt;br /&gt;
    return&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
a: int = 0&lt;br /&gt;
b: np.ndarray = np.zeros((10,))&lt;br /&gt;
c: torch.Tensor = torch.zeros((10, 1))&lt;br /&gt;
d: Callable = func&amp;lt;/syntaxhighlight&amp;gt;As you can see, we had to change &amp;#039;&amp;#039;&amp;#039;b&amp;#039;&amp;#039;&amp;#039; a bit because we didn’t use &amp;#039;&amp;#039;&amp;#039;import numpy&amp;#039;&amp;#039;&amp;#039; but used &amp;#039;&amp;#039;&amp;#039;import numpy as np&amp;#039;&amp;#039;&amp;#039;. Thus we had to use &amp;#039;&amp;#039;&amp;#039;np.ndarray&amp;#039;&amp;#039;&amp;#039; instead of &amp;#039;&amp;#039;&amp;#039;numpy.ndarray&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Concerning &amp;#039;&amp;#039;&amp;#039;&amp;amp;#x3C;class ‘function’&amp;amp;#x3E;&amp;#039;&amp;#039;&amp;#039;, this is a specical case. And requires an import from the typing module via &amp;#039;&amp;#039;&amp;#039;from typing import Callable&amp;#039;&amp;#039;&amp;#039;. More about that later.&lt;br /&gt;
&lt;br /&gt;
=== [https://mypy.readthedocs.io/en/latest/builtin_types.html#simple-types Simple types] ===&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Here are examples of some common built-in types:&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Type&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|int&lt;br /&gt;
|integer&lt;br /&gt;
|-&lt;br /&gt;
|float&lt;br /&gt;
|floating point number&lt;br /&gt;
|-&lt;br /&gt;
|bool&lt;br /&gt;
|boolean value (subclass of int)&lt;br /&gt;
|-&lt;br /&gt;
|str&lt;br /&gt;
|text, sequence of unicode codepoints&lt;br /&gt;
|-&lt;br /&gt;
|bytes&lt;br /&gt;
|8-bit string, sequence of byte values&lt;br /&gt;
|-&lt;br /&gt;
|object&lt;br /&gt;
|an arbitrary object (object is the common base class)&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int = 0&lt;br /&gt;
b: float = 0.0&lt;br /&gt;
c: bool = True&lt;br /&gt;
d: str = &amp;quot;LaLa&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [https://mypy.readthedocs.io/en/latest/builtin_types.html#any-type Any type] ===&lt;br /&gt;
Special type indicating an unconstrained type.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from typing import Any&lt;br /&gt;
a: Any = 0&lt;br /&gt;
b: float = 0.0&lt;br /&gt;
a = b&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== [https://mypy.readthedocs.io/en/latest/builtin_types.html#generic-types Generic types] ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Type&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|list[str]&lt;br /&gt;
|list of str objects&lt;br /&gt;
|-&lt;br /&gt;
|tuple[int, int]&lt;br /&gt;
|tuple of two int objects (tuple[()] is the empty tuple)&lt;br /&gt;
|-&lt;br /&gt;
|tuple[int, …]&lt;br /&gt;
|tuple of an arbitrary number of int objects&lt;br /&gt;
|-&lt;br /&gt;
|dict[str, int]&lt;br /&gt;
|dictionary from str keys to int values&lt;br /&gt;
|-&lt;br /&gt;
|Iterable[int]&lt;br /&gt;
|iterable object containing ints&lt;br /&gt;
|-&lt;br /&gt;
|Sequence[bool]&lt;br /&gt;
|sequence of booleans (read-only)&lt;br /&gt;
|-&lt;br /&gt;
|Mapping[str, int]&lt;br /&gt;
|mapping from str keys to int values (read-only)&lt;br /&gt;
|-&lt;br /&gt;
|type[C]&lt;br /&gt;
|type object of C (C is a class/type variable/union of types)&lt;br /&gt;
|}&lt;br /&gt;
Examples:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;la: list = [&amp;quot;a&amp;quot;, 1, 3.3]&lt;br /&gt;
&lt;br /&gt;
ta: tuple = (&amp;quot;a&amp;quot;, 1, 3.3)&lt;br /&gt;
tb: tuple[str, int, float] = (&amp;quot;a&amp;quot;, 1, 3.3)&amp;lt;/syntaxhighlight&amp;gt;Wrong:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;la: list[str, int, float] = [&amp;quot;a&amp;quot;, 1, 3.3]&amp;lt;/syntaxhighlight&amp;gt;Correct:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;la: list[str | int | float] = [&amp;quot;a&amp;quot;, 1, 3.3]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#typing.Union |] ==&lt;br /&gt;
In the case you expect a variable that can have differnt types over it’s lifetime. Let us say you initialize it with None and later want to store integer in it:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: None | int = None&amp;lt;/syntaxhighlight&amp;gt;An other example is this:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import torch&lt;br /&gt;
import numpy as np&lt;br /&gt;
a: np.ndarray  | torch.Tensor = torch.zeros((100,))&amp;lt;/syntaxhighlight&amp;gt;This is called a [https://docs.python.org/3/library/typing.html#typing.Union Union]. The Union with &amp;#039;&amp;#039;&amp;#039;None&amp;#039;&amp;#039;&amp;#039; is called [https://docs.python.org/3/library/typing.html#typing.Optional Optional]. But nowadays you just need to remember &amp;#039;&amp;#039;&amp;#039;|&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
In the real world I encountered this problem:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int | None = 1&lt;br /&gt;
b: int&lt;br /&gt;
&lt;br /&gt;
b = a  # Incompatible types in assignment (expression has type &amp;quot;int | None&amp;quot;, variable has type &amp;quot;int&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;The solution is to use [https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement assert]:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: int | None = 1&lt;br /&gt;
b: int&lt;br /&gt;
&lt;br /&gt;
assert a is not None&lt;br /&gt;
&lt;br /&gt;
b = a&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#typing.TypeAlias TypeAlias] ==&lt;br /&gt;
You can create an alias for more complicated types&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from typing import TypeAlias&lt;br /&gt;
&lt;br /&gt;
Numbis: TypeAlias = int | float&lt;br /&gt;
a: Numbis&lt;br /&gt;
&lt;br /&gt;
a = 1&lt;br /&gt;
a = 1.1&lt;br /&gt;
a = &amp;quot;Hello&amp;quot; # Incompatible types in assignment (expression has type &amp;quot;str&amp;quot;, variable has type &amp;quot;int | float&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#typing.Tuple Tuple] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: tuple[int, str, int] = (5, &amp;quot;Hello&amp;quot;, 6)&lt;br /&gt;
a = (&lt;br /&gt;
    &amp;quot;Hello&amp;quot;,&lt;br /&gt;
    4,&lt;br /&gt;
    4,&lt;br /&gt;
)  # Incompatible types in assignment (expression has type &amp;quot;Tuple[str, int, int]&amp;quot;, variable has type &amp;quot;Tuple[int, str, int]&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;Or if you don’t care about what is in the tuple&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;a: tuple = (5, &amp;quot;Hello&amp;quot;, 6)&lt;br /&gt;
a = (&amp;quot;Hello&amp;quot;, 4, 4)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#typing.List List] ==&lt;br /&gt;
A generic list:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;mylist: list = []&lt;br /&gt;
&lt;br /&gt;
mylist.append(1)&lt;br /&gt;
mylist.append(2)&lt;br /&gt;
mylist.append(&amp;quot;Hello&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;Or defining more details about the list:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;mylist: list[int] = []&lt;br /&gt;
&lt;br /&gt;
mylist.append(1)&lt;br /&gt;
mylist.append(2)&lt;br /&gt;
mylist.append(&lt;br /&gt;
    &amp;quot;Hello&amp;quot;&lt;br /&gt;
)  # Argument 1 to &amp;quot;append&amp;quot; of &amp;quot;list&amp;quot; has incompatible type &amp;quot;str&amp;quot;; expected &amp;quot;int&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(mylist) # -&amp;gt; [1, 2, &amp;#039;Hello&amp;#039;]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#typing.Dict Dict] ==&lt;br /&gt;
Generic dictionary:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;mydict: dict = {&amp;quot;A&amp;quot;: 1, &amp;quot;B&amp;quot;: 3.14, &amp;quot;C&amp;quot;: &amp;quot;Hello&amp;quot;}&amp;lt;/syntaxhighlight&amp;gt;We can give it more information. However, we have to be careful to include the types correctly.&lt;br /&gt;
&lt;br /&gt;
This is wrong:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;mydict_a: dict[str, int] = {&amp;quot;A&amp;quot;: 1, &amp;quot;B&amp;quot;: 3.14, &amp;quot;C&amp;quot;: &amp;quot;Hello&amp;quot;, 1: 1}&amp;lt;/syntaxhighlight&amp;gt;We get these errors:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;Dict entry 1 has incompatible type &amp;quot;str&amp;quot;: &amp;quot;float&amp;quot;; expected &amp;quot;str&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
Dict entry 2 has incompatible type &amp;quot;str&amp;quot;: &amp;quot;str&amp;quot;; expected &amp;quot;str&amp;quot;: &amp;quot;int&amp;quot;&lt;br /&gt;
Dict entry 3 has incompatible type &amp;quot;int&amp;quot;: &amp;quot;int&amp;quot;; expected &amp;quot;str&amp;quot;: &amp;quot;int&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;These are correct ways to handle it:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;mydict_a: dict[str | int, str | int | float] = {&amp;quot;A&amp;quot;: 1, &amp;quot;B&amp;quot;: 3.14, &amp;quot;C&amp;quot;: &amp;quot;Hello&amp;quot;, 1: 1}&lt;br /&gt;
mydict_b: dict[str, str | int | float] = {&amp;quot;A&amp;quot;: 1, &amp;quot;B&amp;quot;: 3.14, &amp;quot;C&amp;quot;: &amp;quot;Hello&amp;quot;, &amp;quot;1&amp;quot;: 1}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://numpy.org/devdocs/reference/typing.html Numpy] ==&lt;br /&gt;
Generic:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
a: np.ndarray = np.zeros((10, 1))&amp;lt;/syntaxhighlight&amp;gt;Protecting against a wrong dtype:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
from typing import Any&lt;br /&gt;
&lt;br /&gt;
a: np.ndarray[Any, np.dtype[np.uint64]]&lt;br /&gt;
&lt;br /&gt;
a = np.zeros((10, 1), dtype=np.uint64)&lt;br /&gt;
a = np.zeros((10, 1)) # -&amp;gt; Incompatible types in assignment (expression has type &amp;quot;ndarray[Any, dtype[floating[_64Bit]]]&amp;quot;, variable has type &amp;quot;ndarray[Any, dtype[unsignedinteger[_64Bit]]]&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PyTorch ==&lt;br /&gt;
Please note the big T!&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import torch&lt;br /&gt;
&lt;br /&gt;
a: torch.Tensor = torch.zeros((10, 1))&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#annotating-callable-objects Callable] ==&lt;br /&gt;
Callable means “function”. As you know, you can shove functions objects around and also use then as function arguments of other function. It is helpful to make sure that the function you get has the properties you expect.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;Callable[[Arg1Type, Arg2Type], ReturnType]&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from typing import Callable&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def function_a(x: int) -&amp;gt; int:&lt;br /&gt;
    return x + 1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def function_a_bad(x: int, y: int) -&amp;gt; int:&lt;br /&gt;
    return x + y&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def function_b(x, other_function: Callable[[int], int]) -&amp;gt; int:&lt;br /&gt;
    return other_function(x) ** 2&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(function_b(1, function_a))  # -&amp;gt; 4&lt;br /&gt;
&lt;br /&gt;
print(function_b(1, function_b)) # -&amp;gt; Argument 2 to &amp;quot;function_b&amp;quot; has incompatible type &amp;quot;Callable[[Any, Callable[[int], int]], int]&amp;quot;; expected &amp;quot;Callable[[int], int]&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== [https://docs.python.org/3/library/typing.html#newtype NewType], [https://docs.python.org/3/library/typing.html#generics Generics], [https://docs.python.org/3/library/typing.html#user-defined-generic-types User-defined generic types] ==&lt;br /&gt;
{: .topic-optional} This is an optional topic!&lt;br /&gt;
&lt;br /&gt;
Well… this exists… never used it.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|NewType&lt;br /&gt;
|Use the NewType helper to create distinct types&lt;br /&gt;
|-&lt;br /&gt;
|Generics&lt;br /&gt;
|Since type information about objects kept in containers cannot be statically inferred in a generic way, many container classes in the standard library support subscription to denote the expected types of container elements.&lt;br /&gt;
|-&lt;br /&gt;
|User-defined generic types&lt;br /&gt;
|A user-defined class can be defined as a generic class.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* [https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html Type hints cheat sheet]&lt;br /&gt;
* [https://docs.python.org/3/library/typing.html typing — Support for type hints]&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>