Skip to content

fix: disallow overriding staticmethod/classmethod with instance method - #5316

Closed
Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-static-classmethod-override
Closed

Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-static-classmethod-override

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

Summary

Overriding an inherited @staticmethod or @classmethod with an ordinary instance method can introduce a runtime error when the method is accessed through a class.

For example, if a base class defines a static method and a subclass overrides it with def fn(self), calling fn() through a class object can fail because Python does not automatically provide an instance:

class Foo:
    @staticmethod
    def fn() -> None: ...

class Bar(Foo):
    def fn(self) -> None: ...

def call_fn(foo: type[Foo]) -> None:
    foo.fn()  # Runtime TypeError when foo is Bar

call_fn(Bar)

Previously, Pyrefly checked method override consistency after resolving both parent and child members as instance attributes via as_instance_attribute. Since this removes the binding difference between an ordinary instance method and a static method, their resulting types could appear compatible, allowing the invalid override to pass without a diagnostic.

This PR fixes the issue by:

  1. Adding method-descriptor helpers (is_staticmethod, is_classmethod, and is_ordinary_instance_method) to ClassField and Type.
  2. Updating check_consistent_override_for_field to detect when an inherited static or class method is overridden by an ordinary instance method and emit a BadOverride diagnostic.
  3. Preserving valid override patterns, including overriding an instance method with a @staticmethod or @classmethod, and interchange between @staticmethod and @classmethod.

Fixes #5296

  • I am a human, and I fully understand the code I am submitting.

Test Plan

Added regression tests in pyrefly/lib/test/class_overrides.rs covering:

  • Overriding an inherited @staticmethod with an instance method, with and without @override.
  • Overriding an inherited @classmethod with an instance method, with and without @override.
  • Implementing an abstract @staticmethod with an instance method.
  • Ensuring valid overrides, such as overriding an instance method with a @staticmethod, continue to pass without errors.

Validation

cargo test --lib -p pyrefly -- test_override_staticmethod_with_instance_method
cargo test --lib -p pyrefly -- test_override_classmethod_with_instance_method
cargo test --lib -p pyrefly -- test_override_abstract_staticmethod_with_instance_method
cargo test --lib -p pyrefly -- test::class_overrides::
cargo clippy -p pyrefly
cargo fmt -- --check

@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D126711223. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

============================================================
SUMMARY
============================================================
Total: +25 new errors, -11 fixed errors
By preset: +25/-10 (default), +25/-10 (strict)

Projects with changes (7):
  pip: +1 -1
  apprise: +2 -0
  rotki: +0 -1
  ibis: +2 -0
  pandas: +2 -2
  pytensor: +10 -2
  cvxpy: +8 -5
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

