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

feat(python): Support wrapped aliases for Pydantic v2 #5115

Conversation

matthew-giglio
Copy link
Contributor

@matthew-giglio matthew-giglio commented Nov 5, 2024

  • Wrapped aliases are not supported for v2
  • We need to support both the wrapped aliases themselves along with validators for them
  • The v2 wrapped aliases leverage RootModel and model_validator on their validators
  • RootModel does not support extra as a keyword nor does it support frozen behavior well`, so we omit those from the model behavior here
  • Tested by generating Pydantic v1 and v2 wrapped aliases with validators
  • We don't support this for "both" (have tests demonstrating) since there's no great in between model that works well between both versions

@matthew-giglio matthew-giglio changed the base branch from main to dsinghvi/v2-wrapped-aliases November 5, 2024 16:22
module_path=("pydantic",),
),
),
import_=Pydantic.PydanticImport(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this for easier reference in other codepaths

Comment on lines +118 to +126
def _model_validator(self, pre: bool = False) -> AST.FunctionInvocation:
return AST.FunctionInvocation(
function_definition=AST.Reference(
qualified_name_excluding_import=("model_validator",),
import_=Pydantic.PydanticImport(),
),
kwargs=[("mode", AST.Expression(expression='"before"' if pre else '"after"'))],
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't like having this as part of the class, but I also don't know where you store utility files in your repo. Feel free to move!

@dsinghvi dsinghvi merged commit c0409db into fern-api:dsinghvi/v2-wrapped-aliases Nov 5, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants