-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Support Pydantic model attributes #9
Comments
I'm favorable to this. I'll just have to play with pydantic a bit before tackling it. |
I'm actually very interested in this. Once I can get my head around where you're going with the refactor, I'm more than happy to tackle this. |
Great! So, as I said in the refactor issue, I'll write a proper summary of how |
It may be possible to utilize the JSON Schema generation for Pydantic models as has been done with sphinx-pydantic[1] via sphinx-jsonschema[2]. From Model Properties:
[1] https://pypi.org/project/sphinx-pydantic/ |
FWIW: After testing sphinx-pydantic (which is light a wrapper around sphinx-jsonschema), I wish a more robust approach were available. Sphinx's autodoc plugin is very good about creating linked references to inherited models, related models, params, and a host of other well-worn doc features which are elided/unavailable/ugly in the jsonschema approach. json_schema is great for many things, but it can quickly become unreadable. Self-documenting pydantic models with just a little more functionality than the json_schema docs provide would be extremely useful when working with other dev teams, PMs, etc. A Sphinx pre-processor plugin would probably get the job done with zero run time overhead, but populating docstrings on Fields() would be useful for many other static analysis libs that rely on robust docstring information. |
If you don't mind, I'll transfer the issue over to pytkdocs, since it's the project that is now responsible for loading documentation from Python code! |
@shyamd now that the refactor is done, you're free to try and come up with a PR 🙂 |
So both |
Not at all! This should simply be properly commented / documented 🙂 |
Might want to start with the more generic approach, being able to knock out both stdlib dataclasses and Pydantic models would be a great benefit! |
I think that's what @shyamd meant?
|
Hi @StephenBrown2, @demospace! @shyamd just got their pull request merged in master! If you want to try them now, before I make a new release, you can simply |
So I tried real hard, but it seems that it's not working in my main project, but it does work on a minimal demo project. Probably something on my end, but it's a good start! Also, that project is getting sunset, so I won't be able to test it out much there. I think that this is probably good to go, since it works in a fresh environment. I would prefer that it gathered doc-comments as well if it didn't get the FieldInfo, since dataclasses also don't have a description, but that's not critical for a first release. |
About dataclasses fields not having a description, yeah, we decided to do that in another PR, because it needs a refactoring of the attributes parser. I'll go ahead and put this in the "to release" column 🙂 |
Released in v0.4.0. Please open new issues for any encountered problem with this feature! |
I'm using Pydantic for several models in my code, and I would like mkdocstrings to handle them. The issue is, class attributes get sucked up into a
__fields__
attribute with separate__annotations__
, and so they get filtered out byfilter_name_out
due to theglobal_filters
.Example
__dict__.items()
on a Pydantic class with two fields (debugged from just before https://github.com/pawamoy/mkdocstrings/blob/ca82f6964e223b7d8b0a3d0e0fe4d9ea8f4296ce/src/mkdocstrings/documenter.py#L340):A crude implementation for an older version of pydantic was put forward here: pydantic/pydantic#638 but not accepted into Pydantic proper. Perhaps mkdocstrings could take some of the logic and parse through the field attributes?
Update: I should probably also note that I started down this path by attempting to add some docstrings to the attributes as suggested by mkdocstrings docs, but then got this error:
Changing
node.target.attr
tonode.target.id
fixed that for meavoided the error, but I'm not sure how relevant it is, just means I can't docstring my pydantic models (yet) since it's filtering the dunder-attrs.The text was updated successfully, but these errors were encountered: