Skip to content

fix cached_property overriding unannotated class variable #3062#3564

Open
asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:3062
Open

fix cached_property overriding unannotated class variable #3062#3564
asukaminato0721 wants to merge 1 commit into
facebook:mainfrom
asukaminato0721:3062

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented May 24, 2026

Summary

Fixes #3062

cached properties are now treated as writable for override consistency, using the getter return type and promoting inferred literal returns like return True to match class-variable inference.

Test Plan

add test

@meta-cla meta-cla Bot added the cla signed label May 24, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review May 24, 2026 14:00
Copilot AI review requested due to automatic review settings May 24, 2026 14:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@github-actions
Copy link
Copy Markdown

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

django-stubs (https://github.com/typeddjango/django-stubs)
- ERROR django-stubs/contrib/gis/db/backends/mysql/features.pyi:17:9-45: Class member `DatabaseFeatures.supports_geometry_field_unique_index` overrides parent class `BaseSpatialFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:17:9-16: Class member `MySQLOperations.mariadb` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:20:9-14: Class member `MySQLOperations.mysql` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:23:9-15: Class member `MySQLOperations.select` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:26:9-18: Class member `MySQLOperations.from_text` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
+ ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:26:9-18: Class member `MySQLOperations.from_text` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override-mutable-attribute]
- ERROR django-stubs/contrib/gis/db/backends/mysql/operations.pyi:34:9-30: Class member `MySQLOperations.unsupported_functions` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/oracle/introspection.pyi:10:9-27: Class member `OracleIntrospection.data_types_reverse` overrides parent class `DatabaseIntrospection` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/postgis/operations.pyi:51:9-23: Class member `PostGISOperations.function_names` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/postgis/operations.pyi:54:9-24: Class member `PostGISOperations.spatial_version` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/spatialite/features.pyi:10:9-31: Class member `DatabaseFeatures.supports_area_geodetic` overrides parent class `BaseSpatialFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/spatialite/operations.pyi:26:9-30: Class member `SpatiaLiteOperations.unsupported_functions` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/gis/db/backends/spatialite/operations.pyi:29:9-24: Class member `SpatiaLiteOperations.spatial_version` overrides parent class `BaseSpatialOperations` in an inconsistent manner [bad-override]
- ERROR django-stubs/contrib/postgres/indexes.pyi:16:9-24: Class member `PostgresIndex.max_name_length` overrides parent class `Index` in an inconsistent manner [bad-override]
- ERROR django-stubs/core/handlers/asgi.pyi:34:9-12: Class member `ASGIRequest.GET` overrides parent class `HttpRequest` in an inconsistent manner [bad-override]
- ERROR django-stubs/core/handlers/asgi.pyi:41:9-16: Class member `ASGIRequest.COOKIES` overrides parent class `HttpRequest` in an inconsistent manner [bad-override]
- ERROR django-stubs/core/handlers/wsgi.pyi:32:9-12: Class member `WSGIRequest.GET` overrides parent class `HttpRequest` in an inconsistent manner [bad-override]
- ERROR django-stubs/core/handlers/wsgi.pyi:35:9-16: Class member `WSGIRequest.COOKIES` overrides parent class `HttpRequest` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:38:9-33: Class member `DatabaseFeatures.minimum_database_version` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
+ ERROR django-stubs/db/backends/mysql/features.pyi:38:9-33: Class member `DatabaseFeatures.minimum_database_version` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override-mutable-attribute]
- ERROR django-stubs/db/backends/mysql/features.pyi:41:9-24: Class member `DatabaseFeatures.test_collations` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
+ ERROR django-stubs/db/backends/mysql/features.pyi:41:9-24: Class member `DatabaseFeatures.test_collations` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override-mutable-attribute]
- ERROR django-stubs/db/backends/mysql/features.pyi:44:9-26: Class member `DatabaseFeatures.django_test_skips` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:47:9-25: Class member `DatabaseFeatures.allows_auto_pk_0` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:50:9-31: Class member `DatabaseFeatures.update_can_self_select` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:53:9-36: Class member `DatabaseFeatures.can_introspect_foreign_keys` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:56:9-33: Class member `DatabaseFeatures.introspected_field_types` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:59:9-39: Class member `DatabaseFeatures.can_return_columns_from_insert` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:65:9-30: Class member `DatabaseFeatures.has_zoneinfo_database` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:70:9-42: Class member `DatabaseFeatures.supports_column_check_constraints` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:76:9-41: Class member `DatabaseFeatures.can_introspect_check_constraints` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:79:9-33: Class member `DatabaseFeatures.has_select_for_update_of` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:84:9-34: Class member `DatabaseFeatures.supported_explain_formats` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:90:9-32: Class member `DatabaseFeatures.ignores_table_name_case` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:93:9-37: Class member `DatabaseFeatures.supports_default_in_lead_lag` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:99:9-34: Class member `DatabaseFeatures.can_introspect_json_field` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:102:9-39: Class member `DatabaseFeatures.supports_index_column_ordering` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:105:9-36: Class member `DatabaseFeatures.supports_expression_indexes` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:108:9-37: Class member `DatabaseFeatures.supports_select_intersection` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:114:9-37: Class member `DatabaseFeatures.supports_expression_defaults` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:117:9-30: Class member `DatabaseFeatures.has_native_uuid_field` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:120:9-37: Class member `DatabaseFeatures.allows_group_by_selected_pks` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/mysql/features.pyi:123:9-27: Class member `DatabaseFeatures.supports_any_value` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:55:9-26: Class member `DatabaseFeatures.django_test_skips` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:58:9-33: Class member `DatabaseFeatures.introspected_field_types` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:61:9-24: Class member `DatabaseFeatures.test_collations` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:64:9-40: Class member `DatabaseFeatures.supports_collation_on_charfield` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:67:9-42: Class member `DatabaseFeatures.supports_primitives_in_json_field` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:70:9-33: Class member `DatabaseFeatures.supports_frame_exclusion` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:73:9-40: Class member `DatabaseFeatures.supports_comparing_boolean_expr` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:78:9-27: Class member `DatabaseFeatures.bare_select_suffix` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/oracle/features.pyi:81:9-31: Class member `DatabaseFeatures.supports_tuple_lookups` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/postgresql/features.pyi:49:9-26: Class member `DatabaseFeatures.django_test_skips` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/postgresql/features.pyi:52:9-38: Class member `DatabaseFeatures.django_test_expected_failures` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/postgresql/features.pyi:57:9-52: Class member `DatabaseFeatures.prohibits_null_characters_in_text_exception` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
+ ERROR django-stubs/db/backends/postgresql/features.pyi:57:9-52: Class member `DatabaseFeatures.prohibits_null_characters_in_text_exception` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override-mutable-attribute]
- ERROR django-stubs/db/backends/postgresql/features.pyi:60:9-33: Class member `DatabaseFeatures.introspected_field_types` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/sqlite3/features.pyi:14:9-26: Class member `DatabaseFeatures.django_test_skips` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/sqlite3/features.pyi:17:9-33: Class member `DatabaseFeatures.introspected_field_types` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/sqlite3/features.pyi:23:9-28: Class member `DatabaseFeatures.supports_json_field` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/backends/sqlite3/features.pyi:32:9-39: Class member `DatabaseFeatures.can_return_columns_from_insert` overrides parent class `BaseDatabaseFeatures` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/models/expressions.pyi:136:9-24: Class member `CombinedExpression.allowed_default` overrides parent class `Expression` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/models/expressions.pyi:195:9-24: Class member `Func.allowed_default` overrides parent class `Expression` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/models/expressions.pyi:285:9-24: Class member `When.allowed_default` overrides parent class `Expression` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/models/expressions.pyi:298:9-24: Class member `Case.allowed_default` overrides parent class `Expression` in an inconsistent manner [bad-override]
- ERROR django-stubs/db/models/fields/related.pyi:82:9-22: Class member `RelatedField.related_model` overrides parent class `Field` in an inconsistent manner [bad-override]
+ ERROR django-stubs/db/models/fields/related.pyi:82:9-22: Class member `RelatedField.related_model` overrides parent class `Field` in an inconsistent manner [bad-override-mutable-attribute]
- ERROR django-stubs/db/models/lookups.pyi:48:9-24: Class member `Lookup.allowed_default` overrides parent class `Expression` in an inconsistent manner [bad-override]
+ ERROR tests/assert_type/apps/test_config.py:27:1-2: Unused `# pyrefly: ignore` comment for code(s): bad-override [unused-ignore]

@github-actions
Copy link
Copy Markdown

Primer Diff Classification

✅ 1 improvement(s) | 1 project(s) total | +6, -63 errors

1 improvement(s) across django-stubs.

Project Verdict Changes Error Kinds Root Cause
django-stubs ✅ Improvement +6, -63 Removed bad-override false positives (63 errors) is_cached_property()
Detailed analysis

✅ Improvement (1)

django-stubs (+6, -63)

Removed bad-override false positives (63 errors): These were false positives where pyrefly incorrectly flagged cached_property overriding plain class variables. The stubs have # type: ignore[override] comments confirming these were known false positives. The PR correctly treats cached_property as writable, making it compatible with mutable class variable overrides. This is a clear improvement.
New bad-override-mutable-attribute errors (5 errors): These are pyrefly-only errors on a well-tested stubs project (0/5 flagged by mypy or pyright). The errors occur where child classes use cached_property to override parent class variables. The bad-override-mutable-attribute error indicates pyrefly finds the types incompatible under invariant checking for mutable attributes. Without seeing the parent class definitions, the exact cause is unclear — it could be that pyrefly infers different types for the parent attributes than what the child's cached_property returns (e.g., the parent might have a default value that infers to a more specific or different type). Since neither mypy nor pyright flags these, and the stubs already have # type: ignore[override] comments, these appear to be remaining false positives from type inference issues. This is a minor regression within the larger improvement.
New unused-ignore error (1 error): This is a cascade effect — a # pyrefly: ignore comment for bad-override is now unused because the underlying false positive was fixed. This is neutral/positive — it means the fix worked.

