@@ -160,10 +160,9 @@ Supported features include:
160
160
- [ PEP 586] ( https://www.python.org/dev/peps/pep-0586/ ) (Literal)
161
161
- [ PEP 591] ( https://www.python.org/dev/peps/pep-0591/ ) (Final/@final )
162
162
- [ 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)
163
164
- [ PEP 647] ( https://www.python.org/dev/peps/pep-0647/ ) (TypeGuard):
164
165
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 )
167
166
168
167
Features from the ` typing ` module that are not present in all
169
168
supported Python 3 versions must be imported from ` typing_extensions `
@@ -322,9 +321,9 @@ Stub files should only contain information necessary for the type
322
321
checker, and leave out unnecessary detail:
323
322
* for arguments with a default, use ` ... ` instead of the actual
324
323
default;
325
- * for arguments that default to ` None ` , use ` Optional[] ` explicitly
324
+ * for arguments that default to ` None ` , use ` Foo | None ` explicitly
326
325
(see below for details);
327
- * use ` float ` instead of ` Union[ int, float] ` .
326
+ * use ` float ` instead of ` int | float ` .
328
327
329
328
Some further tips for good type hints:
330
329
* use built-in generics (` list ` , ` dict ` , ` tuple ` , ` set ` ), instead
@@ -335,7 +334,7 @@ Some further tips for good type hints:
335
334
from ` collections.abc ` instead of ` typing ` ;
336
335
* avoid invariant collection types (` list ` , ` dict ` ) in argument
337
336
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 ;
339
338
* in Python 2, whenever possible, use ` unicode ` if that's the only
340
339
possible type, and ` Text ` if it can be either ` unicode ` or ` bytes ` ;
341
340
* use platform checks like ` if sys.platform == 'win32' ` to denote
@@ -351,7 +350,7 @@ unless:
351
350
When adding type hints, avoid using the ` Any ` type when possible. Reserve
352
351
the use of ` Any ` for when:
353
352
* 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).
355
354
356
355
Note that ` Any ` is not the correct type to use if you want to indicate
357
356
that some function can accept literally anything: in those cases use
@@ -371,9 +370,9 @@ When adding type annotations for context manager classes, annotate
371
370
the return type of ` __exit__ ` as bool only if the context manager
372
371
sometimes suppresses exceptions -- if it sometimes returns ` True `
373
372
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
375
374
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 ` .
377
376
See https://github.com/python/mypy/issues/7214 for more details.
378
377
379
378
` __enter__ ` methods and other methods that return instances of the
0 commit comments