Using stub file syntax at runtime

You may also occasionally need to elide actual logic in regularPython code – for example, when writing methods inoverload variants orcustom protocols.

The recommended style is to use ellipses to do so, just like instub files. It is also considered stylistically acceptable tothrow a NotImplementedError in cases where the user of thecode may accidentally call functions with no actual logic.

You can also elide default arguments as long as the function bodyalso contains no runtime logic: the function body only containsa single ellipsis, the pass statement, or a raise NotImplementedError().It is also acceptable for the function body to contain a docstring.For example:

  1. from typing import List
  2. from typing_extensions import Protocol
  3.  
  4. class Resource(Protocol):
  5. def ok_1(self, foo: List[str] = ...) -> None: ...
  6.  
  7. def ok_2(self, foo: List[str] = ...) -> None:
  8. raise NotImplementedError()
  9.  
  10. def ok_3(self, foo: List[str] = ...) -> None:
  11. """Some docstring"""
  12. pass
  13.  
  14. # Error: Incompatible default for argument "foo" (default has
  15. # type "ellipsis", argument has type "List[str]")
  16. def not_ok(self, foo: List[str] = ...) -> None:
  17. print(foo)

Note

Ellipsis expressions are legal syntax in Python 3 only. This meansit is not possible to elide default arguments in Python 2 code.You can still elide function bodies in Python 2 by using eitherthe pass statement or by throwing a NotImplementedError.