Skip to content

pyrefly does not track mapping keys after explicit check #289

@AA-Turner

Description

@AA-Turner

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

Sub-issues

Metadata

Metadata

Labels

narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothtypechecking

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions