I have a function load_config
that loads and returns a python module:
import imp
def load_config(path: str):
return imp.load_source('config', path)
print(type(load_config('config.py')))
This snippet prints <class 'module'>
.
How do I annotate load_config
return value with Mypy?
The correct annotation is:
import imp
import types
def load_config(path: str) -> types.ModuleType:
return imp.load_source('config', path)
However, there's an open issue to fix it in the current version of mypy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With