Skip to content
Merged
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,4 @@
from typing import Literal, cast
from typing import Literal

AnAllOfEnum = Literal["a_default", "bar", "foo", "overridden_default"]

Expand All @@ -12,5 +12,5 @@

def check_an_all_of_enum(value: str) -> AnAllOfEnum:
if value in AN_ALL_OF_ENUM_VALUES:
return cast(AnAllOfEnum, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ALL_OF_ENUM_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

AnEnum = Literal["FIRST_VALUE", "SECOND_VALUE"]

Expand All @@ -10,5 +10,5 @@

def check_an_enum(value: str) -> AnEnum:
if value in AN_ENUM_VALUES:
return cast(AnEnum, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ENUM_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

AnEnumWithNull = Literal["FIRST_VALUE", "SECOND_VALUE"]

Expand All @@ -10,5 +10,5 @@

def check_an_enum_with_null(value: str) -> AnEnumWithNull:
if value in AN_ENUM_WITH_NULL_VALUES:
return cast(AnEnumWithNull, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ENUM_WITH_NULL_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

AnIntEnum = Literal[-1, 1, 2]

Expand All @@ -11,5 +11,5 @@

def check_an_int_enum(value: int) -> AnIntEnum:
if value in AN_INT_ENUM_VALUES:
return cast(AnIntEnum, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_INT_ENUM_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

DifferentEnum = Literal["DIFFERENT", "OTHER"]

Expand All @@ -10,5 +10,5 @@

def check_different_enum(value: str) -> DifferentEnum:
if value in DIFFERENT_ENUM_VALUES:
return cast(DifferentEnum, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {DIFFERENT_ENUM_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

GetUserListIntEnumHeader = Literal[1, 2, 3]

Expand All @@ -11,5 +11,5 @@

def check_get_user_list_int_enum_header(value: int) -> GetUserListIntEnumHeader:
if value in GET_USER_LIST_INT_ENUM_HEADER_VALUES:
return cast(GetUserListIntEnumHeader, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {GET_USER_LIST_INT_ENUM_HEADER_VALUES!r}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, cast
from typing import Literal

GetUserListStringEnumHeader = Literal["one", "three", "two"]

Expand All @@ -11,5 +11,5 @@

def check_get_user_list_string_enum_header(value: str) -> GetUserListStringEnumHeader:
if value in GET_USER_LIST_STRING_ENUM_HEADER_VALUES:
return cast(GetUserListStringEnumHeader, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {GET_USER_LIST_STRING_ENUM_HEADER_VALUES!r}")
22 changes: 14 additions & 8 deletions end_to_end_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import shutil
import subprocess
import sys
from filecmp import cmpfiles, dircmp
from pathlib import Path

Expand Down Expand Up @@ -99,10 +101,12 @@ def run_e2e_test(
gr_path, g.output_path, expected_differences=expected_differences, expected_missing=expected_missing
)

import mypy.api

out, err, status = mypy.api.run([str(g.output_path), "--strict"])
assert status == 0, f"Type checking client failed: {out}"
result = subprocess.run(
[sys.executable, "-m", "mypy", str(g.output_path), "--strict"],
capture_output=True,
text=True,
)
assert result.returncode == 0, f"Type checking client failed: {result.stdout}"

return g.generator_result

Expand Down Expand Up @@ -281,10 +285,12 @@ def test_update_integration_tests():
config_path=config_path
)
_compare_directories(source_path, temp_dir, ignore=["pyproject.toml"])
import mypy.api

out, err, status = mypy.api.run([str(temp_dir), "--strict"])
assert status == 0, f"Type checking client failed: {out=} {err=}"
result = subprocess.run(
[sys.executable, "-m", "mypy", str(temp_dir), "--strict"],
capture_output=True,
text=True,
)
assert result.returncode == 0, f"Type checking client failed: {result.stdout=} {result.stderr=}"

finally:
shutil.rmtree(temp_dir)
442 changes: 249 additions & 193 deletions integration-tests/pdm.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions openapi_python_client/templates/literal_enum.py.jinja
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Literal, cast
from typing import Literal

{{ enum.class_info.name }} = Literal{{ "%r" | format(enum.values|list|sort) }}

{{ enum.get_class_name_snake_case() | upper }}_VALUES: set[{{ enum.class_info.name }}] = { {% for v in enum.values|list|sort %}{{"%r"|format(v)}}, {% endfor %} }

def check_{{ enum.get_class_name_snake_case() }}(value: {{ enum.get_instance_type_string() }}) -> {{ enum.class_info.name}}:
if value in {{ enum.get_class_name_snake_case() | upper }}_VALUES:
return cast({{enum.class_info.name}}, value)
return value
raise TypeError(f"Unexpected value {value!r}. Expected one of {{"{"}}{{ enum.get_class_name_snake_case() | upper }}_VALUES!r}")
Loading
Loading