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

Fix resolution of forward refs in dataclass base classes that are not present in the subclass module namespace #8751

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

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/_internal/_generate_schema.py Outdated Show resolved Hide resolved
pydantic/_internal/_generate_schema.py Show resolved Hide resolved
tests/test_dataclasses.py Outdated Show resolved Hide resolved
tests/test_dataclasses.py Outdated Show resolved Hide resolved
@matsjoyce-refeyn
Copy link
Contributor Author

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

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?

@sydney-runkle
Copy link
Member

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
53 checks passed
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