mkdocstrings / pytkdocs

Load Python objects documentation.
https://mkdocstrings.github.io/pytkdocs
ISC License
50 stars 32 forks source link

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

Closed shashankrnr32 closed 3 years ago

shashankrnr32 commented 3 years ago

Describe the bug pydantic fields with _outertype (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):

Additional context

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

https://github.com/mkdocstrings/pytkdocs/blob/bf04764f1608970643932329c9f6c8c63a0c5632/src/pytkdocs/loader.py#L682

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)

pawamoy commented 3 years ago

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