Tim Swena
Tim Swena

Reputation: 14786

What return type annotation to use if a function returns a module in Python?

I'm writing a function to help with an optional dependency (similar to pytest.importorskip) and I'd like to type it, but unsure what type to use. Since I'm always returning a specific module or None, I think I can be more specific than "Any".

def try_import_pyarrow():
    try:
        import pyarrow
    except ImportError:
        pyarrow = None
    return pyarrow

Upvotes: 2

Views: 991

Answers (1)

mkrieger1
mkrieger1

Reputation: 23250

The type of a module is accessible as types.ModuleType.

Upvotes: 7

Related Questions