Skip to content

Commit 90afc56

Browse files
Copilotdbanty
andauthored
fix: update code for mypy v2 compatibility
Agent-Logs-Url: https://github.com/openapi-generators/openapi-python-client/sessions/fcf228f8-9c17-45a7-a11e-f87474279b51 Co-authored-by: dbanty <43723790+dbanty@users.noreply.github.com>
1 parent 04e02d6 commit 90afc56

9 files changed

Lines changed: 30 additions & 24 deletions

File tree

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/an_all_of_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

@@ -12,5 +12,5 @@
1212

1313
def check_an_all_of_enum(value: str) -> AnAllOfEnum:
1414
if value in AN_ALL_OF_ENUM_VALUES:
15-
return cast(AnAllOfEnum, value)
15+
return value
1616
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ALL_OF_ENUM_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/an_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

@@ -10,5 +10,5 @@
1010

1111
def check_an_enum(value: str) -> AnEnum:
1212
if value in AN_ENUM_VALUES:
13-
return cast(AnEnum, value)
13+
return value
1414
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ENUM_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/an_enum_with_null.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

@@ -10,5 +10,5 @@
1010

1111
def check_an_enum_with_null(value: str) -> AnEnumWithNull:
1212
if value in AN_ENUM_WITH_NULL_VALUES:
13-
return cast(AnEnumWithNull, value)
13+
return value
1414
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_ENUM_WITH_NULL_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/an_int_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

33
AnIntEnum = Literal[-1, 1, 2]
44

@@ -11,5 +11,5 @@
1111

1212
def check_an_int_enum(value: int) -> AnIntEnum:
1313
if value in AN_INT_ENUM_VALUES:
14-
return cast(AnIntEnum, value)
14+
return value
1515
raise TypeError(f"Unexpected value {value!r}. Expected one of {AN_INT_ENUM_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/different_enum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

@@ -10,5 +10,5 @@
1010

1111
def check_different_enum(value: str) -> DifferentEnum:
1212
if value in DIFFERENT_ENUM_VALUES:
13-
return cast(DifferentEnum, value)
13+
return value
1414
raise TypeError(f"Unexpected value {value!r}. Expected one of {DIFFERENT_ENUM_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/get_user_list_int_enum_header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

33
GetUserListIntEnumHeader = Literal[1, 2, 3]
44

@@ -11,5 +11,5 @@
1111

1212
def check_get_user_list_int_enum_header(value: int) -> GetUserListIntEnumHeader:
1313
if value in GET_USER_LIST_INT_ENUM_HEADER_VALUES:
14-
return cast(GetUserListIntEnumHeader, value)
14+
return value
1515
raise TypeError(f"Unexpected value {value!r}. Expected one of {GET_USER_LIST_INT_ENUM_HEADER_VALUES!r}")

end_to_end_tests/literal-enums-golden-record/my_enum_api_client/models/get_user_list_string_enum_header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

@@ -11,5 +11,5 @@
1111

1212
def check_get_user_list_string_enum_header(value: str) -> GetUserListStringEnumHeader:
1313
if value in GET_USER_LIST_STRING_ENUM_HEADER_VALUES:
14-
return cast(GetUserListStringEnumHeader, value)
14+
return value
1515
raise TypeError(f"Unexpected value {value!r}. Expected one of {GET_USER_LIST_STRING_ENUM_HEADER_VALUES!r}")

end_to_end_tests/test_end_to_end.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import shutil
2+
import subprocess
3+
import sys
24
from filecmp import cmpfiles, dircmp
35
from pathlib import Path
46

@@ -99,10 +101,12 @@ def run_e2e_test(
99101
gr_path, g.output_path, expected_differences=expected_differences, expected_missing=expected_missing
100102
)
101103

102-
import mypy.api
103-
104-
out, err, status = mypy.api.run([str(g.output_path), "--strict"])
105-
assert status == 0, f"Type checking client failed: {out}"
104+
result = subprocess.run(
105+
[sys.executable, "-m", "mypy", str(g.output_path), "--strict"],
106+
capture_output=True,
107+
text=True,
108+
)
109+
assert result.returncode == 0, f"Type checking client failed: {result.stdout}"
106110

107111
return g.generator_result
108112

@@ -281,10 +285,12 @@ def test_update_integration_tests():
281285
config_path=config_path
282286
)
283287
_compare_directories(source_path, temp_dir, ignore=["pyproject.toml"])
284-
import mypy.api
285-
286-
out, err, status = mypy.api.run([str(temp_dir), "--strict"])
287-
assert status == 0, f"Type checking client failed: {out=} {err=}"
288+
result = subprocess.run(
289+
[sys.executable, "-m", "mypy", str(temp_dir), "--strict"],
290+
capture_output=True,
291+
text=True,
292+
)
293+
assert result.returncode == 0, f"Type checking client failed: {result.stdout=} {result.stderr=}"
288294

289295
finally:
290296
shutil.rmtree(temp_dir)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from typing import Literal, cast
1+
from typing import Literal
22

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

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

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

0 commit comments

Comments
 (0)