Skip to content

Commit c0e2346

Browse files
authored
new union syntax in CONTRIBUTING.md (#5879)
1 parent 1409f6e commit c0e2346

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,9 @@ Supported features include:
160160
- [PEP 586](https://www.python.org/dev/peps/pep-0586/) (Literal)
161161
- [PEP 591](https://www.python.org/dev/peps/pep-0591/) (Final/@final)
162162
- [PEP 589](https://www.python.org/dev/peps/pep-0589/) (TypedDict)
163+
- [PEP 604](https://www.python.org/dev/peps/pep-0604/) (`Foo | Bar` union syntax)
163164
- [PEP 647](https://www.python.org/dev/peps/pep-0647/) (TypeGuard):
164165
see [#5406](https://github.com/python/typeshed/issues/5406)
165-
- [PEP 604](https://www.python.org/dev/peps/pep-0604/) (Union
166-
pipe operator): see [#4819](https://github.com/python/typeshed/issues/4819)
167166

168167
Features from the `typing` module that are not present in all
169168
supported Python 3 versions must be imported from `typing_extensions`
@@ -322,9 +321,9 @@ Stub files should only contain information necessary for the type
322321
checker, and leave out unnecessary detail:
323322
* for arguments with a default, use `...` instead of the actual
324323
default;
325-
* for arguments that default to `None`, use `Optional[]` explicitly
324+
* for arguments that default to `None`, use `Foo | None` explicitly
326325
(see below for details);
327-
* use `float` instead of `Union[int, float]`.
326+
* use `float` instead of `int | float`.
328327

329328
Some further tips for good type hints:
330329
* use built-in generics (`list`, `dict`, `tuple`, `set`), instead
@@ -335,7 +334,7 @@ Some further tips for good type hints:
335334
from `collections.abc` instead of `typing`;
336335
* avoid invariant collection types (`list`, `dict`) in argument
337336
positions, in favor of covariant types like `Mapping` or `Sequence`;
338-
* avoid Union return types: https://github.com/python/mypy/issues/1693;
337+
* avoid union return types: https://github.com/python/mypy/issues/1693;
339338
* in Python 2, whenever possible, use `unicode` if that's the only
340339
possible type, and `Text` if it can be either `unicode` or `bytes`;
341340
* use platform checks like `if sys.platform == 'win32'` to denote
@@ -351,7 +350,7 @@ unless:
351350
When adding type hints, avoid using the `Any` type when possible. Reserve
352351
the use of `Any` for when:
353352
* the correct type cannot be expressed in the current type system; and
354-
* to avoid Union returns (see above).
353+
* to avoid union returns (see above).
355354

356355
Note that `Any` is not the correct type to use if you want to indicate
357356
that some function can accept literally anything: in those cases use
@@ -371,9 +370,9 @@ When adding type annotations for context manager classes, annotate
371370
the return type of `__exit__` as bool only if the context manager
372371
sometimes suppresses exceptions -- if it sometimes returns `True`
373372
at runtime. If the context manager never suppresses exceptions,
374-
have the return type be either `None` or `Optional[bool]`. If you
373+
have the return type be either `None` or `bool | None`. If you
375374
are not sure whether exceptions are suppressed or not or if the
376-
context manager is meant to be subclassed, pick `Optional[bool]`.
375+
context manager is meant to be subclassed, pick `bool | None`.
377376
See https://github.com/python/mypy/issues/7214 for more details.
378377

379378
`__enter__` methods and other methods that return instances of the

0 commit comments

Comments
 (0)