Skip to content

Fix cascading errors when narrowing missing attributes - #5482

Open
maharshi-coding wants to merge 1 commit into
meta:mainfrom
maharshi-coding:fix/5333-missing-attribute-unknown
Open

maharshi-coding wants to merge 1 commit into
meta:mainfrom
maharshi-coding:fix/5333-missing-attribute-unknown

Conversation

@maharshi-coding

Copy link
Copy Markdown

Fixes #5333

What was going wrong?

When narrowing an attribute that doesn't exist, Pyrefly was caching its type as object instead of Unknown.
For example:

x: int = 10

if x.foo:
    print(x.foo[0])

Pyrefly correctly reports that int has no attribute foo, but it also reports another error when trying to index x.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 error Unknown instead of falling back to object. Actual internal lookup errors still use the existing object fallback.
  • 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 | Unknown instead of Foo | 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, and len() 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.

@meta-cla meta-cla Bot added the cla signed label Nov 2, 2026
@github-actions github-actions Bot added the size/l label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

⚠️ Action needed: this PR is missing required items from our pull request checklist:

Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour.

@meta-codesync

meta-codesync Bot commented Nov 2, 2026

Copy link
Copy Markdown
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.)

@rchen152 rchen152 added size/xs and removed size/l labels Nov 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Narrowing on a missing attribute narrows it to object instead of Unknown, causing cascading errors

2 participants