pyrefly
pyrefly copied to clipboard
pyrefly does not track mapping keys after explicit check
Describe the Bug
Consider:
options: dict[str, str] = {}
if 'contains' in options:
ingredients = [x.strip() for x in options.get('contains').split(',')]
options is known to have the key 'contains' in the branch given the explicit check, so options.get('contains') will have type VT (here str) not None. pyrefly currently reports an error, though.
I don't believe any type checker currently recognises this, so no objections to this being closed as out of scope, but I thought worth raising the question.
A
Sandbox Link
https://pyrefly.org/sandbox/?code=PYBwLglsB2DOBcACAJhAxmA2rMAnANIjrgLqIC8iA3gL4BQEAZogORoxgCGEcLiPiUJBgI6icf2gBzXAFNUs6GFgVEmAB4A6YhBAAKAJSJGwXInWTB4KHE1TZYPWw7deB7SAA2ERy3wsDEjogA
(Only applicable for extension issues) IDE Information
No response
Thanks for flagging this; I think it's similar to (but not exactly the same as) https://github.com/facebook/pyrefly/issues/238
Thanks Danny, I didn't see that in a quick search. Sorry!
A
This issue has someone assigned, but has not had recent activity for more than 2 weeks.
If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.
Thank you for your contributions!
Taking myself off this because I'm not really touching narrowing anymore
cc @yangdanny97 - this is pretty similar to some of the other narrowing cases like hasattr
Yeah, I think this would require implementing https://github.com/facebook/pyrefly/issues/238 + adding a hasattr style narrowing for keys.
@xaskii is working on hasattr right now, so I'll probably extend that to support this once he's done
This issue has someone assigned, but has not had recent activity for more than 2 weeks.
If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.
Thank you for your contributions!