pip (https://github.com/pypa/pip)
- ERROR src/pip/_vendor/distlib/resources.py:293:9-22: Class member `ZipResourceFinder._is_directory` overrides parent class `ResourceFinder` in an inconsistent manner [bad-override]
+ ERROR src/pip/_vendor/distlib/resources.py:293:9-22: Instance method `ZipResourceFinder._is_directory` overrides static method of the same name in parent class `ResourceFinder` [bad-override]

apprise (https://github.com/caronc/apprise)
+ ERROR tests/test_apprise_cli.py:1079:13-22: Instance method `NoURLIDNotification.parse_url` overrides static method of the same name in parent class `NotifyBase` [bad-override]
+ ERROR tests/test_apprise_cli.py:1103:13-22: Instance method `TestNotification.parse_url` overrides static method of the same name in parent class `NotifyBase` [bad-override]

rotki (https://github.com/rotki/rotki)
- ERROR rotkehlchen/externalapis/blockscout.py:235:93-117: Unused `# type: ignore` comment [unused-type-ignore]

ibis (https://github.com/ibis-project/ibis)
+ ERROR ibis/backends/clickhouse/__init__.py:90:9-24: Instance method `Backend._convert_kwargs` overrides static method of the same name in parent class `SQLBackend` [bad-override]
+ ERROR ibis/backends/snowflake/__init__.py:177:9-24: Instance method `Backend._convert_kwargs` overrides static method of the same name in parent class `SQLBackend` [bad-override]

pandas (https://github.com/pandas-dev/pandas)
- ERROR pandas/tests/apply/conftest.py:21:9-12: Class member `MockExecutionEngine.map` overrides parent class `BaseExecutionEngine` in an inconsistent manner [bad-override-param-name]
+ ERROR pandas/tests/apply/conftest.py:21:9-12: Instance method `MockExecutionEngine.map` overrides static method of the same name in parent class `BaseExecutionEngine` [bad-override]
- ERROR pandas/tests/apply/conftest.py:25:9-14: Class member `MockExecutionEngine.apply` overrides parent class `BaseExecutionEngine` in an inconsistent manner [bad-override-param-name]
+ ERROR pandas/tests/apply/conftest.py:25:9-14: Instance method `MockExecutionEngine.apply` overrides static method of the same name in parent class `BaseExecutionEngine` [bad-override]

pytensor (https://github.com/pymc-devs/pytensor)
+ ERROR pytensor/compile/mode.py:317:9-22: Instance method `_ActiveModeGraphRewriter.print_profile` overrides class method of the same name in parent class `GraphRewriter` [bad-override]
+ ERROR pytensor/graph/null_type.py:29:9-18: Instance method `NullType.values_eq` overrides class method of the same name in parent class `Type` [bad-override]
+ ERROR pytensor/link/c/params_type.py:666:9-18: Instance method `ParamsType.values_eq` overrides class method of the same name in parent class `CType` [bad-override]
+ ERROR pytensor/link/c/params_type.py:674:9-25: Instance method `ParamsType.values_eq_approx` overrides class method of the same name in parent class `CType` [bad-override]
+ ERROR pytensor/link/c/type.py:545:9-18: Instance method `EnumType.values_eq` overrides class method of the same name in parent class `CType` [bad-override]
+ ERROR pytensor/link/c/type.py:548:9-25: Instance method `EnumType.values_eq_approx` overrides class method of the same name in parent class `CType` [bad-override]
+ ERROR pytensor/scalar/basic.py:326:9-25: Instance method `ScalarType.values_eq_approx` overrides class method of the same name in parent class `CType` [bad-override]
- ERROR pytensor/sparse/type.py:186:9-25: Class member `SparseTensorType.values_eq_approx` overrides parent class `TensorType` in an inconsistent manner [bad-override]
+ ERROR pytensor/sparse/type.py:186:9-25: Instance method `SparseTensorType.values_eq_approx` overrides static method of the same name in parent class `TensorType` [bad-override]
- ERROR pytensor/sparse/type.py:202:9-18: Class member `SparseTensorType.values_eq` overrides parent class `TensorType` in an inconsistent manner [bad-override]
+ ERROR pytensor/sparse/type.py:202:9-18: Instance method `SparseTensorType.values_eq` overrides static method of the same name in parent class `TensorType` [bad-override]
+ ERROR pytensor/typed_list/type.py:78:9-18: Instance method `TypedListType.values_eq` overrides class method of the same name in parent class `CType` [bad-override]

cvxpy (https://github.com/cvxpy/cvxpy)
+ ERROR cvxpy/lin_ops/backends/base.py:453:9-33: Instance method `DictTensorView.combine_potentially_none` overrides static method of the same name in parent class `TensorView` [bad-override]
+ ERROR cvxpy/lin_ops/backends/coo_backend.py:1664:9-28: Instance method `CooCanonBackend.concatenate_tensors` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
- ERROR cvxpy/lin_ops/backends/coo_backend.py:1672:9-20: Class member `CooCanonBackend.sum_entries` overrides parent class `PythonCanonBackend` in an inconsistent manner [bad-override]
+ ERROR cvxpy/lin_ops/backends/coo_backend.py:1672:9-20: Instance method `CooCanonBackend.sum_entries` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
- ERROR cvxpy/lin_ops/backends/coo_backend.py:1730:9-16: Class member `CooCanonBackend.reshape` overrides parent class `PythonCanonBackend` in an inconsistent manner [bad-override]
+ ERROR cvxpy/lin_ops/backends/coo_backend.py:1730:9-16: Instance method `CooCanonBackend.reshape` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
+ ERROR cvxpy/lin_ops/backends/coo_backend.py:1959:9-37: Instance method `CooCanonBackend.get_constant_data_from_const` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
- ERROR cvxpy/lin_ops/backends/coo_backend.py:2512:9-14: Class member `CooCanonBackend.trace` overrides parent class `PythonCanonBackend` in an inconsistent manner [bad-override]
+ ERROR cvxpy/lin_ops/backends/coo_backend.py:2512:9-14: Instance method `CooCanonBackend.trace` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
- ERROR cvxpy/lin_ops/backends/scipy_backend.py:413:9-30: Class member `SciPyCanonBackend.reshape_constant_data` overrides parent class `PythonCanonBackend` in an inconsistent manner [bad-override]
+ ERROR cvxpy/lin_ops/backends/scipy_backend.py:413:9-30: Instance method `SciPyCanonBackend.reshape_constant_data` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]
- ERROR cvxpy/lin_ops/backends/scipy_backend.py:837:9-20: Class member `SciPyCanonBackend.sum_entries` overrides parent class `PythonCanonBackend` in an inconsistent manner [bad-override]
+ ERROR cvxpy/lin_ops/backends/scipy_backend.py:837:9-20: Instance method `SciPyCanonBackend.sum_entries` overrides static method of the same name in parent class `PythonCanonBackend` [bad-override]

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in ffe6763 Oct 22, 2026
@meta-codesync meta-codesync Bot added the Merged label Oct 22, 2026
@meta-codesync

meta-codesync Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in ffe6763.

@Aayushyaash
Aayushyaash deleted the fix-static-classmethod-override branch October 23, 2026 04:33
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.

no error when overriding staticmethod/classmethod with instance method

3 participants