fork download
  1. """Mocking a local function in doctest, by Cees Timmerman 2025-03-22."""
  2.  
  3. import sys
  4. from typing import Callable
  5.  
  6.  
  7. def get_input() -> str:
  8. return input()
  9.  
  10.  
  11. def way_a(x: Callable[[], str] = get_input) -> None:
  12. """
  13. >>> way_a(lambda: "42")
  14. 42
  15. """
  16. print(x())
  17.  
  18.  
  19. def way_b() -> None:
  20. """
  21. >>> way_b()
  22. 42
  23. """
  24. if "doctest.py" in str(sys.modules["__main__"].__file__):
  25. get_input: Callable[[], str] = lambda: "42"
  26. print(get_input())
  27.  
  28.  
  29. if __name__ == "__main__":
  30. way_a()
  31. way_b()
Runtime error #stdin #stdout #stderr 0.94s 37028KB
stdin
asdf
stdout
asdf
stderr
Traceback (most recent call last):
  File "./prog.py", line 31, in <module>
  File "./prog.py", line 26, in way_b
UnboundLocalError: cannot access local variable 'get_input' where it is not associated with a value