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 |
David Sanders | a9544a2 | 2022-03-12 00:59:08 | [diff] [blame] | 8 | [//docs/python3_migration.md](../../docs/python3_migration.md) for more on |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 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 |
Bruce Dawson | 942f9bcd | 2022-08-10 18:40:22 | [diff] [blame] | 17 | run under vpython, so it is safe there, and vpython is the default for |
| 18 | running scripts during PRESUBMIT checks (input_api.python3_executable points to |
| 19 | vpython3 and is used in GetPythonUnitTests), but you should not use vpython |
| 20 | during gclient runhooks, or during the build unless a |
| 21 | [//build/OWNER](../../build/OWNERS) has told you that it is okay to do so. |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 22 | |
| 23 | Also, there is some performance overhead to using vpython, so prefer not |
| 24 | to use vpython unless you need it (to pick up packages not available in the |
| 25 | source tree). |
| 26 | |
| 27 | "shebang lines" (the first line of many unix scripts, like `#!/bin/sh`) |
| 28 | aren't as useful as you might think in Chromium, because |
| 29 | most of our python invocations come from other tools like Ninja or |
| 30 | the swarming infrastructure, and they also don't work on Windows. |
Bruce Dawson | 942f9bcd | 2022-08-10 18:40:22 | [diff] [blame] | 31 | So, don't expect them to help you. That said, a python 3 shebang is one way to |
| 32 | indicate to the presubmit system that test scripts should be run under Python 3 |
| 33 | rather than Python 2. |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 34 | |
| 35 | However, if your script is executable, you should still use one, and for |
David Munro | 9980b2c | 2022-02-02 15:30:41 | [diff] [blame] | 36 | Python you should use `#!/usr/bin/env python3` or `#!/usr/bin/env vpython3` |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 37 | in order to pick up the right version of Python from your $PATH rather than |
| 38 | assuming you want the version in `/usr/bin`; this allows you to pick up the |
| 39 | versions we endorse from |
| 40 | `depot_tools`. |
| 41 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 42 | Chromium follows [PEP-8](https://www.python.org/dev/peps/pep-0008/). |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 43 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 44 | It is also encouraged to follow advice from |
| 45 | [Google's Python Style Guide](https://google.github.io/styleguide/pyguide.html), |
| 46 | which is a superset of PEP-8. |
| 47 | |
| 48 | See also: |
Brian Norris | 13e1fe42 | 2023-08-25 20:26:11 | [diff] [blame^] | 49 | * [ChromiumOS Python Style Guide](https://chromium.googlesource.com/chromiumos/docs/+/HEAD/styleguide/python.md) |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 50 | * [Blink Python Style Guide](blink-python.md) |
| 51 | |
| 52 | [TOC] |
| 53 | |
| 54 | ## Our Previous Python Style |
| 55 | |
| 56 | Chromium used to differ from PEP-8 in the following ways: |
| 57 | * Use two-space indentation instead of four-space indentation. |
| 58 | * Use `CamelCase()` method and function names instead of `unix_hacker_style()` |
| 59 | names. |
| 60 | * 80 character line limits rather than 79. |
| 61 | |
| 62 | New scripts should not follow these deviations, but they should be followed when |
| 63 | making changes to files that follow them. |
| 64 | |
| 65 | ## Making Style Guide Changes |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 66 | |
| 67 | You can propose changes to this style guide by sending an email to |
| 68 | `[email protected]`. Ideally, the list will arrive at some consensus and you |
| 69 | 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] | 70 | [`//styleguide/python/OWNERS`](https://chromium.googlesource.com/chromium/src/+/main/styleguide/python/OWNERS) |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 71 | get to decide. |
| 72 | |
Bruce Dawson | 942f9bcd | 2022-08-10 18:40:22 | [diff] [blame] | 73 | ## Portability |
| 74 | |
| 75 | There are a couple of differences in how text files are handled on Windows that |
| 76 | can lead to portability problems. These differences are: |
| 77 | |
| 78 | * The default encoding when reading/writing text files is cp1252 on Windows and |
| 79 | utf-8 on Linux, which can lead to Windows-only test failures. These can be |
| 80 | avoided by always specifying `encoding='utf-8'` when opening text files. |
| 81 | |
| 82 | * The default behavior when writing text files on Windows is to emit \r\n |
| 83 | (carriage return line feed) line endings. This can lead to cryptic Windows-only |
| 84 | test failures and is generally undesirable. This can be avoided by always |
| 85 | specifying `newline=''` when opening text files for writing. |
| 86 | |
| 87 | That is, use these forms when opening text files in Python: |
| 88 | |
| 89 | * reading: with open(filename, 'r', encoding='utf-8') as f: |
| 90 | * writing: with open(filename, 'w', encoding='utf-8', newline='') as f: |
| 91 | |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 92 | ## Tools |
| 93 | |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 94 | ### pylint |
Peter Wen | dcdb2ec | 2018-11-26 23:00:36 | [diff] [blame] | 95 | [Depot tools](http://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools.html) |
| 96 | contains a local copy of pylint, appropriately configured. |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 97 | * Directories need to opt into pylint presubmit checks via: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 98 | `input_api.canned_checks.RunPylint()`. |
| 99 | |
| 100 | ### YAPF |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 101 | [YAPF](https://github.com/google/yapf) is the Python formatter used by: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 102 | |
| 103 | ```sh |
| 104 | git cl format --python |
| 105 | ``` |
| 106 | |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 107 | Directories can opt into enforcing auto-formatting by adding a `.style.yapf` |
| 108 | file with the following contents: |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 109 | ``` |
| 110 | [style] |
Andrew Grieve | 83d4c6a9 | 2020-01-07 16:50:23 | [diff] [blame] | 111 | based_on_style = pep8 |
Andrew Grieve | a6dad4a | 2018-11-29 13:57:32 | [diff] [blame] | 112 | ``` |
| 113 | |
| 114 | Entire files can be formatted (rather than just touched lines) via: |
| 115 | ```sh |
| 116 | git cl format --python --full |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 117 | ``` |
| 118 | |
Andrew Grieve | 32bc5e0d | 2020-01-03 19:39:52 | [diff] [blame] | 119 | YAPF has gotchas. You should review its changes before submitting. Notably: |
| 120 | * It does not re-wrap comments. |
| 121 | * It won't insert characters in order wrap lines. You might need to add ()s |
| 122 | yourself in order to have to wrap long lines for you. |
| 123 | * It formats lists differently depending on whether or not they end with a |
| 124 | trailing comma. |
| 125 | |
| 126 | |
agrieve | 1a1b9066b | 2018-11-30 15:47:22 | [diff] [blame] | 127 | #### Bugs |
| 128 | * Are tracked here: https://github.com/google/yapf/issues. |
| 129 | * For Chromium-specific bugs, please discuss on `[email protected]`. |
| 130 | |
| 131 | #### Editor Integration |
Dirk Pranke | cc23234 | 2021-05-13 22:20:12 | [diff] [blame] | 132 | See: https://github.com/google/yapf/tree/main/plugins |
| 133 | |
| 134 | [vpython]: https://chromium.googlesource.com/infra/infra/+/refs/heads/main/doc/users/vpython.md |