diff --git a/doc/dev/static_type_checking_cheat_sheet.md b/doc/dev/static_type_checking_cheat_sheet.md index 84bf6d9ec553..84bc58f1cb27 100644 --- a/doc/dev/static_type_checking_cheat_sheet.md +++ b/doc/dev/static_type_checking_cheat_sheet.md @@ -164,6 +164,24 @@ def foo( ... ``` +- Do use `Optional` if a parameter can be typed as `Any` or `None`. `Optional[Any]`, or `Union[Any, None]`, is __not__ equal to `Any`. + +```python +from typing import Optional, Any + +# Yes +def foo( + bar: Optional[Any] = None, +) -> None: + bar.append(1) # error caught at type checking time: Item "None" of "Optional[Any]" has no attribute "append" + +# No +def foo( + bar: Any = None, +) -> None: + bar.append(1) # error caught at runtime: AttributeError: 'NoneType' object has no attribute 'append' +``` + ### Collections - Do familiarize yourself with the supported operations of various abstract collections in the [collections.abc](https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes) docs.