Skip to content

Commit aea31db

Browse files
committed
first steps to type hints
1 parent 55e7afc commit aea31db

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

notation/software.sty

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ text={\softwareStyle{Mypy}},%
4848
name={Mypy},%
4949
sort={Mypy},%
5050
description={%
51-
is the static type-checking tool for \python~\cite{LLHSVRZSJYYMC2024MOSTFP}. %
51+
\pythonIdx{Mypy}%
52+
is the static type checking tool for \python~\cite{LLHSVRZSJYYMC2024MOSTFP}. %
5253
Learn more at \url{https://github.com/python/mypy} or in \cref{sec:variableTypesAndTypeHints}.%
5354
}%
5455
}%
@@ -84,6 +85,7 @@ text={\softwareStyle{pip}},%
8485
name={pip},%
8586
sort={pip},%
8687
description={%
88+
\pythonIdx{pip}%
8789
is the standard tool to install \python\ software packages from the \pgls{pypi} repository~\cite{PSF2024IPM}. %
8890
To install a package \bashil{thepackage} hosted on \pgls{pypi}, type \bashil{pip install thepackage} into the \pgls{terminal}. %
8991
Learn more at \url{https://packaging.python.org/installing}.%

text/main/basics/variables/typesAndTypeHints/typesAndTypeHints.tex

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
\endhsection%
1515
%
1616
\hsection{Types and Confusion}%
17+
\label{sec:typesAndConfusion}%
1718
\gitPythonAndOutput{\programmingWithPythonCodeRepo}{01_variables}{variable_types_wrong.py}{--args format}{variables:types_wrong}{%
1819
An example for the confusing variable types.}
1920

@@ -62,9 +63,9 @@
6263
%
6364
\begin{enumerate}%
6465
%
65-
\item Use static type-checking tools to find such potential errors in our code.%
66+
\item Use static type checking tools to find such potential errors in our code.%
6667
%
67-
\item Use type hints to annotate variables with their types to \emph{a)}~make our intention clearer and \emph{b)}~support type-checking tools.%
68+
\item Use type hints to annotate variables with their types to \emph{a)}~make our intention clearer and \emph{b)}~support type checking tools.%
6869
%
6970
\end{enumerate}%
7071
%
@@ -95,7 +96,7 @@
9596
In \python, which allows for dynamic typing and is an interpreted language, we will use a tool like \mypy~\cite{LLHSVRZSJYYMC2024MOSTFP}.
9697
You can install this tool by opening a terminal.
9798
Under \ubuntu, you therefore press \ubuntuTerminal, and under \windows, you \windowsTerminal.
98-
Then type in \bashil{pip install mypy} and hit \keys{\enter}.
99+
Then type in \bashil{pip install mypy}\pythonIdx{Mypy}\pythonIdx{pip} and hit \keys{\enter}.
99100
The \mypy\ tool will be installed as illustrated in \cref{fig:pipInstallMypy}.
100101

101102
We can now apply the tool to the program from \cref{lst:variables:types_wrong}.
@@ -115,14 +116,39 @@
115116
It also cannot fix the errors, as it cannot what the programmer actually intended to do.
116117
But knowing that line~4 in \cref{lst:variables:types_wrong} is probably wrong will help the programmer to fix that error or oversight before passing the program on to someone else.%
117118
%
118-
\bestPractice{staticTypeChecking}{Every program should pass static type-checking with tools such as \mypy. %
119+
\bestPractice{staticTypeChecking}{Every program should pass static type checking with tools such as \mypy. %
119120
Any issue found by the tools should be fixed. %
120-
In other words, type-check the program. %
121-
If there is an error, fix the error and \emph{type-check it again}. %
122-
Repeat this until no errors are found anyore.%
121+
In other words, type check the program. %
122+
If there is an error, fix the error and \emph{type check it again}. %
123+
Repeat this until no errors are found anymore.%
123124
}%
124125
%
125126
\endhsection%
127+
%
128+
\hsection{Type Hints}%
129+
%
130+
When we discussed \cref{lst:variables:types_wrong} in \cref{sec:typesAndConfusion}, we stated that there could be two reasons for the error in the code:
131+
Either, the author accidentally mixed-up two datatypes or operators~(\pythonilIdx{/}~vs.~\pythonilIdx{//}) or they chose a misleading name for their variable~\pythonil{int_var}.
132+
The problem that any type checking tool faces is that it cannot know the intention of the programmer.
133+
It can find that line~4 four is probably wrong, because the variable \pythonil{int_var}, which former contained an~\pythonil{int}, now gets a \pythonil{float} value assigned to it.
134+
135+
Oddly enough, the problem of guessing the intention of the programmer does not exist in a statically typed language like~pgls{C}.
136+
Here, we \emph{need} to define the type of every variable before assigning a value to it.
137+
Therefore, if the programmer would have wanted \pythonil{int_var} to strictly be an integer, they would have declared it as an integer variable.
138+
If they wanted to store \pythonils{float} in it, they would have declared it as a \pythonil{float} variable.
139+
The compiler would have seen any malpractice right away and could tell us that either line~4 is wrong or our initial assignment of an \pythonil{int} to the variable in line~1.
140+
141+
This is where the dynamic typing and lenience of \python\ comes back to bite us.
142+
It is very convenient for small projects, but as soon as the projects get bigger, it creates a mess.
143+
Remember that \inQuotes{real programs} are much more complex than \cref{lst:variables:types_wrong}.
144+
Imagine wading through thousands of lines of code to figure out what type a variable has, and, while doing so, remember that \python\ permits overwriting the contents of a variable with objects of an entirely different type whenever it pleases us.
145+
146+
Realizing that dynamic typing can be a blessing but also a problem, \emph{optional} type hints were introduced into the \python\ language.
147+
We can now declare the type of a variable if we want.
148+
This solves the above problem basically entirely and allows us to tell type checking tools our intention.%
149+
%
150+
\endhsection%
151+
%
126152
\FloatBarrier%
127153
\endhsection%
128154
%

0 commit comments

Comments
 (0)