Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mypy "class module" annotation

Tags:

python

types

mypy

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?

like image 917
PovilasB Avatar asked Oct 03 '16 08:10

PovilasB


1 Answers

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.

like image 170
max Avatar answered Oct 21 '22 10:10

max