GeezerVison is showing github.com — gh-104050: Argument clinic: improve typing around `parse_arg()` methods by AlexWaygood · Pull Request #107288 · python/cpython
Conversation
AlexWaygood
commented
Jul 26, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
CConverter.parse_arg() returns str | None, and all subclasses of CConverter return super().parse_arg() in at least one branch. As such, all subclasses must also return str | None.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I experimented with an alternative diff:
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index d9e893f1f1..c8ab574e33 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -3046,7 +3046,7 @@ def pre_render(self) -> None:
"""
pass
- def parse_arg(self, argname: str, displayname: str):
+ def parse_arg(self, argname: str, displayname: str) -> str:
if self.format_unit == 'O&':
return """
if (!{converter}({argname}, &{paramname})) {{{{
@@ -3081,7 +3081,7 @@ def parse_arg(self, argname: str, displayname: str):
return """
{paramname} = {cast}{argname};
""".format(argname=argname, paramname=self.parser_name, cast=cast)
- return None
+ raise AssertionError(f"Unknown format unit {self.format_unit!r}")
def set_template_dict(self, template_dict: TemplateDict) -> None:
pass
However, mypy pointed out that there are lots of places in clinic.py that explicitly check to see whether the result of the parse_arg() method call is None or not, and that these would all become redundant if parse_arg() always returned a str:
Tools\clinic\clinic.py:1020: error: Statement is unreachable [unreachable]
parsearg = """
^
Tools\clinic\clinic.py:1100: error: Statement is unreachable [unreachable]
parser_code = None
^~~~~~~~~~~~~~~~~~
Tools\clinic\clinic.py:1115: error: Statement is unreachable [unreachable]
if not new_or_init:
^
Tools\clinic\clinic.py:1193: error: Statement is unreachable [unreachable]
parser_code = None
^~~~~~~~~~~~~~~~~~
Tools\clinic\clinic.py:1247: error: Statement is unreachable [unreachable]
declarations = declare_parser(f, hasformat=True)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It also causes some runtime tests to fail, as well.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|