GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-104050: Argument clinic: improve typing around `parse_arg()` methods by AlexWaygood · Pull Request #107288 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Jul 26, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

  • Issue: Add type annotations to clinic.py #104050

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AlexWaygood
Argument clinic: improve typing around parse_arg() methods
7e4aaa8
@AlexWaygood AlexWaygood added the skip news label Jul 26, 2023
@AlexWaygood
AlexWaygood requested a review from erlend-aasland as a code owner July 26, 2023 09:37
@bedevere-bot bedevere-bot mentioned this pull request Jul 26, 2023
Add type annotations to clinic.py #104050
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Jul 26, 2023
@AlexWaygood

AlexWaygood commented Jul 26, 2023

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland
erlend-aasland approved these changes Jul 26, 2023
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jul 26, 2023
@AlexWaygood
AlexWaygood merged commit c362678 into python:main Jul 26, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label 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
@AlexWaygood @jtcave
pythongh-104050: Argument clinic: improve typing around parse_arg()…
0388b96
… methods (python#107288)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@erlend-aasland erlend-aasland erlend-aasland approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

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.

3 participants

@AlexWaygood @erlend-aasland @bedevere-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.