Both epydoc and Sphinx document generators permit the coder to annotate what the types should be of any/all function parameter.
My question is: Is there a way (or module) that enforces these types (at run-time) when documented in the docstring. This wouldn't be strong-typing (compile-time checking), but (more likely) might be called firm-typing (run-time checking). Maybe raising a "ValueError", or even better still... raising a "SemanticError"
Ideally there would already be something (like a module) similar to the "import antigravity
" module as per xkcd, and this "firm_type_check" module would already exist somewhere handy for download.
FYI: The docstring for epydoc and sphinz are as follows:
epydoc: Functions and Methods parameters:
- @param p: ... # A description of the parameter p for a function or method.
- @type p: ... # The expected type for the parameter p.
- @return: ... # The return value for a function or method.
- @rtype: ... # The type of the return value for a function or method.
- @keyword p: ... # A description of the keyword parameter p.
- @raise e: ... # A description of the circumstances under which a function or method raises exception e.
Sphinx: Inside Python object description directives, reST field lists with these fields are recognized and formatted nicely:
- param, parameter, arg, argument, key, keyword: Description of a parameter.
- type: Type of a parameter.
- raises, raise, except, exception: That (and when) a specific exception is raised.
- var, ivar, cvar: Description of a variable.
- returns, return: Description of the return value.
- rtype: Return type.
The closest I could find was a mention by Guido in mail.python.org and created by Jukka Lehtosalo at Mypy Examples. CMIIW: mypy cannot be imported as a py3 module.
Similar stackoverflow questions that do not use the docstring per se: