Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 1 | # Chromium Python Style Guide |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 2 | |
| 3 | _For other languages, please see the [Chromium style |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 4 | guides](https://chromium.googlesource.com/chromium/src/+/main/styleguide/styleguide.md)._ |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 5 | |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 6 | As of 2021-05-12, Chromium is transitioning from Python 2 to Python 3 (follow |
| 7 | [crbug.com/941669](https://crbug.com/941669) for updates). See |
| 8 | [//docs/python3_migration.md](../docs/python3_migration.md) for more on |
| 9 | how to migrate code. |
| 10 | |
| 11 | For new (Python 3) code, you can assume Python 3.8 (and that's what the bots |
| 12 | will use), but we are increasingly seeing people running 3.9 locally as well. |
| 13 | |
| 14 | We (often) use a tool called [vpython] to manage Python packages; vpython |
| 15 | is a wrapper around virtualenv. However, it is not safe to use vpython |
| 16 | regardless of context, as it can have performance issues. All tests are |
| 17 | run under vpython, so it is safe there, but you should not use vpython |
| 18 | during PRESUBMIT checks, gclient runhooks, or during the build unless |
| 19 | a [//build/OWNER](../../build/OWNERS) has told you that it is okay to do so. |
| 20 | |
| 21 | Also, there is some performance overhead to using vpython, so prefer not |
| 22 | to use vpython unless you need it (to pick up packages not available in the |
| 23 | source tree). |
| 24 | |
| 25 | "shebang lines" (the first line of many unix scripts, like `#!/bin/sh`) |
| 26 | aren't as useful as you might think in Chromium, because |
| 27 | most of our python invocations come from other tools like Ninja or |
| 28 | the swarming infrastructure, and they also don't work on Windows. |
| 29 | So, don't expect them to help you. |
| 30 | |
| 31 | However, if your script is executable, you should still use one, and for |
David Munro | 9980b2c | 2022-02-02 15:30:41 | [diff] [blame^] | 32 | Python you should use `#!/usr/bin/env python3` or `#!/usr/bin/env vpython3` |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 33 | in order to pick up the right version of Python from your $PATH rather than |
| 34 | assuming you want the version in `/usr/bin`; this allows you to pick up the |
| 35 | versions we endorse from |
| 36 | `depot_tools`. |
| 37 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 38 | Chromium follows [PEP-8](https://www.python.org/dev/peps/pep-0008/). |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 39 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 40 | It is also encouraged to follow advice from |
| 41 | [Google's Python Style Guide](https://google.github.io/styleguide/pyguide.html), |
| 42 | which is a superset of PEP-8. |
| 43 | |
| 44 | See also: |
| 45 | * [Chromium OS Python Style Guide](https://sites.google.com/a/chromium.org/dev/chromium-os/python-style-guidelines) |
| 46 | * [Blink Python Style Guide](blink-python.md) |
| 47 | |
| 48 | [TOC] |
| 49 | |
| 50 | ## Our Previous Python Style |
| 51 | |
| 52 | Chromium used to differ from PEP-8 in the following ways: |
| 53 | * Use two-space indentation instead of four-space indentation. |
| 54 | * Use `CamelCase()` method and function names instead of `unix_hacker_style()` |
| 55 | names. |
| 56 | * 80 character line limits rather than 79. |
| 57 | |
| 58 | New scripts should not follow these deviations, but they should be followed when |
| 59 | making changes to files that follow them. |
| 60 | |
| 61 | ## Making Style Guide Changes |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 62 | |
| 63 | You can propose changes to this style guide by sending an email to |
| 64 | `[email protected]`. Ideally, the list will arrive at some consensus and you |
| 65 | can request review for a change to this file. If there's no consensus, |
John Palmer | be05130 | 2021-05-19 11:48:35 | [diff] [blame] | 66 | [`//styleguide/python/OWNERS`](https://chromium.googlesource.com/chromium/src/+/main/styleguide/python/OWNERS) |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 67 | get to decide. |
| 68 | |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 69 | ## Tools |
| 70 | |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 71 | ### pylint |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 72 | [Depot tools](http://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools.html) |
| 73 | contains a local copy of pylint, appropriately configured. |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 74 | * Directories need to opt into pylint presubmit checks via: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 75 | `input_api.canned_checks.RunPylint()`. |
| 76 | |
| 77 | ### YAPF |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 78 | [YAPF](https://github.com/google/yapf) is the Python formatter used by: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 79 | |
| 80 | ```sh |
| 81 | git cl format --python |
| 82 | ``` |
| 83 | |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 84 | Directories can opt into enforcing auto-formatting by adding a `.style.yapf` |
| 85 | file with the following contents: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 86 | ``` |
| 87 | [style] |
Andrew Grieve | 83d4c6a9 | 2020-01-07 16:50:23 | [diff] [blame] | 88 | based_on_style = pep8 |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 89 | ``` |
| 90 | |
| 91 | Entire files can be formatted (rather than just touched lines) via: |
| 92 | ```sh |
| 93 | git cl format --python --full |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 94 | ``` |
| 95 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 96 | YAPF has gotchas. You should review its changes before submitting. Notably: |
| 97 | * It does not re-wrap comments. |
| 98 | * It won't insert characters in order wrap lines. You might need to add ()s |
| 99 | yourself in order to have to wrap long lines for you. |
| 100 | * It formats lists differently depending on whether or not they end with a |
| 101 | trailing comma. |
| 102 | |
| 103 | |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 104 | #### Bugs |
| 105 | * Are tracked here: https://github.com/google/yapf/issues. |
| 106 | * For Chromium-specific bugs, please discuss on `[email protected]`. |
| 107 | |
| 108 | #### Editor Integration |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 109 | See: https://github.com/google/yapf/tree/main/plugins |
| 110 | |
| 111 | [vpython]: https://chromium.googlesource.com/infra/infra/+/refs/heads/main/doc/users/vpython.md |