blob: f1df3e79701a5b3b69c94249d51ba7d821639240 [file] [log] [blame] [view]
Andrew Grieve32bc5e0d2020-01-03 19:39:521# Chromium Python Style Guide
Peter Wendcdb2ec2018-11-26 23:00:362
3_For other languages, please see the [Chromium style
John Palmerbe051302021-05-19 11:48:354guides](https://chromium.googlesource.com/chromium/src/+/main/styleguide/styleguide.md)._
Peter Wendcdb2ec2018-11-26 23:00:365
zhoupeng6ebd49d2025-04-03 20:45:006The minimum supported Python version is 3.9, we recommend using Python 3.11
7(as in, that's what the bots use, so don't assume or require anything older
8*or* newer), but most newer versions of Python3 will work fine for most
9things. There is an appropriate version of Python3 in
10`$depot_tools/python-bin`, if you don't have one already.
Dirk Prankecc232342021-05-13 22:20:1211
12We (often) use a tool called [vpython] to manage Python packages; vpython
13is a wrapper around virtualenv. However, it is not safe to use vpython
14regardless of context, as it can have performance issues. All tests are
Bruce Dawson942f9bcd2022-08-10 18:40:2215run under vpython, so it is safe there, and vpython is the default for
16running scripts during PRESUBMIT checks (input_api.python3_executable points to
17vpython3 and is used in GetPythonUnitTests), but you should not use vpython
18during gclient runhooks, or during the build unless a
19[//build/OWNER](../../build/OWNERS) has told you that it is okay to do so.
Dirk Prankecc232342021-05-13 22:20:1220
21Also, there is some performance overhead to using vpython, so prefer not
22to use vpython unless you need it (to pick up packages not available in the
23source tree).
24
25"shebang lines" (the first line of many unix scripts, like `#!/bin/sh`)
26aren't as useful as you might think in Chromium, because
27most of our python invocations come from other tools like Ninja or
28the swarming infrastructure, and they also don't work on Windows.
Bruce Dawson942f9bcd2022-08-10 18:40:2229So, don't expect them to help you. That said, a python 3 shebang is one way to
30indicate to the presubmit system that test scripts should be run under Python 3
31rather than Python 2.
Dirk Prankecc232342021-05-13 22:20:1232
33However, if your script is executable, you should still use one, and for
David Munro9980b2c2022-02-02 15:30:4134Python you should use `#!/usr/bin/env python3` or `#!/usr/bin/env vpython3`
Dirk Prankecc232342021-05-13 22:20:1235in order to pick up the right version of Python from your $PATH rather than
36assuming you want the version in `/usr/bin`; this allows you to pick up the
37versions we endorse from
38`depot_tools`.
39
Andrew Grieve32bc5e0d2020-01-03 19:39:5240Chromium follows [PEP-8](https://www.python.org/dev/peps/pep-0008/).
Peter Wendcdb2ec2018-11-26 23:00:3641
Andrew Grieve32bc5e0d2020-01-03 19:39:5242It is also encouraged to follow advice from
43[Google's Python Style Guide](https://google.github.io/styleguide/pyguide.html),
44which is a superset of PEP-8.
45
46See also:
Brian Norris13e1fe422023-08-25 20:26:1147* [ChromiumOS Python Style Guide](https://chromium.googlesource.com/chromiumos/docs/+/HEAD/styleguide/python.md)
Andrew Grieve32bc5e0d2020-01-03 19:39:5248* [Blink Python Style Guide](blink-python.md)
49
50[TOC]
51
52## Our Previous Python Style
53
54Chromium used to differ from PEP-8 in the following ways:
55* Use two-space indentation instead of four-space indentation.
56* Use `CamelCase()` method and function names instead of `unix_hacker_style()`
57 names.
58* 80 character line limits rather than 79.
59
60New scripts should not follow these deviations, but they should be followed when
61making changes to files that follow them.
62
63## Making Style Guide Changes
Peter Wendcdb2ec2018-11-26 23:00:3664
65You can propose changes to this style guide by sending an email to
Takuto Ikuta8ba91152023-09-25 13:42:4466[`[email protected]`]. Ideally, the list will arrive at some consensus and you
Peter Wendcdb2ec2018-11-26 23:00:3667can request review for a change to this file. If there's no consensus,
John Palmerbe051302021-05-19 11:48:3568[`//styleguide/python/OWNERS`](https://chromium.googlesource.com/chromium/src/+/main/styleguide/python/OWNERS)
Peter Wendcdb2ec2018-11-26 23:00:3669get to decide.
70
Bruce Dawson942f9bcd2022-08-10 18:40:2271## Portability
72
73There are a couple of differences in how text files are handled on Windows that
74can lead to portability problems. These differences are:
75
76* The default encoding when reading/writing text files is cp1252 on Windows and
77utf-8 on Linux, which can lead to Windows-only test failures. These can be
78avoided by always specifying `encoding='utf-8'` when opening text files.
79
80* The default behavior when writing text files on Windows is to emit \r\n
81(carriage return line feed) line endings. This can lead to cryptic Windows-only
82test failures and is generally undesirable. This can be avoided by always
83specifying `newline=''` when opening text files for writing.
84
85That is, use these forms when opening text files in Python:
86
87* reading: with open(filename, 'r', encoding='utf-8') as f:
88* writing: with open(filename, 'w', encoding='utf-8', newline='') as f:
89
Peter Wendcdb2ec2018-11-26 23:00:3690## Tools
91
Andrew Grievea6dad4a2018-11-29 13:57:3292### pylint
Peter Wendcdb2ec2018-11-26 23:00:3693[Depot tools](http://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools.html)
94contains a local copy of pylint, appropriately configured.
Dirk Prankecc232342021-05-13 22:20:1295* Directories need to opt into pylint presubmit checks via:
Andrew Grievea6dad4a2018-11-29 13:57:3296 `input_api.canned_checks.RunPylint()`.
97
98### YAPF
agrieve1a1b9066b2018-11-30 15:47:2299[YAPF](https://github.com/google/yapf) is the Python formatter used by:
Andrew Grievea6dad4a2018-11-29 13:57:32100
101```sh
102git cl format --python
103```
104
agrieve1a1b9066b2018-11-30 15:47:22105Directories can opt into enforcing auto-formatting by adding a `.style.yapf`
106file with the following contents:
Andrew Grievea6dad4a2018-11-29 13:57:32107```
108[style]
Andrew Grieve83d4c6a92020-01-07 16:50:23109based_on_style = pep8
Andrew Grievea6dad4a2018-11-29 13:57:32110```
111
112Entire files can be formatted (rather than just touched lines) via:
113```sh
114git cl format --python --full
agrieve1a1b9066b2018-11-30 15:47:22115```
116
Andrew Grieve32bc5e0d2020-01-03 19:39:52117YAPF has gotchas. You should review its changes before submitting. Notably:
118 * It does not re-wrap comments.
119 * It won't insert characters in order wrap lines. You might need to add ()s
120 yourself in order to have to wrap long lines for you.
121 * It formats lists differently depending on whether or not they end with a
122 trailing comma.
123
124
agrieve1a1b9066b2018-11-30 15:47:22125#### Bugs
126* Are tracked here: https://github.com/google/yapf/issues.
Takuto Ikuta8ba91152023-09-25 13:42:44127* For Chromium-specific bugs, please discuss on [`[email protected]`].
agrieve1a1b9066b2018-11-30 15:47:22128
129#### Editor Integration
Dirk Prankecc232342021-05-13 22:20:12130See: https://github.com/google/yapf/tree/main/plugins
131
132[vpython]: https://chromium.googlesource.com/infra/infra/+/refs/heads/main/doc/users/vpython.md
Takuto Ikuta8ba91152023-09-25 13:42:44133[`[email protected]`]: https://groups.google.com/a/chromium.org/g/python