From 9f93f9abe58e5c2604f02b3b2c852610b28d8213 Mon Sep 17 00:00:00 2001 From: Roman Kalyakin Date: Fri, 29 Mar 2024 13:48:22 +0100 Subject: [PATCH] Add literals alongside enums --- openapi_python_client/templates/int_enum.py.jinja | 8 ++++++++ openapi_python_client/templates/str_enum.py.jinja | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/openapi_python_client/templates/int_enum.py.jinja b/openapi_python_client/templates/int_enum.py.jinja index a508f1c8e..a20390daa 100644 --- a/openapi_python_client/templates/int_enum.py.jinja +++ b/openapi_python_client/templates/int_enum.py.jinja @@ -1,3 +1,4 @@ +from typing import Literal from enum import IntEnum class {{ enum.class_info.name }}(IntEnum): @@ -7,3 +8,10 @@ class {{ enum.class_info.name }}(IntEnum): def __str__(self) -> str: return str(self.value) + + +{{ enum.class_info.name }}Literal = Literal[ + {% for _, value in enum.values.items() %} + {{ value }}, + {% endfor %} +] diff --git a/openapi_python_client/templates/str_enum.py.jinja b/openapi_python_client/templates/str_enum.py.jinja index e0da5ed0f..b095d717c 100644 --- a/openapi_python_client/templates/str_enum.py.jinja +++ b/openapi_python_client/templates/str_enum.py.jinja @@ -1,3 +1,4 @@ +from typing import Literal from enum import Enum class {{ enum.class_info.name }}(str, Enum): @@ -7,3 +8,10 @@ class {{ enum.class_info.name }}(str, Enum): def __str__(self) -> str: return str(self.value) + + +{{ enum.class_info.name }}Literal = Literal[ + {% for _, value in enum.values|dictsort(true) %} + "{{ value }}", + {% endfor %} +]