acton icon indicating copy to clipboard operation
acton copied to clipboard

Extend hard coded check `if X is not None` to also cover `if X is None: ... else:`

Open plajjan opened this issue 2 years ago • 1 comments

We have a hard coded pattern for when under what scope it is acceptable to treat X as a non-optional type and that is if X is not None:.

We've talked about opening up other cases, like checking object attributes and similar in #1401 but it is hard.

Every now and then I find myself wanting to flip the if X is not None: with its else clause, just because it might be more appealing writing one clause before the other. I think in general I like to write the positive / happy case first and error handling later. When X is a variable that holds a normal return value, this is the case.

x = foo()
if x is not None:
    ... happy path
else:
    ... error handling

but we have places where we might get an error / exception, in which case we want to flip it around:

err: ?Exception
if err is None:
    ... happy path
else:
    print(err)

this later case is not currently supported. Is it trivial to add? If so, I'd like to have it added. @nordlander is this a quick addition or something larger?

plajjan avatar Sep 26 '23 19:09 plajjan

This is just a rewrite rule in the compiler, so it's fairly simple but it does take a few hours or something and since it's not all that important we'll wait a bit...

plajjan avatar Nov 14 '23 20:11 plajjan