-
Notifications
You must be signed in to change notification settings - Fork 8
Track narrowed expressions in type-checker e.g. to support if a.b is not None
#1906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@nordlander can we track a dict key as a narrowed expression? if isinstance(foo["hello"]):
blargh = foo["hello"] |
I suppose the question is really whether we can dynamically check if expression What we can do is to check if Instead we rely on type inference to validate an expression like That said, we also have a loop-hole in the current type-checker when it comes to |
Sorry, I don't know what happened there, I just seem to have left out parts of the example. Here's a fixed and more complete one: foo = {
"hello": "world",
"answer": 42
}
def function_that_takes_a_str(s: str):
print(s)
def function_that_takes_an_int(s: int):
print(s)
actor main(env):
if isinstance(foo["hello"], str):
function_that_takes_a_str(foo["hello"])
if isinstance(foo["foo"], int):
function_that_takes_an_int(foo["foo"])
env.exit(0) so what I mean by tracking the dict key is that we isinstance check the value that the key maps to, not the key itself. |
@nordlander and I spoke about this separately and yes, dict key accesses like that should act the same way, we can narrow it. I suppose it's general, so we can narrow any expression? |
Yes, this feature is on the to-do list. But type narrowing will be tied to the syntax of the expressions matched, so the following won't work:
Also, the matched expresssions will have to be pure, otherwise we can't guarantee that a repeated occurrence evaluates to an identical instance. |
@nordlander right, but it is still based on like the AST, so whitespace or like which quoting character is used is irrelevant, right? Like this, (note " vs '):
|
Exactly, expression matching will be done on the abstract syntax. |
I would like to make it possible to use this like a guard statement. Swift has an explicit guard statement, which is basically like an
guards are nice since we can do a bunch of conditions but avoid heavy nesting. However, I don't really understand why the guard needs to be its own statement. Perhaps subject to taste. Anyway, I would like to be able to do:
|
We should track a set of narrowed expressions in the type checker to allow for more natural use of e.g.
if a.b is not None
orisinstance(a.b, int)
This is from #1401 but reshaped here to be more focused (other solutions / partial workarounds are mentioned in #1401).
This means that in this scope,
a.b
will have a narrower type. However, whena
is passed around, it will still have the optionalb
. It is onlya.b
when accessed exactly asa.b
that has the narrower type.The text was updated successfully, but these errors were encountered: