Is there a type hint to go with SimpleNamespace?

Viewed 1011

Say I am defining a function that returns a types.SimpleNamespace. I would like to type-hint the result:

from types import SimpleNamespace

def func() -> SimpleNamespace(x: int, y: str):  # SyntaxError! What should be used instead?
    return SimpleNamespace(x=3, y='abc')

Note that SimpleNamespace is commonly used as alternative to tuple in which data members are named. For tuples, a corresponding type hint exists:

from typing import Tuple

def func() -> Tuple[int, str]:  # OK
    return 3, 'abc'

BTW, today I am using the following for documentation's sake:

from types import SimpleNamespace

def func() -> SimpleNamespace(x=int, y=str):  # Seems to work fine
    return SimpleNamespace(x=3, y='abc')

But this is not standard (so it won't be supported by type checkers), and perhaps not proper Python (or is it?).

0 Answers
Related