Find answers from the community

Updated 3 months ago

Why not `possible_entities = Optional[

Why not possible_entities = Optional[List[str]]????
Attachment
image.png
L
A
9 comments
Its not List[str] because its supposed to be

Plain Text
from typing import Literal

....
possible_relations=Literal["one", "two", ...],
Maybe related to the version of python you are using?
I've never run into this issue actually
Yea it was my Python version
But why using Literals for that? To make the code more verbose or clear to understand?
So, under the hood its using create_model() from pydantic to create a pydantic model on the fly, and it needs a python type to properly create the field for the pydantic model

However, python does not support creating types on the fly like
Plain Text
def create(possible_realtions: List[str]):
  Literal[possible_relations]  # <- Failure


So, stuck with making the input a Literal type
Thanks. I see now. Have you tried msgspec?
May help if I'm not wrong it supports creating an object from a regular Python type
class Person(Struct):
name: str


msgspec.iforgotthefunction(["arthur"], type=Person)
Add a reply
Sign up and join the conversation on Discord