Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-7-sonnet-20250219-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-sonnet-4-20250514-thinking-32k’s answer.
  1. User

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

  2. Claude Assistant

    In Python, TypedDict fields don't have traditional docstrings that can be accessed programmatically, as they're dictionary keys rather than true attributes. Here's how you can work with documentation in TypedDicts:

    from typing import TypedDict, get_type_hints
    
    class Person(TypedDict):
        """TypedDict representing a person."""
        name: str  # These are comments, not accessible docstrings
        age: int
        
    # You can access the TypedDict class docstring
    print(Person.__doc__)
    
    # You can see field types, but not their documentation
    print(get_type_hints(Person))
    

    If you need documented fields, you might create a custom solution like:

    from typing import TypedDict, Dict
    
    class Person(TypedDict):
        name: str
        age: int
    
    # Store field documentation separately
    FIELD_DOCS: Dict[str, str] = {
        "name": "The person's full name",
        "age": "The person's age in years"
    }
    
    # Function to retrieve documentation
    def get_field_doc(field_name: str) -> str:
        return FIELD_DOCS.get(field_name, "No documentation available")
    

    Would you like me to explain or break down this code?

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