Skip to content

fix: recognize @override on property getter with setter - #5098

Closed
Aayushyaash wants to merge 1 commit into
pyrefly-org:mainfrom
Aayushyaash:fix-property-override-getter
Closed

Aayushyaash wants to merge 1 commit into
pyrefly-org:mainfrom
Aayushyaash:fix-property-override-getter

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

Summary

When a subclass overrides a property that defines both a getter and a setter, Python's class evaluation binds the class attribute to the setter function.

Previously, ClassField::is_override() only checked the top-level function ty.is_override(). When @override was placed on the getter, it was missed because the getter is stored inside ty.property_metadata(), resulting in a false-positive missing-override-decorator error.

This PR updates ClassField::is_override() to check if either the top-level function or any constituent function in property_metadata (getter or setter) has the @override decorator, while preserving bad-override checks when no parent attribute exists.

Fixes #5058

  • 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 for:

  • A getter with @override and a setter
  • A setter with @override
  • Both getter and setter with @override
  • @override on a property with no corresponding base class attribute, ensuring BadOverride is still reported

Validation:

cargo test test_missing_override_decorator_property
cargo test test_bad_override_property_with_setter
cargo fmt -- --check
python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension

When a property defines both a getter and a setter, the class field binds to the setter function, causing ClassField::is_override() to ignore @OverRide on the getter. This change checks both the top-level function and property_metadata so that @OverRide on either component is recognized while preserving bad-override checks, accompanied by regression tests.
@meta-codesync

meta-codesync Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

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

============================================================
SUMMARY
============================================================
Total: +0 new errors, -2 fixed errors
By preset: +0/-0 (default), +0/-2 (strict)

Projects with changes (2):
  zulip: +0 -1
  core: +0 -1
============================================================

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

zulip (https://github.com/zulip/zulip)
- ERROR zerver/lib/response.py:45:9-16: Class member `MutableJsonResponse.content` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]

core (https://github.com/home-assistant/core)
- ERROR homeassistant/components/zha/config_flow.py:160:9-13: Class member `BaseZhaFlow.hass` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]

@rchen152 rchen152 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.

@rchen152

rchen152 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Thanks @Aayushyaash!

@meta-codesync meta-codesync Bot closed this in c9e8429 Oct 6, 2026
@meta-codesync

meta-codesync Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in c9e8429.

@meta-codesync meta-codesync Bot added the Merged label Oct 6, 2026
@Aayushyaash
Aayushyaash deleted the fix-property-override-getter branch October 8, 2026 08:06
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.

missing-override-decorator ignores @override on a property getter when the property has a setter

3 participants