Overall: This is a net improvement. The PR fixes a real issue (#3062) where cached_property overriding an unannotated class variable was incorrectly flagged. 63 false positive bad-override errors are removed. The 5 new bad-override-mutable-attribute errors are pyrefly-only on a stubs project — looking at the specific cases:

  1. minimum_database_version and test_collations in BaseDatabaseFeatures — the child class uses cached_property returning tuple[int, ...] and dict[str, str] respectively. The parent class BaseDatabaseFeatures likely defines these as plain class variables. The bad-override-mutable-attribute error indicates pyrefly detects a mutable attribute override but finds the types incompatible — this could be due to how pyrefly infers the parent's attribute type (e.g., from a default value assignment) versus the child's explicit return type annotation.
  2. from_text in BaseSpatialOperations — similar pattern where the parent has a class variable and the child overrides with cached_property.
  3. related_model in Field — the child RelatedField overrides with cached_property.

These 5 new errors are not flagged by mypy or pyright (0/5 cross-check), and the stubs already have # type: ignore[override] comments, suggesting these are known override patterns that other type checkers accept. These appear to be false positives, likely due to type inference differences in how pyrefly resolves the parent class attribute types compared to the child's cached_property return types. The 1 unused-ignore error is a cascade from fixing the other errors — a # pyrefly: ignore comment for bad-override is now unused because the underlying false positive was fixed.

Net: 63 false positives removed, 5 new false positives added (plus 1 unused-ignore). This is a significant net improvement (57 fewer total errors).

Attribution: The changes in pyrefly/lib/alt/class/class_field.rs added two new match arms for ClassAttribute::Property when got_getter.[is_cached_property()](https://github.com/facebook/pyrefly/blob/main/pyrefly/lib/alt/class/class_field.rs) is true. The first arm handles cached_property overriding a ReadWrite (mutable) attribute with invariant checking, and the second handles cached_property overriding a ReadOnly attribute with covariant checking. The cached_property_value_type helper extracts the getter return type and promotes inferred literals. This removed the previous fallthrough to the generic property-vs-attribute override check that was producing the 63 false positive bad-override errors. The 5 remaining bad-override-mutable-attribute errors come from the new invariant check in the first match arm, where the cached_property return type doesn't match the parent's mutable attribute type invariantly.


Was this helpful? React with 👍 or 👎

Classification by primer-classifier (1 LLM)

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.

cached_property overriding unannotated class variable

2 participants