Skip to content

Conversation

@matsjoyce-refeyn
Copy link
Contributor

@matsjoyce-refeyn matsjoyce-refeyn commented Feb 7, 2024

Change Summary

(Text mostly copied from #8730) When the base class of a dataclass has an annotation type that is not imported by the module the subclass is in, and from __future__ import annotations is in use (or in my case, Cython is in use), an error occurs.

The root cause is that when pydantic builds up the type namespace, it does not consider base classes (https://github.com/pydantic/pydantic/blob/main/pydantic/_internal/_generate_schema.py#L1474). This is fine for models, because each base class builds a schema immediately, but does not work for plain stdlib dataclasses. This PR adds code to iterate though all of the base classes of the dataclass that are also dataclasses, and push their namespaces as well, which should cover all of the fields which Pydantic will attempt to resolve.

Related issue number

Fixes #8730

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

Selected Reviewer: @alexmojaki

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 7, 2024

CodSpeed Performance Report

Merging #8751 will not alter performance

Comparing matsjoyce-refeyn:bugfix/pydantic-8730-fix-base-dataclass-forward-refs-unknown-error (daa2ba8) with main (825a692)

Summary

✅ 10 untouched benchmarks

@matsjoyce-refeyn
Copy link
Contributor Author

The only job failing on CI is the tags check, so please review

Copy link
Contributor

@alexmojaki alexmojaki left a comment

Choose a reason for hiding this comment

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

Thank you, this looks promising!

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Feb 7, 2024
@matsjoyce-refeyn
Copy link
Contributor Author

All comments have been addressed, please review (looks like hooky doesn't react to rereview requests).

@pydantic-hooky pydantic-hooky bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Feb 7, 2024
@alexmojaki alexmojaki added the relnotes-fix Used for bugfixes. label Feb 7, 2024
Copy link
Contributor

@alexmojaki alexmojaki left a comment

Choose a reason for hiding this comment

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

Great, thanks!

@matsjoyce-refeyn
Copy link
Contributor Author

@alexmojaki Just to check, is there anything else I need to do to get this merged?

@alexmojaki alexmojaki requested a review from dmontagu February 13, 2024 12:19
@sydney-runkle
Copy link
Contributor

I've tested out using swapped orders for traversing the MROs, and I think the current approach makes sense. There were test failures for all other options re MRO traversal @dmontagu

@sydney-runkle sydney-runkle merged commit 4672662 into pydantic:main Feb 13, 2024
ashb added a commit to apache/airflow that referenced this pull request Oct 30, 2024
ashb added a commit to apache/airflow that referenced this pull request Oct 30, 2024
ashb added a commit to apache/airflow that referenced this pull request Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type annotations on dataclass base class cannot be resolved if future annotations are used

4 participants