Bind self for un-annotated Callable class attributes#4203
Open
ANU-2524 wants to merge 1 commit into
Open
Conversation
Class attributes whose type is a `Callable` (rather than a `def`) were only treated as methods when named like a dunder or annotated `ClassVar[Callable]`. Any other `Callable` stored in the class body was treated as a plain data attribute, so calling it on an instance did not drop `self`. This produced a false `bad-argument-count` for factory-produced methods such as PySpark's `Column.isNull`/`asc`/`eqNullSafe`, which are assigned as `isNull = _unary_op(...)` (issue facebook#3465). At runtime a plain function object stored on a class is a descriptor, so it binds `self`. mypy and pyright model this by binding `self` for any class-body Callable unless it carries an explicit, non-`ClassVar` annotation. `is_method` now follows the same rule: an inferred (un-annotated) Callable, a `ClassVar[Callable]`, or a dunder binds `self`; an explicitly annotated bare Callable does not. This also makes a class-body lambda a bound method, matching mypy/pyright. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.
Class attributes whose type is a
Callable(rather than adef) were only treated as methods when named like a dunder or annotatedClassVar[Callable]. Any otherCallablestored in the class body was treated as a plain data attribute, so calling it on an instance did not dropself. This produced a falsebad-argument-countfor factory-produced methods such as PySpark'sColumn.isNull/asc/eqNullSafe, which are assigned asisNull = _unary_op(...)(issue #3465).At runtime a plain function object stored on a class is a descriptor, so it binds
self. mypy and pyright model this by bindingselffor any class-body Callable unless it carries an explicit, non-ClassVarannotation.is_methodnow follows the same rule: an inferred (un-annotated) Callable, aClassVar[Callable], or a dunder bindsself; an explicitly annotated bare Callable does not. This also makes a class-body lambda a bound method, matching mypy/pyright.Summary
Fixes #3465
Test Plan
selfwhile class access does not.Callable, explicitCallable, andClassVar[Callable].isNull,asc,desc,eqNullSafe) and verified the falsebad-argument-counterrors are resolved.