Skip to content

Commit c21329b

Browse files
authored
Recommend using PEP 585 (#5055)
* Remove conventions enforced by black Remove old note about optional default arguments (now part of PEP 484 and enforced by CI) * Recommend to use PEP 585 Cf. #4820 * Try out using collections.abc * Reference mypy bug
1 parent c00c725 commit c21329b

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

CONTRIBUTING.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ you should know about.
209209

210210
Style conventions for stub files are different from PEP 8. The general
211211
rule is that they should be as concise as possible. Specifically:
212-
* lines can be up to 130 characters long;
213-
* functions and methods that don't fit in one line should be split up
214-
with one argument per line;
215212
* all function bodies should be empty;
216213
* prefer ``...`` over ``pass``;
217214
* prefer ``...`` on the same line as the class/function signature;
@@ -221,8 +218,7 @@ rule is that they should be as concise as possible. Specifically:
221218
if the classes are very small;
222219
* do not use docstrings;
223220
* use variable annotations instead of type comments, even for stubs
224-
that target older versions of Python;
225-
* for arguments with a type and a default, use spaces around the `=`.
221+
that target older versions of Python.
226222

227223
Stubs should be reformatted with the formatters
228224
[black](https://github.com/psf/black) and
@@ -241,7 +237,13 @@ checker, and leave out unnecessary detail:
241237
* use `float` instead of `Union[int, float]`.
242238

243239
Some further tips for good type hints:
244-
* avoid invariant collection types (`List`, `Dict`) in argument
240+
* use built-in generics (`list`, `dict`, `tuple`, `set`), instead
241+
of importing them from `typing`, **except** for arbitrary length tuples
242+
(`Tuple[int, ...]`) (see
243+
[python/mypy#9980](https://github.com/python/mypy/issues/9980));
244+
* in Python 3 stubs, import collections (`Mapping`, `Iterable`, etc.)
245+
from `collections.abc` instead of `typing`;
246+
* avoid invariant collection types (`list`, `dict`) in argument
245247
positions, in favor of covariant types like `Mapping` or `Sequence`;
246248
* avoid Union return types: https://github.com/python/mypy/issues/1693;
247249
* in Python 2, whenever possible, use `unicode` if that's the only
@@ -265,14 +267,6 @@ Note that `Any` is not the correct type to use if you want to indicate
265267
that some function can accept literally anything: in those cases use
266268
`object` instead.
267269

268-
For arguments with type and a default value of `None`, PEP 484
269-
prescribes that the type automatically becomes `Optional`. However we
270-
prefer explicit over implicit in this case, and require the explicit
271-
`Optional[]` around the type. The mypy tests enforce this (through
272-
the use of --no-implicit-optional) and the error looks like
273-
`Incompatible types in assignment (expression has type None, variable
274-
has type "Blah") `.
275-
276270
Stub files support forward references natively. In other words, the
277271
order of class declarations and type aliases does not matter in
278272
a stub file. You can also use the name of the class within its own

stdlib/random.pyi

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _random
22
import sys
3-
from typing import AbstractSet, Any, Callable, Iterable, List, MutableSequence, Optional, Sequence, Tuple, TypeVar, Union
3+
from collections.abc import Callable, Iterable, MutableSequence, Sequence, Set
4+
from typing import Any, Optional, Tuple, TypeVar, Union
45

56
_T = TypeVar("_T")
67

@@ -22,14 +23,14 @@ class Random(_random.Random):
2223
*,
2324
cum_weights: Optional[Sequence[float]] = ...,
2425
k: int = ...,
25-
) -> List[_T]: ...
26+
) -> list[_T]: ...
2627
def shuffle(self, x: MutableSequence[Any], random: Optional[Callable[[], float]] = ...) -> None: ...
2728
if sys.version_info >= (3, 9):
2829
def sample(
29-
self, population: Union[Sequence[_T], AbstractSet[_T]], k: int, *, counts: Optional[Iterable[_T]] = ...
30-
) -> List[_T]: ...
30+
self, population: Union[Sequence[_T], Set[_T]], k: int, *, counts: Optional[Iterable[_T]] = ...
31+
) -> list[_T]: ...
3132
else:
32-
def sample(self, population: Union[Sequence[_T], AbstractSet[_T]], k: int) -> List[_T]: ...
33+
def sample(self, population: Union[Sequence[_T], Set[_T]], k: int) -> list[_T]: ...
3334
def random(self) -> float: ...
3435
def uniform(self, a: float, b: float) -> float: ...
3536
def triangular(self, low: float = ..., high: float = ..., mode: Optional[float] = ...) -> float: ...
@@ -64,14 +65,14 @@ def choices(
6465
*,
6566
cum_weights: Optional[Sequence[float]] = ...,
6667
k: int = ...,
67-
) -> List[_T]: ...
68+
) -> list[_T]: ...
6869
def shuffle(x: MutableSequence[Any], random: Optional[Callable[[], float]] = ...) -> None: ...
6970

7071
if sys.version_info >= (3, 9):
71-
def sample(population: Union[Sequence[_T], AbstractSet[_T]], k: int, *, counts: Optional[Iterable[_T]] = ...) -> List[_T]: ...
72+
def sample(population: Union[Sequence[_T], Set[_T]], k: int, *, counts: Optional[Iterable[_T]] = ...) -> list[_T]: ...
7273

7374
else:
74-
def sample(population: Union[Sequence[_T], AbstractSet[_T]], k: int) -> List[_T]: ...
75+
def sample(population: Union[Sequence[_T], Set[_T]], k: int) -> list[_T]: ...
7576

7677
def random() -> float: ...
7778
def uniform(a: float, b: float) -> float: ...

0 commit comments

Comments
 (0)