Skip to content

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

@shashankrnr32

Description

@shashankrnr32

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions