Repository navigation
Fix cascading errors when narrowing missing attributes - #5482
Open
maharshi-coding wants to merge 1 commit into
Open
maharshi-coding wants to merge 1 commit into
maharshi-coding wants to merge 1 commit into
Conversation
Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour. |
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D129020529. (Because this pull request was imported automatically, there will not be any future comments.) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5333
What was going wrong?
When narrowing an attribute that doesn't exist, Pyrefly was caching its type as
objectinstead ofUnknown.For example:
Pyrefly correctly reports that
inthas no attributefoo, but it also reports another error when trying to indexx.foo. That second error isn't really useful since the missing attribute was already reported.What I changed
I made two changes to fix this:
attr.rs: Changed how missing attributes are handled during narrowing. They now produce an errorUnknowninstead of falling back toobject. Actual internal lookup errors still use the existingobjectfallback.expr.rs: Adjusted the error-origin check so that reading a previously cached error type doesn't trigger another diagnostic.This also preserves valid types when narrowing unions. For example, if one union member has the attribute and another doesn't, the result can now be
Foo | Unknowninstead ofFoo | object.Testing
Added 8 regression tests and updated one existing expectation.
The tests cover missing attributes in different conditions, including truthy checks,
is not None, nested branches, andlen()checks. I also checked union types, class attributes,isinstance()narrowing, and error provenance diagnostics.The main thing I wanted to make sure of was that Pyrefly still reports the original missing-attribute error without producing additional errors from the same issue, while keeping valid narrowing behavior unchanged.