8000 Add auto-formatting section · realpython/python-guide@e438a80 · GitHub
[go: up one dir, main page]

Skip to content

Commit e438a80

Browse files
committed
Add auto-formatting section
1 parent 3525c13 commit e438a80

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

docs/writing/style.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,14 @@ Then run it on a file or series of files to get a report of any violations.
496496
optparse.py:472:29: E221 multiple spaces before operator
497497
optparse.py:544:21: W601 .has_key() is deprecated, use 'in'
498498
499+
Auto-Formatting
500+
~~~~~~~~~~~~~~~
501+
502+
There are several auto-formatting tools that can reformat your code,
503+
in order to comply with PEP 8.
504+
505+
**autopep8**
506+
499507
The program `autopep8 <https://pypi.org/project/autopep8/>`_ can be used to
500508
automatically reformat code in the PEP 8 style. Install the program with:
501509

@@ -513,6 +521,49 @@ Excluding the ``--in-place`` flag will cause the program to output the modified
513521
code directly to the console for review. The ``--aggressive`` flag will perform
514522
more substantial changes and can be applied multiple times for greater effect.
515523

524+
**yapf**
525+
526+
While autopep8 focuses on solving the PEP 8 violations, `yapf <https://github.com/google/yapf>`_
527+
tries to improve the format of your code aside from complying with PEP 8.
528+
This formatter aims at providing as good looking code as a programmer who
529+
writes PEP 8 compliant code.
530+
It gets installed with:
531+
532+
.. code-block:: console
533+
534+
$ pip install yapf
535+
536+
Run the auto-formatting of a file with:
537+
538+
.. code-block:: console
539+
540+
$ yapf --in-place optparse.py
541+
542+
Similar to autopep8, running the command without the ``--in-place`` flag will
543+
output the diff for review before applying the changes.
544+
545+
**black**
546+
547+
The auto-formatter `black <https://github.com/psf/black>`_ offers an
548+
opinionated and deterministic reformatting of your code base.
549+
Its main focus lies in providing a uniform code style without the need of
550+
configuration throughout its users. Hence, users of black are able to forget
551+
about formatting altogether. Also, due to the deterministic approach minimal
552+
git diffs with only the relevant changes are guaranteed. You can install the
553+
tool as follows:
554+
555+
.. code-block:: console
556+
557+
$ pip install black
558+
559+
A python file can be formatted with:
560+
561+
.. code-block:: console
562+
563+
$ black optparse.py
564+
565+
Adding the ``--diff`` flag provides the code modification for review without
566+
direct application.
516567

517568
***********
518569
Conventions

0 commit comments

Comments
 (0)
0