pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

pyrefly does not track mapping keys after explicit check

Open AA-Turner opened this issue 6 months ago • 2 comments
trafficstars

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

AA-Turner avatar May 20 '25 01:05 AA-Turner

Thanks for flagging this; I think it's similar to (but not exactly the same as) https://github.com/facebook/pyrefly/issues/238

yangdanny97 avatar May 20 '25 01:05 yangdanny97

Thanks Danny, I didn't see that in a quick search. Sorry!

A

AA-Turner avatar May 20 '25 01:05 AA-Turner

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!

github-actions[bot] avatar Jul 06 '25 00:07 github-actions[bot]

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

stroxler avatar Aug 11 '25 18:08 stroxler

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

yangdanny97 avatar Aug 14 '25 02:08 yangdanny97

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!

github-actions[bot] avatar Aug 29 '25 00:08 github-actions[bot]