Skip to content

Commit 535fd06

Browse files
Merge pull request #70 from rootstrap/python/typing-mypy
Add typing hints documentation to python readme
2 parents 4af7ccd + e4739b1 commit 535fd06

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

python/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ prescriptions for Python developmen that mostly applies for Django.
6464
- [Leaving the virtualenv](#leaving-the-virtualenv)
6565
- [VirtualenvWrapper](#virtualenvwrapper)
6666
- [NOTE:](#note)
67+
- [Typing hints](#typing-hints)
68+
- [Pros of typing hints](#pros-of-typing-hints)
69+
- [Cons of typing hints](#cons-of-typing-hints)
70+
- [Typing: please remember](#typing-please-remember)
6771
- [Django](#django)
6872
- [Suggested project scaffolding](#suggested-project-scaffolding)
6973
- [Viewsets](#viewsets)
@@ -1324,6 +1328,57 @@ $ add2virtualenv <dir1> [dir2, dir3, ...]
13241328
for further reading you can consult the docs.
13251329
https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html
13261330

1331+
## Typing hints
1332+
1333+
Python by default is a dynamically typed language but there are also some statically typed languages, for instance, C and Java. However, [PEP 484](https://peps.python.org/pep-0484/) introduced typed hints, which make it possible to also do static type checking of Python code. Type hints by themselves don’t cause Python to enforce types. As the name says, type hints just suggest types. There are other tools, such as [Mypy](http://mypy-lang.org/), that will be discussed later in this section.
1334+
1335+
Here at Rootstrap, we suggest you use typing for functions (params and outputs). It is not necessary to use it in variable annotations (as described in [PEP 526](https://peps.python.org/pep-0526/)). We prefer to use typing as a guide for data type flow in our apps.
1336+
1337+
Now you will see a few examples of python code, this is how it will normally looks, no typing at all:
1338+
1339+
```python
1340+
import math
1341+
1342+
def calculate_circumference(radius):
1343+
return 2 * math.pi * radius
1344+
```
1345+
1346+
Now this is how a code with type comments will look like:
1347+
1348+
```python
1349+
import math
1350+
1351+
def calculate_circumference(radius):
1352+
# type: (float) -> float
1353+
return 2 * math.pi * radius
1354+
```
1355+
1356+
And finally how a typed hint code will look like:
1357+
1358+
```python
1359+
import math
1360+
1361+
def calculate_circumference(radius: float) -> float:
1362+
return 2 * math.pi * radius
1363+
```
1364+
1365+
### Pros of typing hints
1366+
1367+
* Helps you catch certain errors by improving IDEs and linters.
1368+
* Helps you document your code, changes doctstrings with types to a self documented code.
1369+
* Build and mantain a cleaner architecture. This will enforce you to think about types in your code.
1370+
* In bigger projects, type hints help you understand how types flow through your code, and are highly recommended. Even more so in projects where there are a lot of people involved.
1371+
1372+
### Cons of typing hints
1373+
1374+
* Will take developer time and effort to add it.
1375+
* Work best in modern versions of Python. Python 2.7 could use typed comments, but it was introduced in Python 3.0.
1376+
* It could introduce a slight penalty in start-up time, specially in short scripts if you need to use `import typing` module.
1377+
1378+
### Typing: please remember
1379+
1380+
This typing concepts does not mean you HAVE to use static type checking in your code, there is a concept call [Gradual typing in PEP 483](https://peps.python.org/pep-0483/#summary-of-gradual-typing). This means that you can gradually introduce types into the code. Code without type hints will be ignored by the static type checker. Therefore, you can start adding types to critical components.
1381+
13271382
# Django
13281383
To start a new Django project, we recomend to use [Django-Cookiecutter](https://github.com/pydanny/cookiecutter-django). Please go to our [guide to start a Django project](./cookiecutter-django.md) using this tool.
13291384

0 commit comments

Comments
 (0)