I'm maintaining a scientific Python package which takes string parameters for some arguments. For example:
def method(...
interp_method, # options being "cgrid_velocity", "partialslip", "freeslip"
):
I know that I can type annotate these using Literal[...] and define a type alias in a _typing.py file (e.g, InterpMethod = Literal[...]) to avoid repetition. What I want to know is:
- How could I do runtime typechecking here in a way that promotes a single source of truth? Is there a standard approach here? A lot of methods take
interp_methodas a parameter and I don't want to repeat myself
Any resources on type guarding in public APIs would be useful.