Skip to content

Add support for Pydantic models in stubgen #19095

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

teplandr
Copy link

@teplandr teplandr commented May 16, 2025

This PR adds support for generating stubs for Pydantic models in stubgen.

Changes

  • Added detection of classes that inherit from pydantic.BaseModel directly or indirectly
  • Enhanced the stub generation process to add appropriate initializers (= ...) to field annotations in Pydantic models
  • Added comprehensive test cases for different Pydantic model scenarios

Fixes #16968

@teplandr teplandr changed the title Add support for Pydantic models in ASTStubGenerator Add support for Pydantic models in stubgen May 20, 2025
@teplandr teplandr marked this pull request as ready for review May 20, 2025 11:03
@teplandr
Copy link
Author

Hello @JelleZijlstra! Hope you're doing well 🙂
I saw that you recently reviewed PR with stubgen improvements. Could you please take a look at proposed changes?
Thanks in advance!

@teplandr
Copy link
Author

Hello @hauntsaninja! Hope you're doing well!
Could you please review the proposed changes, or suggest someone who can?

@teplandr
Copy link
Author

Hi @sobolevn! You're my last hope 🙂
Could you please review the proposed changes?

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks! One main comment :)


class User(BaseUser):
name: str
email: str
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
email: str
email: str = '@'

Copy link
Author

Choose a reason for hiding this comment

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

There is an issue connected with this change. Currently, the code only sets processing_pydantic_model = True when the direct base class is pydantic.BaseModel, but it doesn't handle cases where a class inherits from another class that inherits from pydantic.BaseModel. I tried a few ideas but they aren't working. Could you please give me a clue how to tackle the problem?

name: str
email: str

[case testPydanticModelWithMethods]
Copy link
Member

Choose a reason for hiding this comment

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

We can just add some methods to existing models. Let's reduce the amount of tests without reducing test features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect stub generation for subclasses of pydantic.BaseModel
2 participants