Skip to content

gh-104050: Argument clinic: improve typing around parse_arg() methods - #107288

Merged
AlexWaygood merged 1 commit into
python:mainfrom
AlexWaygood:clinic-parse-arg-typing
Jul 26, 2023
Merged

AlexWaygood merged 1 commit into
python:mainfrom
AlexWaygood:clinic-parse-arg-typing

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Jul 26, 2023 •

Copy link
Copy Markdown
Member

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.

@AlexWaygood

Copy link
Copy Markdown
Member Author

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.

@AlexWaygood
AlexWaygood merged commit c362678 into python:main Jul 26, 2023
@AlexWaygood
AlexWaygood deleted the clinic-parse-arg-typing branch July 26, 2023 10:51
jtcave pushed a commit to jtcave/cpython that referenced this pull request Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants