PovilasB
PovilasB

Reputation: 1608

Mypy "class module" annotation

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?

Upvotes: 7

Views: 1745

Answers (1)

max
max

Reputation: 52373

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.

Upvotes: 6

Related Questions