A story about trying to introduce Linter in the middle of a Python (Flask) project

topic

--The backend (Flask) code of the project you are participating in has had a hard time (still ongoing) because Linter and formatter have not been introduced.

Hope

――Do you want to lint & format with Githook? ?? ?? ?? ?? ??

problem

--The code written at the beginning of the project ignores pep8 gun and introduces lint, an error occurs. ――I want to apply autofix at once, but the difference is too large to review. ――I want to refactor the whole amount, but there is no man-hours, and the test code does not seem to be enough

compromise

--Lint & format is applied to the newly created code. --The existing code remains as it is --Review the test code of the existing code and implement it additionally so that it can be refactored at any time. --Lint is applied with hook. The format is decided by the human being (a measure of bitterness). --Existing is forced to commit with git commit --no-verify

Development method

If new

  1. Coding
  2. Lint runs at commit
  3. Apply formater black hogehoge.py
  4. If you have to fix it by hand, fix it until the error disappears and commit (lintflake8 hogehoge.py by specifying the file)
  5. Push to pull request review

If you want to get existing code

  1. Coding
  2. Lint runs at commit
  3. If there is an error, check the error in the additional part with black hogehoge.py --diff and fix it by hand. (Lintflake8 hogehoge.py by file specification)
  4. When committing, skip the hook with git commit --no-verify
  5. Push to pull request review

Linter settings

linter uses flake8.

  1. pip install flake8
  2. Create a configuration file (create .flake8 in the project root)

.flake8


[flake8]
ignore = E203, E266, E501, W503, F403, F401
max-line-length = 79
max-complexity = 18
select = B,C,E,F,W,T4,B9

* Change the flake8 settings as appropriate.

--Honestly, max-line-length <80 is too few in modern times ――E203, W503, W504 are required for igonore because of the relationship with black. Other than that, as appropriate -Look here

formatter settings

formatter uses black

  1. pip install black
  2. Create a configuration file (create pyproject.toml in the project root)

pyproject.toml


[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
    \.git
)
'''

Match with flake8

――I'm surprised when I sometimes get an error even though I use formatr.

githook settings

Use pre-commit to set the hook.

  1. pip install pre-commit
  2. Create configuration file (install .pre-commit-config.yaml in the project root)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: flake8

Summary

――Because of the introduction period of the tool, manual work is included. Change little by little --Lint and formater should be entered first. The minimum rules (pep8) should be included ――Change the rules when you find it difficult to write --If the test code is correct, there is no problem with the existing ones.

Recommended Posts

A story about trying to introduce Linter in the middle of a Python (Flask) project
A story about trying to implement a private variable in Python.
A reminder about the implementation of recommendations in Python
[Note] A story about trying to override a class method with two underscores in Python 3 series.
How to determine the existence of a selenium element in Python
How to check the memory size of a dictionary in Python
A story about trying to run multiple python versions (Mac edition)
The story of trying to reconnect the client
The story of blackjack A processing (python)
A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
I made a program to check the size of a file in Python
Get the caller of a function in Python
Make a copy of the list in Python
A note about the python version of python virtualenv
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
Output in the form of a python array
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
To output a value even in the middle of a cell with Jupyter Notebook
A story of a high school graduate technician trying to predict the survival of the Titanic
How to get a list of files in the same directory with python
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
How to get the number of digits in Python
The story of FileNotFound in Python open () mode ='w'
In the middle of development, we will introduce Alembic
A story about trying a (Golang +) Python monorepo with Bazel
A story about changing the master name of BlueZ
Tasks at the start of a new python project
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python Note: The mystery of assigning a variable to a variable
A story that got stuck when trying to upgrade the Python version on GCE
I can't find the clocksource tsc! ?? The story of trying to write a kernel patch
A story of trial and error trying to create a dynamic user group in Slack
How to check in Python if one of the elements of a list is in another list
I used Python to find out about the role choices of the 51 "Yachts" in the world.
A memo of misunderstanding when trying to load the entire self-made module with Python3
About the features of Python
Find out the apparent width of a string in python
How to use the __call__ method in a Python class
The story of introducing jedi (python auto-completion package) to emacs
Get the number of specific elements in a python list
How to develop in a virtual environment of Python [Memo]
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
A story about how to deal with the CORS problem
Find the eigenvalues of a real symmetric matrix in Python
Now in Singapore The story of creating a LineBot and wanting to do a memorable job
Use hash to lighten collision detection of about 1000 balls in Python (related to the new coronavirus)
Get the value of a specific key up to the specified index in the dictionary list in Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
What seems to be a template of the standard input part of the competition pro in python3
I tried to create a Python script to get the value of a cell in Microsoft Excel
A story about trying to use cron on a Raspberry Pi and getting stuck in space
[Django] A story about getting stuck in a swamp trying to validate a zip with form [TDD]
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
How to pass the execution result of a shell command in a list in Python (non-blocking version)