I have this code
if foo:
bar = 1
else:
bar = maybe_return_int(baz)
The return type of maybe_return_int
is Optional[int]
, so mypy complains
error: Incompatible types in assignment (expression has type "int | None", variable has type "int") [assignment]
However, in the context of my code I know that maybe_return_int(baz)
will never return None
. To get it to type-check, I have to write this tortuous thing:
if foo:
bar = 1
else:
bar_maybe_none = maybe_return_int(baz)
assert bar_maybe_none is not None
bar = bar_maybe_none
Is there a shorter way to write this? I would like it if there were an assert_not_none
function that I could use like this:
if foo:
bar = 1
else:
bar = assert_not_none(maybe_return_int(baz))
Could I write such an assert_not_none
function?
I could use cast, but I like that assert will actually do the check (at least when assertions aren’t disabled) and that with assert I don’t have to write the type (in my real code it’s a more verbose thing to type than just an int).
9
This is an implementation of assert_not_none
:
from typing import TypeVar, Optional
T = TypeVar("T")
def assert_not_none(value: Optional[T]) -> T:
assert value is not None
return value
With the above, the following code typechecks:
if foo:
bar = 1
else:
bar = assert_not_none(maybe_return_int(baz))
It feels like it would be useful for this to be implemented in some common place (e.g. the stdlib typing
module) but it isn’t as far as I can tell. I may propose it on the Python Ideas Discourse forum.
One alternative, that at least works in mypy, is to say that the type of bar
is int | None
, and then later assert that it cannot be None
. For example:
from typing import reveal_type
bar: int | None
if foo:
bar = 1
else:
bar = maybe_none(baz)
assert bar is not None
# after this if statement, the type checker knows it is impossible for bar to
# contain None, and so narrows its type to just "int" until such time that it
# might be possible for it to contain None again.
reveal_type(bar) # Revealed type is "builtins.int"
You can avoid the initial annotation if you invert your if branch so that the initial assignment to bar
is of the wider type.
if not foo:
bar = maybe_none(baz)
assert bar is not None
else:
bar = 1
reveal_type(bar) # Revealed type is "builtins.int"