Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an Error When Computed Field Overrides Field #7346

Merged
merged 4 commits into from Sep 5, 2023

Conversation

sydney-runkle
Copy link
Member

@sydney-runkle sydney-runkle commented Sep 5, 2023

Change Summary

  • If a computed_field is defined in a subclass such that it overrides a field from a superclass, a ValueError is raised.

Related issue number

fix #7250

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@sydney-runkle sydney-runkle marked this pull request as ready for review September 5, 2023 20:07
tests/test_computed_fields.py Outdated Show resolved Hide resolved
sydney-runkle and others added 2 commits September 5, 2023 15:25
Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
@adriangb adriangb enabled auto-merge (squash) September 5, 2023 20:30
Copy link
Member

@adriangb adriangb left a comment

Choose a reason for hiding this comment

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

Approving since this error is at least less confusing than the current behavior. We can continue the discussion in #7250 if erroring doesn't seem like an acceptable approach.

@adriangb adriangb merged commit d78afb5 into pydantic:main Sep 5, 2023
47 checks passed
@davidhewitt davidhewitt added the relnotes-fix Used for bugfixes. label Sep 21, 2023
@silky
Copy link

silky commented Mar 26, 2024

It's moderately unfortunate that this behaviour is not available when it is available with pure dataclasses (despite the mypy warning.)

Is there any thoughts of a work-around? I.e. to override an inheritied property; with a computed field or otherwise?

-- Edit:

A related observation is that if you merely removed the @computed_field tag for the field of interest, you no longer get a warning, you merely get that your re-defined field is never evaluated; which is probably still at least a bug. I.e. the central behaviour that isn't supported is this style of overriding!

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.

Failure to override an inherited field with a computed field
4 participants