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

[BUG] Pydantic Field documentation generates wrong type for List/Set/Tuple #94

Closed
shashankrnr32 opened this issue Feb 20, 2021 · 1 comment · Fixed by #95
Closed

[BUG] Pydantic Field documentation generates wrong type for List/Set/Tuple #94

shashankrnr32 opened this issue Feb 20, 2021 · 1 comment · Fixed by #95

Comments

@shashankrnr32
Copy link
Contributor

shashankrnr32 commented Feb 20, 2021

Describe the bug
pydantic fields with outer_type (Eg. typing.List, typing.Set etc) are populated with the wrong type in documentation. The documentation shows only the inner type. (See example)

To Reproduce

from typing import List, Union
from pydantic import BaseModel, Field

class MyModel(BaseModel):
    """
    MyModel
    """
    attr_bug: List[str] = Field(..., description="This is a mock attribute that is buggy")
    attr_perfect: Union[List[str], List[int]] = Field(description="This is a mock attribute that is perfect")
  1. The type information of the attr_bug shows up as str and not List[str] (which is expected)
  2. The type information of the attr_perfect shows up as Union[List[str], List[int]] as expected

Expected behavior

Explained above below the code example.

Screenshots
Attaching a screenshot of the documentation produced by mkdocstrings with the above example code

Screenshot 2021-02-20 at 9 52 40 AM

System (please complete the following information):

  • pytkdocs version : 0.10.1
  • mkdocstrings version : 0.14.0
  • Python version: 3.6.1 (I know I should update!!)
  • OS: macOS

Additional context

According to my quick run through the code of pytkdocs and pydantic, the reason is most probably because of the below line.

attr_type=prop.type_,

Instead of the above line, it should have been prop.outer_type_ (that is provided by pydantic here).

Debug code

print(MyModel.__dict__["__fields__"]["attr_bug"].type_)
# prints <class 'str'>

print(MyModel.__dict__["__fields__"]["attr_bug"].outer_type_)
# prints typing.List[str]

Lastly, thank you very much for creating mkdocstrings (and pytkdocs)

shashankrnr32 added a commit to shashankrnr32/pytkdocs that referenced this issue Feb 20, 2021
Change `prop.type_` to `prop.outer_type_` to get the
correct type of the pydantic field when using List/Set/Tuple/Union

Optional[Type] does not work because of pydantic's internal
implementation to change outer_type

Fixes mkdocstrings#94
@pawamoy
Copy link
Member

pawamoy commented Feb 24, 2021

Hello, thank you very much for the detailed bug report and the PR 🙂 I've left a comment on the PR.

shashankrnr32 added a commit to shashankrnr32/pytkdocs that referenced this issue Feb 25, 2021
Change `prop.type_` to `prop.outer_type_` to get the
correct type of the pydantic field when using List/Set/Tuple/Union

Optional[Type] does not work because of pydantic's internal
implementation to change outer_type

Fixes mkdocstrings#94
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 a pull request may close this issue.

2 participants