Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 26, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jul 26, 2023

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
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
25 checks passed
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants