[Python] How can I reference the type hint for an instance variable on another class?

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    The OpenAI Python types define a Batch class with a status field:

    class Batch(BaseModel):
    id: str

    # ...

    status: Literal[
    "validating", "failed", "in_progress", "finalizing", "completed", "expired", "cancelling", "cancelled"
    ]
    """The current status of the batch."""


    I'd like to re-use the Literal[...] type hint from the status field in my own class. Here's an attempt:

    from typing import Optional, TypedDict
    import openai

    class FileStatus(TypedDict):
    filename: str
    sha256: str
    file_id: Optional[str]
    batch_id: Optional[str]
    batch_status: Optional[openai.types.Batch.status]
    # ~~~~~~
    # Variable not allowed in type expression Pylance(reportInvalidTypeForm)


    What should I use as the type hint for batch_status? I'd prefer to avoid copy/pasting the Literal[...] expression from the OpenAI API if possible.

    Continue reading...

Compartilhe esta Página