Report an error for a default on a method's self/cls parameter#3844
Report an error for a default on a method's self/cls parameter#3844markselby9 wants to merge 1 commit into
self/cls parameter#3844Conversation
A method's `self`/`cls` receiver is always supplied implicitly at call time, so a default value on it (e.g. `def m(self=1)`) is unreachable and almost always a mistake. Pyrefly already relied on the invariant that "self/cls never has a default" elsewhere, yet silently accepted such definitions. Detect this during function elaboration: a function has an implicit receiver exactly when `self_type` is `Some` (instance methods, classmethods, `__new__`) and not for staticmethods or top-level functions. When such a function's first positional parameter carries a default, emit a `BadFunctionDefinition` error pointing at the default value. Closes facebook#3729
|
Diff from mypy_primer, showing the effect of this PR on open source code: alerta (https://github.com/alerta/alerta)
+ ERROR alerta/database/base.py:214:47-51: Parameter `query` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
zope.interface (https://github.com/zopefoundation/zope.interface)
+ ERROR src/zope/interface/common/builtins.py:47:18-22: Parameter `key` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/common/idatetime.py:242:16-20: Parameter `tz` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/common/idatetime.py:447:23-26: Parameter `sep` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/common/sequence.py:152:19-21: Parameter `index` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/common/sequence.py:161:22-26: Parameter `cmpfunc` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:222:26-30: Parameter `callback` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:353:19-24: Parameter `all` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:364:34-39: Parameter `all` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:1189:35-39: Parameter `component` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:1217:37-41: Parameter `component` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:1292:35-39: Parameter `factory` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:1379:47-51: Parameter `factory` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
+ ERROR src/zope/interface/interfaces.py:1467:35-39: Parameter `handler` is the `self`/`cls` parameter and cannot have a default value [bad-function-definition]
|
Primer Diff Classification✅ 2 improvement(s) | 2 project(s) total | +14 errors 2 improvement(s) across alerta, zope.interface.
Detailed analysis✅ Improvement (2)alerta (+1)
zope.interface (+13)
Was this helpful? React with 👍 or 👎 Classification by primer-classifier (2 LLM) |
Closes #3729.
Summary
A method's
self/clsreceiver is always supplied implicitly at call time, so a default value on it (e.g.def m(self=1)) is unreachable and almost always a mistake. Pyrefly already relied on the invariant that the receiver never has a default, yet silently accepted such definitions. basedpyright reports the same viareportSelfClsParameterDefault.The check runs during function elaboration: a function has an implicit receiver exactly when
self_typeisSome(instance methods, classmethods including__init_subclass__/__class_getitem__, properties, and__new__; not staticmethods or top-level functions). When such a function's first positional parameter has a default, it emits abad-function-definitionerror pointing at the default.Test Plan
New tests in
pyrefly/lib/test/self_cls_default.rscover instance / classmethod / positional-only /__new__/__init_subclass__/ property receivers (flagged) and staticmethod, top-level function, and non-receiver params (not flagged).cargo testpasses; full lib test suite is green.