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

Viewed 194

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
1 Answers
Related