Repository navigation
fix: disallow overriding staticmethod/classmethod with instance method - #5316
Closed
Aayushyaash wants to merge 2 commits into
Closed
Aayushyaash wants to merge 2 commits into
Aayushyaash wants to merge 2 commits into
Conversation
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.) |
|
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
approved these changes
Oct 22, 2026
stroxler
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Contributor
|
This pull request has been merged in ffe6763. |
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
Overriding an inherited
@staticmethodor@classmethodwith 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), callingfn()through a class object can fail because Python does not automatically provide an instance: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:
is_staticmethod,is_classmethod, andis_ordinary_instance_method) toClassFieldandType.check_consistent_override_for_fieldto detect when an inherited static or class method is overridden by an ordinary instance method and emit aBadOverridediagnostic.@staticmethodor@classmethod, and interchange between@staticmethodand@classmethod.Fixes #5296
Test Plan
Added regression tests in
pyrefly/lib/test/class_overrides.rscovering:@staticmethodwith an instance method, with and without@override.@classmethodwith an instance method, with and without@override.@staticmethodwith an instance method.@staticmethod, continue to pass without errors.Validation