@@ -257,8 +257,10 @@ Module names can still collide. Some module names are inconveniently long.
257
257
- ` y ` conflicts with a common parameter name that is part of the public
258
258
API (e.g., ` features ` ).
259
259
- ` y ` is an inconveniently long name.
260
- * Use ` import y as z ` only when ` z ` is a standard abbreviation (e.g., ` np ` for
261
- ` numpy ` ).
260
+ - ` y ` is too generic in the context of your code (e.g., `from
261
+ storage.file_system import options as fs_options`).
262
+ * Use ` import y as z ` only when ` z ` is a standard abbreviation (e.g., `import
263
+ numpy as np`).
262
264
263
265
For example the module ` sound.effects.echo ` may be imported as follows:
264
266
@@ -1794,8 +1796,9 @@ No: # Stuff on first line forbidden.
1794
1796
1795
1797
Trailing commas in sequences of items are recommended only when the closing
1796
1798
container token `]` , `)` , or `}` does not appear on the same line as the final
1797
- element. The presence of a trailing comma is also used as a hint to our Python
1798
- code auto- formatter
1799
+ element, as well as for tuples with a single element. The presence of a trailing
1800
+ comma is also used as a hint to our Python code auto- formatter
1801
+
1799
1802
< a id = " Python_Interpreter" >< / a>
1800
1803
< a id = " s3.7-shebang-line" >< / a>
1801
1804
< a id = " 37-shebang-line" >< / a>
@@ -1955,11 +1958,12 @@ aptly described using a one-line docstring.
1955
1958
1956
1959
<a id="doc-function-returns"></a>
1957
1960
[*Returns:* (or *Yields:* for generators)](#doc-function-returns)
1958
- : Describe the type and semantics of the return value. If the function only
1959
- returns None, this section is not required. It may also be omitted if the
1960
- docstring starts with Returns or Yields (e.g. `""" Returns row from Bigtable
1961
- as a tuple of strings.""" `) and the opening sentence is sufficient to
1962
- describe the return value. Do not imitate 'NumPy style'
1961
+ : Describe the semantics of the return value, including any type information
1962
+ that the type annotation does not provide. If the function only returns
1963
+ None, this section is not required. It may also be omitted if the docstring
1964
+ starts with Returns or Yields (e.g. `""" Returns row from Bigtable as a tuple
1965
+ of strings.""" `) and the opening sentence is sufficient to describe the
1966
+ return value. Do not imitate 'NumPy style'
1963
1967
([example](http://numpy.org/doc/stable/reference/generated/numpy.linalg.qr.html)),
1964
1968
which frequently documents a tuple return value as if it were multiple
1965
1969
return values with individual names (never mentioning the tuple). Instead,
@@ -3246,8 +3250,8 @@ def next(l: list[_T]) -> _T:
3246
3250
return l.pop()
3247
3251
3248
3252
def print_when_called(f: Callable[_P, _T]) -> Callable[_P, _T]:
3249
- def inner(* args: P .args, ** kwargs: P .kwargs) -> R :
3250
- print (' Function was called' )
3253
+ def inner(* args: _P .args, ** kwargs: _P .kwargs) -> _T :
3254
+ print (" Function was called" )
3251
3255
return f(* args, ** kwargs)
3252
3256
return inner
3253
3257
```
@@ -3352,19 +3356,6 @@ def generate_foo_scores(foo: set[str]) -> list[float]:
3352
3356
...
3353
3357
```
3354
3358
3355
- NOTE : Users of [Apache Beam](https:// github.com/ apache/ beam/ issues/ 23366 ) should
3356
- continue to import parametric containers from `typing` .
3357
-
3358
- ```python
3359
- from typing import Set, List
3360
-
3361
- # Only use this older style if you are required to by introspection
3362
- # code such as Apache Beam that has not yet been updated for PEP-585,
3363
- # or if your code needs to run on Python versions earlier than 3.9.
3364
- def generate_foo_scores(foo: Set[str ]) -> List[float ]:
3365
- ...
3366
- ```
3367
-
3368
3359
< a id = " s3.19.13-conditional-imports" >< / a>
3369
3360
< a id = " 31913-conditional-imports" >< / a>
3370
3361
@@ -3471,14 +3462,20 @@ def get_names(employee_ids: Sequence[_T]) -> Mapping[_T, str]:
3471
3462
* BE CONSISTENT * .
3472
3463
3473
3464
If you' re editing code, take a few minutes to look at the code around you and
3474
- determine its style. If they use spaces around all their arithmetic operators,
3475
- you should too. If their comments have little boxes of hash marks around them,
3476
- make your comments have little boxes of hash marks around them too.
3465
+ determine its style. If they use `_idx` suffixes in index variable names, you
3466
+ should too. If their comments have little boxes of hash marks around them, make
3467
+ your comments have little boxes of hash marks around them too.
3477
3468
3478
3469
The point of having style guidelines is to have a common vocabulary of coding so
3479
3470
people can concentrate on what you' re saying rather than on how you' re saying
3480
3471
it. We present global style rules here so people know the vocabulary, but local
3481
3472
style is also important. If code you add to a file looks drastically different
3482
3473
from the existing code around it, it throws readers out of their rhythm when
3483
- they go to read it. Avoid this.
3474
+ they go to read it.
3475
+
3476
+ However, there are limits to consistency. It applies more heavily locally and on
3477
+ choices unspecified by the global style. Consistency should not generally be
3478
+ used as a justification to do things in an old style without considering the
3479
+ benefits of the new style, or the tendency of the codebase to converge on newer
3480
+ styles over time.
3484
3481
0 commit comments