'Using type() with Annotations in python

I am trying to learn how to use annotations.

I have this class:

class ApiItemPydanticWithTags(ApiItemPydantic):
    tags: str
ApiItemPydanticWithTags.__name__ = "ApiWIthTags"

I want to replicate this class creation with type():

ApiItemPydanticWithTags = type("ApiWIthTags", (ApiItemPydantic,), dict(tags: str = None))

But Obviously, dict(tags: str = None) does not work ...



Solution 1:[1]

I found out what I wanted to do with:

ApiItemPydanticWithTags = type(
    "ApiWIthTags", (ApiItemPydantic,), dict(__annotations__={"tags": str})
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SlimBeji