# Agenda
1. Discuss Python formatters.
2. Discuss Python linters.
3. MyPy improvements.
---
# Python Formatters
---
Why Use Code Styles?
---
# PEP 8
## Style Guide for Python Code
https://www.python.org/dev/peps/pep-0008/
---
## All about consistency!
## All about redability!
---
## Popular autoformatters
* [YAPF](https://github.com/google/yapf)
* [Black](https://github.com/python/black)
---
## Yapf
Yet another Python formatter is a tool which is produced and maintained by Google.
9,430 stars at Github!
---
## Black
Uncompromising Python code formatter.
11,230 stars at Github!
---
# Black
> So, what is Black? Black is a Python code formatter. But, why use it? Is it super configurable, letting you fine tune it to produce the exact code-style you want?
Nope, the opposite!
Black operates under similar principles: it removes the effort you have to invest into thinking about how to format your python code, which is now standardised, so you can then instead focus your efforts into combinations of standardised code.
by Mika Naylor @autophagy
---
## Formatting very long if condition
### YAPF
```python=
if very_long_variable_name is not None and \
very_long_variable_name.field > 0 or \
very_long_variable_name.is_debug:
z = 'hello '+'world'
else:
world = 'world'
```
### Black
```python=
if (
very_long_variable_name is not None
and very_long_variable_name.field > 0
or very_long_variable_name.is_debug
):
z = "hello " + "world"
else:
world = "world"
```
---
## Formatting function's parameters
### YAPF
```python=
logger.info(
'\nMemory usage: %s [kB]',
((sys.getsizeof(label_matrix) + sys.getsizeof(position_count_array) +
sys.getsizeof(labels)) / 1024))
```
### Black
```python=
logger.info(
"\nMemory usage: %s [kB]",
(
(
sys.getsizeof(label_matrix)
+ sys.getsizeof(position_count_array)
+ sys.getsizeof(labels)
)
/ 1024
),
)
```
---
## Formatting lists
### YAPF
```python=
regular_formatting = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3,
4, 5, 3, 4, 5
]
short_list = [0, 1, 2, 3, 4, 5, 6, 7, 8]
```
### Black
```python=
regular_formatting = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
0,
1,
2,
3,
4,
5,
3,
4,
5,
6,
7,
8,
0,
1,
2,
3,
4,
5,
3,
4,
5,
]
short_list = [0, 1, 2, 3, 4, 5, 6, 7, 8]
```
---
## Formatting long imports
### YAPF
```python=
from seven_dwwarfs import Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey, Doc,
```
### Black
```python=
from seven_dwwarfs import (
Grumpy,
Happy,
Sleepy,
Bashful,
Sneezy,
Dopey,
Doc,
)
```
---
## Formatting long function definitions
### Input
```python=
def very_important_function(template: str,*variables,file: os.PathLike,debug:bool=False):
"""Applies `variables` to the `template` and writes to `file`."""
with open(file, "w") as f:
...
```
### YAPF
```python=
def very_important_function(template: str,
*variables,
file: os.PathLike,
debug: bool = False):
"""Applies `variables` to the `template` and writes to `file`."""
with open(file, "w") as f:
...
```
### Black
```python=
def very_important_function(
template: str,
*variables,
file: os.PathLike,
debug: bool = False,
):
"""Applies `variables` to the `template` and writes to `file`."""
with open(file, "w") as f:
...
)
```
---
## Formatting text in Black
### Input
```python=
my_text = 'Hello world! I\'m user'
```
### After Blackening
```python=
my_text = "Hello world! I'm user"
```
---
# MYPY
---
## Using stubs for proto files
```python=
pip install mypy-protobuf
```
[mypy-protobuf](https://github.com/dropbox/mypy-protobuf) is an open-source plugin from Dropbox.
## Generating mypy stubs
```python=
python3 -m grpc_tools.protoc \
--proto_path=src/protobuf \
--python_out=src/protobuf/xain/out_test/ \
--grpc_python_out=src/protobuf/xain/out_test/ \
--mypy_out=src/protobuf/xain/mypy/ \ src/protobuf/xain/cproto/*.proto
```
---
{"metaMigratedAt":"2023-06-14T22:39:02.744Z","metaMigratedFrom":"Content","title":"Agenda","breaks":true,"contributors":"[{\"id\":\"96e6ec3e-1252-40c1-8e7b-42574a232bf8\",\"add\":4808,\"del\":562}]"}