refactor(python): use built-in type hinting generics#22925
refactor(python): use built-in type hinting generics#22925timonrieger wants to merge 3 commits intoOpenAPITools:masterfrom
Conversation
There was a problem hiding this comment.
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.
| 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()} |
There was a problem hiding this comment.
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>
| 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()} |
There was a problem hiding this comment.
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]') | |||
There was a problem hiding this comment.
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>
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.
Written for commit 6b50ed1. Summary will update on new commits.