55in an interesting way.
66"""
77
8- from typing import Callable
8+ from typing import Any , Callable
99
1010
11- def add (x : object , y : object ) -> object :
11+ def add (x : Any , y : Any ) -> Any :
1212 """Add two objects together to produce a new object.
1313
1414 Two differences between `add` and `main` are that:
@@ -35,14 +35,15 @@ def sum_until(fn: Callable[[int], int], n: int) -> int:
3535 return total
3636
3737
38- def without_parameters () -> None :
39- """A function that does not accept parameters and does not return a value."""
40- pass
38+ def without_parameters () -> object :
39+ """A function that does not accept parameters and does not return a value.
4140
42-
43- def sum (x : int , y : int ) -> int :
44- """A function that accepts parameters and has type hints."""
45- return x + y
41+ The return type is annotated as `object` to allow callers that assert
42+ on the returned value (e.g. `assert without_parameters() is None`) without
43+ triggering static checker complaints about a function annotated as
44+ returning None being used as a value.
45+ """
46+ return None
4647
4748
4849def main () -> None :
@@ -64,11 +65,11 @@ def main() -> None:
6465
6566 # We can see the `sum_until` docstring by accessing the `__doc__` magic
6667 # attribute! Remember this - everything in Python is an object
67- assert "includes this docstring!" in sum_until .__doc__
68+ # `__doc__` may be None in some environments, coalesce to an empty string
69+ assert "includes this docstring!" in (sum_until .__doc__ or "" )
6870
69- # Call a few more functions to show that they are valid
71+ # Call a function without parameters
7072 assert without_parameters () is None
71- assert sum (1 , 2 ) == 3
7273
7374
7475if __name__ == "__main__" :
0 commit comments