METADATA 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. Metadata-Version: 2.2
  2. Name: autopep8
  3. Version: 2.3.2
  4. Summary: A tool that automatically formats Python code to conform to the PEP 8 style guide
  5. Author-email: Hideo Hattori <hhatto.jp@gmail.com>
  6. Project-URL: Repository, https://github.com/hhatto/autopep8
  7. Keywords: automation,pep8,format,pycodestyle
  8. Classifier: Development Status :: 5 - Production/Stable
  9. Classifier: Environment :: Console
  10. Classifier: Intended Audience :: Developers
  11. Classifier: License :: OSI Approved :: MIT License
  12. Classifier: Operating System :: OS Independent
  13. Classifier: Programming Language :: Python
  14. Classifier: Programming Language :: Python :: 3
  15. Classifier: Programming Language :: Python :: 3.9
  16. Classifier: Programming Language :: Python :: 3.10
  17. Classifier: Programming Language :: Python :: 3.11
  18. Classifier: Programming Language :: Python :: 3.12
  19. Classifier: Programming Language :: Python :: 3.13
  20. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  21. Classifier: Topic :: Software Development :: Quality Assurance
  22. Requires-Python: >=3.9
  23. Description-Content-Type: text/x-rst
  24. License-File: LICENSE
  25. License-File: AUTHORS.rst
  26. Requires-Dist: pycodestyle>=2.12.0
  27. Requires-Dist: tomli; python_version < "3.11"
  28. ========
  29. autopep8
  30. ========
  31. .. image:: https://img.shields.io/pypi/v/autopep8.svg
  32. :target: https://pypi.org/project/autopep8/
  33. :alt: PyPI Version
  34. .. image:: https://github.com/hhatto/autopep8/workflows/Python%20package/badge.svg
  35. :target: https://github.com/hhatto/autopep8/actions
  36. :alt: Build status
  37. .. image:: https://codecov.io/gh/hhatto/autopep8/branch/main/graph/badge.svg
  38. :target: https://codecov.io/gh/hhatto/autopep8
  39. :alt: Code Coverage
  40. autopep8 automatically formats Python code to conform to the `PEP 8`_ style
  41. guide. It uses the pycodestyle_ utility to determine what parts of the code
  42. needs to be formatted. autopep8 is capable of fixing most of the formatting
  43. issues_ that can be reported by pycodestyle.
  44. .. _PEP 8: https://www.python.org/dev/peps/pep-0008/
  45. .. _issues: https://pycodestyle.readthedocs.org/en/latest/intro.html#error-codes
  46. .. contents::
  47. Installation
  48. ============
  49. From pip::
  50. $ pip install --upgrade autopep8
  51. Consider using the ``--user`` option_.
  52. .. _option: https://pip.pypa.io/en/latest/user_guide/#user-installs
  53. Requirements
  54. ============
  55. autopep8 requires pycodestyle_.
  56. .. _pycodestyle: https://github.com/PyCQA/pycodestyle
  57. Usage
  58. =====
  59. To modify a file in place (with aggressive level 2)::
  60. $ autopep8 --in-place --aggressive --aggressive <filename>
  61. Before running autopep8.
  62. .. code-block:: python
  63. import math, sys;
  64. def example1():
  65. ####This is a long comment. This should be wrapped to fit within 72 characters.
  66. some_tuple=( 1,2, 3,'a' );
  67. some_variable={'long':'Long code lines should be wrapped within 79 characters.',
  68. 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
  69. 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
  70. 20,300,40000,500000000,60000000000000000]}}
  71. return (some_tuple, some_variable)
  72. def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
  73. class Example3( object ):
  74. def __init__ ( self, bar ):
  75. #Comments should have a space after the hash.
  76. if bar : bar+=1; bar=bar* bar ; return bar
  77. else:
  78. some_string = """
  79. Indentation in multiline strings should not be touched.
  80. Only actual code should be reindented.
  81. """
  82. return (sys.path, some_string)
  83. After running autopep8.
  84. .. code-block:: python
  85. import math
  86. import sys
  87. def example1():
  88. # This is a long comment. This should be wrapped to fit within 72
  89. # characters.
  90. some_tuple = (1, 2, 3, 'a')
  91. some_variable = {
  92. 'long': 'Long code lines should be wrapped within 79 characters.',
  93. 'other': [
  94. math.pi,
  95. 100,
  96. 200,
  97. 300,
  98. 9876543210,
  99. 'This is a long string that goes on'],
  100. 'more': {
  101. 'inner': 'This whole logical line should be wrapped.',
  102. some_tuple: [
  103. 1,
  104. 20,
  105. 300,
  106. 40000,
  107. 500000000,
  108. 60000000000000000]}}
  109. return (some_tuple, some_variable)
  110. def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}
  111. class Example3(object):
  112. def __init__(self, bar):
  113. # Comments should have a space after the hash.
  114. if bar:
  115. bar += 1
  116. bar = bar * bar
  117. return bar
  118. else:
  119. some_string = """
  120. Indentation in multiline strings should not be touched.
  121. Only actual code should be reindented.
  122. """
  123. return (sys.path, some_string)
  124. Options::
  125. usage: autopep8 [-h] [--version] [-v] [-d] [-i] [--global-config filename]
  126. [--ignore-local-config] [-r] [-j n] [-p n] [-a]
  127. [--experimental] [--exclude globs] [--list-fixes]
  128. [--ignore errors] [--select errors] [--max-line-length n]
  129. [--line-range line line] [--hang-closing] [--exit-code]
  130. [files [files ...]]
  131. Automatically formats Python code to conform to the PEP 8 style guide.
  132. positional arguments:
  133. files files to format or '-' for standard in
  134. optional arguments:
  135. -h, --help show this help message and exit
  136. --version show program's version number and exit
  137. -v, --verbose print verbose messages; multiple -v result in more
  138. verbose messages
  139. -d, --diff print the diff for the fixed source
  140. -i, --in-place make changes to files in place
  141. --global-config filename
  142. path to a global pep8 config file; if this file does
  143. not exist then this is ignored (default:
  144. ~/.config/pep8)
  145. --ignore-local-config
  146. don't look for and apply local config files; if not
  147. passed, defaults are updated with any config files in
  148. the project's root directory
  149. -r, --recursive run recursively over directories; must be used with
  150. --in-place or --diff
  151. -j n, --jobs n number of parallel jobs; match CPU count if value is
  152. less than 1
  153. -p n, --pep8-passes n
  154. maximum number of additional pep8 passes (default:
  155. infinite)
  156. -a, --aggressive enable non-whitespace changes; multiple -a result in
  157. more aggressive changes
  158. --experimental enable experimental fixes
  159. --exclude globs exclude file/directory names that match these comma-
  160. separated globs
  161. --list-fixes list codes for fixes; used by --ignore and --select
  162. --ignore errors do not fix these errors/warnings (default:
  163. E226,E24,W50,W690)
  164. --select errors fix only these errors/warnings (e.g. E4,W)
  165. --max-line-length n set maximum allowed line length (default: 79)
  166. --line-range line line, --range line line
  167. only fix errors found within this inclusive range of
  168. line numbers (e.g. 1 99); line numbers are indexed at
  169. 1
  170. --hang-closing hang-closing option passed to pycodestyle
  171. --exit-code change to behavior of exit code. default behavior of
  172. return value, 0 is no differences, 1 is error exit.
  173. return 2 when add this option. 2 is exists
  174. differences.
  175. Features
  176. ========
  177. autopep8 fixes the following issues_ reported by pycodestyle_::
  178. E101 - Reindent all lines.
  179. E11 - Fix indentation.
  180. E121 - Fix indentation to be a multiple of four.
  181. E122 - Add absent indentation for hanging indentation.
  182. E123 - Align closing bracket to match opening bracket.
  183. E124 - Align closing bracket to match visual indentation.
  184. E125 - Indent to distinguish line from next logical line.
  185. E126 - Fix over-indented hanging indentation.
  186. E127 - Fix visual indentation.
  187. E128 - Fix visual indentation.
  188. E129 - Fix visual indentation.
  189. E131 - Fix hanging indent for unaligned continuation line.
  190. E133 - Fix missing indentation for closing bracket.
  191. E20 - Remove extraneous whitespace.
  192. E211 - Remove extraneous whitespace.
  193. E22 - Fix extraneous whitespace around keywords.
  194. E224 - Remove extraneous whitespace around operator.
  195. E225 - Fix missing whitespace around operator.
  196. E226 - Fix missing whitespace around arithmetic operator.
  197. E227 - Fix missing whitespace around bitwise/shift operator.
  198. E228 - Fix missing whitespace around modulo operator.
  199. E231 - Add missing whitespace.
  200. E241 - Fix extraneous whitespace around keywords.
  201. E242 - Remove extraneous whitespace around operator.
  202. E251 - Remove whitespace around parameter '=' sign.
  203. E252 - Missing whitespace around parameter equals.
  204. E26 - Fix spacing after comment hash for inline comments.
  205. E265 - Fix spacing after comment hash for block comments.
  206. E266 - Fix too many leading '#' for block comments.
  207. E27 - Fix extraneous whitespace around keywords.
  208. E301 - Add missing blank line.
  209. E302 - Add missing 2 blank lines.
  210. E303 - Remove extra blank lines.
  211. E304 - Remove blank line following function decorator.
  212. E305 - Expected 2 blank lines after end of function or class.
  213. E306 - Expected 1 blank line before a nested definition.
  214. E401 - Put imports on separate lines.
  215. E402 - Fix module level import not at top of file
  216. E501 - Try to make lines fit within --max-line-length characters.
  217. E502 - Remove extraneous escape of newline.
  218. E701 - Put colon-separated compound statement on separate lines.
  219. E70 - Put semicolon-separated compound statement on separate lines.
  220. E711 - Fix comparison with None.
  221. E712 - Fix comparison with boolean.
  222. E713 - Use 'not in' for test for membership.
  223. E714 - Use 'is not' test for object identity.
  224. E721 - Use "isinstance()" instead of comparing types directly.
  225. E722 - Fix bare except.
  226. E731 - Use a def when use do not assign a lambda expression.
  227. W291 - Remove trailing whitespace.
  228. W292 - Add a single newline at the end of the file.
  229. W293 - Remove trailing whitespace on blank line.
  230. W391 - Remove trailing blank lines.
  231. W503 - Fix line break before binary operator.
  232. W504 - Fix line break after binary operator.
  233. W605 - Fix invalid escape sequence 'x'.
  234. autopep8 also fixes some issues not found by pycodestyle_.
  235. - Normalize files with mixed line endings.
  236. - Put a blank line between a class docstring and its first method
  237. declaration. (Enabled with ``E301``.)
  238. - Remove blank lines between a function declaration and its docstring. (Enabled
  239. with ``E303``.)
  240. autopep8 avoids fixing some issues found by pycodestyle_.
  241. - ``E112``/``E113`` for non comments are reports of bad indentation that break
  242. syntax rules. These should not be modified at all.
  243. - ``E265``, which refers to spacing after comment hash, is ignored if the
  244. comment looks like code. autopep8 avoids modifying these since they are not
  245. real comments. If you really want to get rid of the pycodestyle_ warning,
  246. consider just removing the commented-out code. (This can be automated via
  247. eradicate_.)
  248. .. _eradicate: https://github.com/myint/eradicate
  249. More advanced usage
  250. ===================
  251. By default autopep8 only makes whitespace changes. Thus, by default, it does
  252. not fix ``E711`` and ``E712``. (Changing ``x == None`` to ``x is None`` may
  253. change the meaning of the program if ``x`` has its ``__eq__`` method
  254. overridden.) Nor does it correct deprecated code ``W6``. To enable these
  255. more aggressive fixes, use the ``--aggressive`` option::
  256. $ autopep8 --aggressive <filename>
  257. Use multiple ``--aggressive`` to increase the aggressiveness level. For
  258. example, ``E712`` requires aggressiveness level 2 (since ``x == True`` could be
  259. changed to either ``x`` or ``x is True``, but autopep8 chooses the former).
  260. ``--aggressive`` will also shorten lines more aggressively. It will also remove
  261. trailing whitespace more aggressively. (Usually, we don't touch trailing
  262. whitespace in docstrings and other multiline strings. And to do even more
  263. aggressive changes to docstrings, use docformatter_.)
  264. .. _docformatter: https://github.com/myint/docformatter
  265. To enable only a subset of the fixes, use the ``--select`` option. For example,
  266. to fix various types of indentation issues::
  267. $ autopep8 --select=E1,W1 <filename>
  268. If the file being fixed is large, you may want to enable verbose progress
  269. messages::
  270. $ autopep8 -v <filename>
  271. Passing in ``--experimental`` enables the following functionality:
  272. - Shortens code lines by taking its length into account
  273. ::
  274. $ autopep8 --experimental <filename>
  275. Disabling line-by-line
  276. ----------------------
  277. It is possible to disable autopep8 until it is turned back on again in the file,
  278. using ``autopep8: off`` and then reenabling with ``autopep8: on``.
  279. .. code-block:: python
  280. # autopep8: off
  281. [
  282. [23, 23, 13, 43],
  283. [32, 34, 34, 34],
  284. [56, 34, 34, 11],
  285. [10, 10, 10, 10],
  286. ]
  287. # autopep8: on
  288. ``fmt: off`` and ``fmt: on`` are also valid.
  289. Use as a module
  290. ===============
  291. The simplest way of using autopep8 as a module is via the ``fix_code()``
  292. function:
  293. >>> import autopep8
  294. >>> autopep8.fix_code('x= 123\n')
  295. 'x = 123\n'
  296. Or with options:
  297. >>> import autopep8
  298. >>> autopep8.fix_code('print( 123 )\n',
  299. ... options={'ignore': ['E']})
  300. 'print( 123 )\n'
  301. Configuration
  302. =============
  303. By default, if ``$HOME/.config/pycodestyle`` (``~\.pycodestyle`` in Windows
  304. environment) exists, it will be used as global configuration file.
  305. Alternatively, you can specify the global configuration file with the
  306. ``--global-config`` option.
  307. Also, if ``setup.cfg``, ``tox.ini``, ``.pep8`` and ``.flake8`` files exist
  308. in the directory where the target file exists, it will be used as the
  309. configuration file.
  310. ``pep8``, ``pycodestyle``, and ``flake8`` can be used as a section.
  311. configuration file example::
  312. [pycodestyle]
  313. max_line_length = 120
  314. ignore = E501
  315. pyproject.toml
  316. --------------
  317. autopep8 can also use ``pyproject.toml``.
  318. The section must be ``[tool.autopep8]``, and ``pyproject.toml`` takes precedence
  319. over any other configuration files.
  320. configuration file example::
  321. [tool.autopep8]
  322. max_line_length = 120
  323. ignore = "E501,W6" # or ["E501", "W6"]
  324. in-place = true
  325. recursive = true
  326. aggressive = 3
  327. Usage with pre-commit
  328. =====================
  329. autopep8 can be used as a hook for pre-commit_.
  330. To add autopep8 as a plugin, add this repo definition to your configuration:
  331. .. code-block:: yaml
  332. repos:
  333. - repo: https://github.com/hhatto/autopep8
  334. rev: ... # select the tag or revision you want, or run `pre-commit autoupdate`
  335. hooks:
  336. - id: autopep8
  337. .. _`pre-commit`: https://pre-commit.com
  338. Testing
  339. =======
  340. Test cases are in ``test/test_autopep8.py``. They can be run directly via
  341. ``python test/test_autopep8.py`` or via tox_. The latter is useful for
  342. testing against multiple Python interpreters. (We currently test against
  343. CPython versions 3.9, 3.10, 3.11, 3.12 and 3.13. We also test against PyPy.)
  344. .. _`tox`: https://pypi.org/project/tox/
  345. Broad spectrum testing is available via ``test/acid.py``. This script runs
  346. autopep8 against Python code and checks for correctness and completeness of the
  347. code fixes. It can check that the bytecode remains identical.
  348. ``test/acid_pypi.py`` makes use of ``acid.py`` to test against the latest
  349. released packages on PyPI.
  350. Troubleshooting
  351. ===============
  352. ``pkg_resources.DistributionNotFound``
  353. --------------------------------------
  354. If you are using an ancient version of ``setuptools``, you might encounter
  355. ``pkg_resources.DistributionNotFound`` when trying to run ``autopep8``. Try
  356. upgrading ``setuptools`` to workaround this ``setuptools`` problem::
  357. $ pip install --upgrade setuptools
  358. Use ``sudo`` if you are installing to the system.
  359. Links
  360. =====
  361. * PyPI_
  362. * GitHub_
  363. * Codecov_
  364. .. _PyPI: https://pypi.org/project/autopep8/
  365. .. _GitHub: https://github.com/hhatto/autopep8
  366. .. _`Codecov`: https://app.codecov.io/gh/hhatto/autopep8