I have two functions:
def get_foo(params) -> Optional[str]
def bar(foo: str)
And a function which chains these functions together:
def f(params):
# other stuff up here
foo = get_foo(params)
return bar(foo)
I know based on the other things happening in my function that the result of get_foo
will never be None.
When I run mypy
against this file, I of course get errors:
error: Argument 1 of "bar" has incompatible type "Optional[str]"; expected "str"
which makes sense.
I could add an assert foo is not None
statement, but this is hot-path code and in my tests it has measurable performance impact. I would like to make a type assertion for mypy only. How do I do that?
EDIT: I also tried adding a comment #type: str
after the assignment statement, but this generated a similar error