Don't resolve implicit __bool__ through __getattr__ - #4542
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
Implicit dunder methods such as `__bool__` are looked up on the type and bypass `__getattr__` at runtime. When evaluating truthiness, Pyrefly resolved `__bool__` with the `__getattr__` fallback enabled, so a class whose `__getattr__` returns a non-callable type (e.g. `torch.nn.Module`, whose `__getattr__` is typed to return `Tensor | Module`) was treated as defining a non-callable `__bool__` and wrongly reported as `not-callable` in boolean contexts. Disable the `__getattr__` fallback when looking up `__bool__` in `as_bool`. Closes facebook#4467
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D115946422. (Because this pull request was imported automatically, there will not be any future comments.) |
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.
Summary
Fixes #4467.
Implicit dunder methods such as
__bool__are looked up on the type and bypass__getattr__at runtime (CPython resolves them via_PyObject_LookupSpecial, which ignores both the instance dict and__getattr__/__getattribute__).When evaluating truthiness in
as_bool, Pyrefly looked up__bool__with the__getattr__fallback enabled. So a class whose__getattr__returns a non-callable type — e.g.torch.nn.Module, whose__getattr__is typed to returnTensor | Module— was treated as defining a non-callable__bool__, and using an instance in a boolean context was wrongly reported asnot-callable:The fix disables the
__getattr__fallback for the__bool__lookup inas_bool(pyrefly/lib/alt/expr.rs). The separatecheck_dunder_bool_is_callablepath already disables the fallback; this brings the truthiness-evaluation path in line. The subsequent__bool__call is only reached when a real__bool__is found directly, so classes that genuinely define__bool__are unaffected.Test Plan
test_getattr_does_not_provide_dunder_boolinpyrefly/lib/test/attributes.rs. It fails before the change (expected 0 errors, but got 2) and passes after.cargo test -p pyrefly --lib: all type-checking tests pass (the only failures on my machine are two pre-existinglsp_interaction::diagnostictimeout tests that also fail on unmodifiedmain, unrelated to this change).cargo clippy -p pyrefly --libandcargo fmt --checkare clean for the changed files.