Typing: Is there a "cast operator"?

Chris Angelico rosuav at gmail.com
Sun Oct 23 23:19:37 EDT 2022


On Mon, 24 Oct 2022 at 14:15, Dan Stromberg <drsalists at gmail.com> wrote:
> I've found that mypy understands simple assert statements.
>
> So if you:
> if f is not None:
>         assert f is not None
>         os.write(f, ...)
>
> You might be in good shape.

Why can't it simply understand the if statement? I'm not a fan of
coddling a type system like this. The entire point of type checking is
to help you find bugs more efficiently, so if you have to repeat
yourself every time you do these kinds of checks just so that mypy is
satisfied, that's counter-productive (case in point: what happens if
you say "if fn is not None: assert f is not None"? Now you've
introduced a bug just to deal with the type system).

ChrisA


More information about the Python-list mailing list