Skip to content

Add literals alongside enums #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from typing import Literal


class AllOfHasPropertiesButNoTypeTypeEnum(IntEnum):
Expand All @@ -7,3 +8,9 @@ class AllOfHasPropertiesButNoTypeTypeEnum(IntEnum):

def __str__(self) -> str:
return str(self.value)


AllOfHasPropertiesButNoTypeTypeEnumLiteral = Literal[
0,
1,
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from typing import Literal


class AllOfSubModelTypeEnum(IntEnum):
Expand All @@ -7,3 +8,9 @@ class AllOfSubModelTypeEnum(IntEnum):

def __str__(self) -> str:
return str(self.value)


AllOfSubModelTypeEnumLiteral = Literal[
0,
1,
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Literal


class AnAllOfEnum(str, Enum):
Expand All @@ -9,3 +10,11 @@ class AnAllOfEnum(str, Enum):

def __str__(self) -> str:
return str(self.value)


AnAllOfEnumLiteral = Literal[
"a_default",
"bar",
"foo",
"overridden_default",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Literal


class AnEnum(str, Enum):
Expand All @@ -7,3 +8,9 @@ class AnEnum(str, Enum):

def __str__(self) -> str:
return str(self.value)


AnEnumLiteral = Literal[
"FIRST_VALUE",
"SECOND_VALUE",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Literal


class AnEnumWithNull(str, Enum):
Expand All @@ -7,3 +8,9 @@ class AnEnumWithNull(str, Enum):

def __str__(self) -> str:
return str(self.value)


AnEnumWithNullLiteral = Literal[
"FIRST_VALUE",
"SECOND_VALUE",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from typing import Literal


class AnIntEnum(IntEnum):
Expand All @@ -8,3 +9,10 @@ class AnIntEnum(IntEnum):

def __str__(self) -> str:
return str(self.value)


AnIntEnumLiteral = Literal[
-1,
1,
2,
]
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from enum import Enum
from typing import Literal


class AnotherAllOfSubModelType(str, Enum):
SUBMODEL = "submodel"

def __str__(self) -> str:
return str(self.value)


AnotherAllOfSubModelTypeLiteral = Literal["submodel",]
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from enum import IntEnum
from typing import Literal


class AnotherAllOfSubModelTypeEnum(IntEnum):
VALUE_0 = 0

def __str__(self) -> str:
return str(self.value)


AnotherAllOfSubModelTypeEnumLiteral = Literal[0,]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Literal


class DifferentEnum(str, Enum):
Expand All @@ -7,3 +8,9 @@ class DifferentEnum(str, Enum):

def __str__(self) -> str:
return str(self.value)


DifferentEnumLiteral = Literal[
"DIFFERENT",
"OTHER",
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import IntEnum
from typing import Literal


class GetLocationHeaderTypesIntEnumHeader(IntEnum):
Expand All @@ -8,3 +9,10 @@ class GetLocationHeaderTypesIntEnumHeader(IntEnum):

def __str__(self) -> str:
return str(self.value)


GetLocationHeaderTypesIntEnumHeaderLiteral = Literal[
1,
2,
3,
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Literal


class GetLocationHeaderTypesStringEnumHeader(str, Enum):
Expand All @@ -8,3 +9,10 @@ class GetLocationHeaderTypesStringEnumHeader(str, Enum):

def __str__(self) -> str:
return str(self.value)


GetLocationHeaderTypesStringEnumHeaderLiteral = Literal[
"one",
"three",
"two",
]
9 changes: 9 additions & 0 deletions openapi_python_client/templates/int_enum.py.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from enum import IntEnum
from typing import Literal


class {{ enum.class_info.name }}(IntEnum):
{% for key, value in enum.values.items() %}
Expand All @@ -7,3 +9,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 %}
]
9 changes: 9 additions & 0 deletions openapi_python_client/templates/str_enum.py.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from enum import Enum
from typing import Literal


class {{ enum.class_info.name }}(str, Enum):
{% for key, value in enum.values|dictsort(true) %}
Expand All @@ -7,3 +9,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 %}
]
Loading