Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514-thinking-32k· AnthropicHugging Face LMArena Human Preference 140K

In Python how may I access the docstrings of the attributes of TypedDict?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    In Python how may I access the docstrings of the attributes of TypedDict?

  2. Claude Assistant

    TypedDict doesn't natively support docstrings for individual attributes/keys like regular Python classes do. The "attributes" in a TypedDict are type annotations for dictionary keys, not actual class attributes that can have docstrings.

    However, here are several approaches you can use:

    Using Annotated for Documentation

    from typing import TypedDict, Annotated
    from typing_extensions import Doc  # Python 3.8+, or use typing.Annotated in 3.9+
    
    class Person(TypedDict):
        name: Annotated[str, Doc("The person's full name")]
        age: Annotated[int, Doc("Age in years")]
        email: Annotated[str, Doc("Email address")]
    
    # Access the documentation
    import typing
    annotations = typing.get_type_hints(Person, include_extras=True)
    for key, annotation in annotations.items():
        if hasattr(annotation, '__metadata__'):
            for meta in annotation.__metadata__:
                if hasattr(meta, '__doc__') or isinstance(meta, str):
                    print(f"{key}: {meta}")
    

    Using Custom Documentation Structure

    from typing import TypedDict
    
    class Person(TypedDict):
        name: str
        age: int
        email: str
    
    # Separate documentation
    PERSON_DOCS = {
        'name': "The person's full name",
        'age': "Age in years", 
        'email': "Email address"
    }
    
    def get_field_doc(typed_dict_class, field_name):
        # You'd need to maintain this mapping
        doc_map = {Person: PERSON_DOCS}
        return doc_map.get(typed_dict_class, {}).get(field_name)
    

    Using dataclasses or Pydantic Instead

    If you need rich documentation, consider alternatives:

    from dataclasses import dataclass
    
    @dataclass
    class Person:
        """A person record."""
        name: str  #: The person's full name
        age: int   #: Age in years
        email: str #: Email address
    
    # Or with Pydantic
    from pydantic import BaseModel, Field
    
    class Person(BaseModel):
        name: str = Field(description="The person's full name")
        age: int = Field(description="Age in years")
        email: str = Field(description="Email address")
    

    The Annotated approach with Doc is probably the closest to what you're looking for while still using TypedDict, though it requires some extra work to extract the documentation programmatically.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.