-
Notifications
You must be signed in to change notification settings - Fork 7
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
Track narrowed expressions in type-checker e.g. to support if a.b is not None
#1906
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. |
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: