Skip to content

Standardize docstrings and improve coverage #21983

Closed as not planned
Closed as not planned
@baskaryan

Description

@baskaryan

Issue

Every public module, class, method and attribute should have a docstring.

Requirements

NOTE!

RST code block must have a newline between .. code-block:: python and the code example, and code example must be tabbed, to render correctly!

Examples

Module

langchain/foo/__init__.py

""""One line summary.

More detailed paragraph.
"""

Class and attributes

Not Pydantic class

class Foo:
  """One line summary.

  More detailed paragraph.
  
  Attributes:
    first_attr: does first thing.
    second_attr: does second thing.

  Example:
    .. code-block:: python
    
      from langchain.foo import Foo
  
      f = Foo(1, 2, "bar")
      ...
  """
  
  def __init__(self, a: int, b: int, c: str) -> None:
    """Initialize using a, b, c.
   
    Args:
      a: ...
      b: ...
      c: ...
   """
    self.first_attr = a + b
    self.second_attr = c

NOTE

If the object attributes and init args are the same then you can just document the init args for non-Pydantic classes and just document the attributes for Pydantic classes.

Pydantic class

from typing import Any

from langchain_core.base_models import BaseModel

class FooPydantic(BaseModel):
  """One line summary.

  More detailed paragraph.

  Example:
    .. code-block:: python
    
      from langchain.foo import Foo
  
      f = Foo(1, 2, "bar")
      ...
  """

  first_attr: int
  """Does first thing."""
  second_attr: str
  """Does second thing.

  Additional info if needed.
  """

    def __init__(self, a: int, b: int, c: str, **kwargs: Any) -> None:
    """Initialize using a, b, c.
   
    Args:
      a: ...
      b: ...
      c: ...
      **kwargs: ...
   """
    first_attr = a + b
    second_attr = c
    super().__init__(first_attr=first_attr, second_attr=second_attr, **kwargs)

Function/method

def bar(a: int, b: str) -> float:
  """One line description.

  More description if needed.

  Args:
    a: ...
    b: ...

  Returns:
    A float of ...

  Raises:
    ValueError: If a is negative.

  Example:
    .. code-block:: python
      
      from langchain.foo import bar

      bar(1, "foo")
      # -> 14.381
  """

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomershelp wantedGood issue for contributors🤖:docsChanges to documentation and examples, like .md, .rst, .ipynb files. Changes to the docs/ folder

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions