# use typing.Literal - mode expects either 'a' or 'b' from typing import Literal def func(mode: Literal['a', 'b'] = 'a'): if mode not in ['a', 'b']: raise AttributeError('Not OK')