Skip to content

refactor(python): use built-in type hinting generics#22925

Open
timonrieger wants to merge 3 commits intoOpenAPITools:masterfrom
timonrieger:builtin-types
Open

refactor(python): use built-in type hinting generics#22925
timonrieger wants to merge 3 commits intoOpenAPITools:masterfrom
timonrieger:builtin-types

Conversation

@timonrieger
Copy link
Contributor

@timonrieger timonrieger commented Feb 9, 2026

these are supported from 3.9 onwards, matching the current python constraint, so non-breaking

https://stackoverflow.com/questions/37087457/difference-between-defining-typing-dict-and-dict#37087556
https://peps.python.org/pep-0585/


Summary by cubic

Refactor Python templates and generated samples to use built-in generics (dict/list/tuple/set) per PEP 585. Simplifies type hints, removes extra typing imports, and keeps Python 3.9+ compatibility with no behavior changes.

  • Refactors
    • Replaced typing.Dict/List/Tuple/Set with dict/list/tuple/set in templates (api, api_client, configuration, models, signing) and regenerated samples.
    • Updated type aliases and annotations (e.g., RequestSerialized, ServerVariablesT, HostSetting.variables, response_types_map, any_of/one_of sets/maps) to built-in generics.
    • Removed now-unneeded typing imports and updated tests to match the new hints.

Written for commit 6b50ed1. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 477 files

Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed.

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py">

<violation number="1" location="samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py:444">
P2: Deserializer now only recognizes lowercase `list[`/`dict[` strings, but response type maps still emit `List[...]`/`Dict[...]`, causing AttributeError or incorrect deserialization.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment on lines +444 to 456
if klass.startswith('list['):
m = re.match(r'list\[(.*)]', klass)
assert m is not None, "Malformed list type definition"
sub_kls = m.group(1)
return [self.__deserialize(sub_data, sub_kls)
for sub_data in data]

if klass.startswith('Dict['):
m = re.match(r'Dict\[([^,]*), (.*)]', klass)
assert m is not None, "Malformed Dict type definition"
if klass.startswith('dict['):
m = re.match(r'dict\[([^,]*), (.*)]', klass)
assert m is not None, "Malformed dict type definition"
sub_kls = m.group(2)
return {k: self.__deserialize(v, sub_kls)
for k, v in data.items()}
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Deserializer now only recognizes lowercase list[/dict[ strings, but response type maps still emit List[...]/Dict[...], causing AttributeError or incorrect deserialization.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py, line 444:

<comment>Deserializer now only recognizes lowercase `list[`/`dict[` strings, but response type maps still emit `List[...]`/`Dict[...]`, causing AttributeError or incorrect deserialization.</comment>

<file context>
@@ -441,16 +441,16 @@ def __deserialize(self, data, klass):
-            if klass.startswith('List['):
-                m = re.match(r'List\[(.*)]', klass)
-                assert m is not None, "Malformed List type definition"
+            if klass.startswith('list['):
+                m = re.match(r'list\[(.*)]', klass)
+                assert m is not None, "Malformed list type definition"
</file context>
Suggested change
if klass.startswith('list['):
m = re.match(r'list\[(.*)]', klass)
assert m is not None, "Malformed list type definition"
sub_kls = m.group(1)
return [self.__deserialize(sub_data, sub_kls)
for sub_data in data]
if klass.startswith('Dict['):
m = re.match(r'Dict\[([^,]*), (.*)]', klass)
assert m is not None, "Malformed Dict type definition"
if klass.startswith('dict['):
m = re.match(r'dict\[([^,]*), (.*)]', klass)
assert m is not None, "Malformed dict type definition"
sub_kls = m.group(2)
return {k: self.__deserialize(v, sub_kls)
for k, v in data.items()}
if klass.startswith(('list[', 'List[')):
m = re.match(r'[lL]ist\[(.*)]', klass)
assert m is not None, "Malformed list type definition"
sub_kls = m.group(1)
return [self.__deserialize(sub_data, sub_kls)
for sub_data in data]
if klass.startswith(('dict[', 'Dict[')):
m = re.match(r'[dD]ict\[([^,]*), (.*)]', klass)
assert m is not None, "Malformed dict type definition"
sub_kls = m.group(2)
return {k: self.__deserialize(v, sub_kls)
for k, v in data.items()}
Fix with Cubic

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 7 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="samples/openapi3/client/petstore/python-pydantic-v1/tests/test_deserialization.py">

<violation number="1" location="samples/openapi3/client/petstore/python-pydantic-v1/tests/test_deserialization.py:78">
P2: ApiClient.__deserialize only handles "List[...]" and "Dict[...]" strings, so the new lowercase built-in generic strings like "dict[str, Pet]"/"list[Pet]" will not be recognized and will fail deserialization unless the implementation is updated.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@@ -75,13 +75,13 @@ def test_deserialize_dict_str_pet(self):
}
response = MockResponse(data=json.dumps(data))

deserialized = self.deserialize(response, 'Dict[str, Pet]')
deserialized = self.deserialize(response, 'dict[str, Pet]')
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: ApiClient.__deserialize only handles "List[...]" and "Dict[...]" strings, so the new lowercase built-in generic strings like "dict[str, Pet]"/"list[Pet]" will not be recognized and will fail deserialization unless the implementation is updated.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/openapi3/client/petstore/python-pydantic-v1/tests/test_deserialization.py, line 78:

<comment>ApiClient.__deserialize only handles "List[...]" and "Dict[...]" strings, so the new lowercase built-in generic strings like "dict[str, Pet]"/"list[Pet]" will not be recognized and will fail deserialization unless the implementation is updated.</comment>

<file context>
@@ -75,13 +75,13 @@ def test_deserialize_dict_str_pet(self):
         response = MockResponse(data=json.dumps(data))
 
-        deserialized = self.deserialize(response, 'Dict[str, Pet]')
+        deserialized = self.deserialize(response, 'dict[str, Pet]')
         self.assertTrue(isinstance(deserialized, dict))
         self.assertTrue(isinstance(deserialized['pet'], petstore_api.Pet))
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant