6

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?

4

1 回答 1

4

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.

于 2017-04-02T19:27:15.793 回答