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: annotate `post_parsing()` and `cleanup()` by AlexWaygood · Pull Request #107225 · python/cpython

Conversation

@AlexWaygood

@AlexWaygood AlexWaygood commented Jul 25, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

The docstring for CConverter.post_parsing() is here:

cpython/Tools/clinic/clinic.py

Lines 3021 to 3026 in f443b54

def post_parsing(self) -> str:
"""
The C statements required to do some operations after the end of parsing but before cleaning up.
Return a string containing this code indented at column 0.
If no operation is necessary, return an empty string.
"""

The docstring for CConverter.cleanup() is here:

cpython/Tools/clinic/clinic.py

Lines 3029 to 3034 in f443b54

def cleanup(self) -> str:
"""
The C statements required to clean up after this variable.
Returns a string containing this code indented at column 0.
If no cleanup is necessary, returns an empty string.
"""

Both docstrings state that these methods should always return a str, so it's arguably a micro-bug that str_converter.post_parsing() Py_UNICODE_converter.cleanup() can both sometimes return None currently, since str_converter and Py_UNICODE_converter are both subclasses of CConverter. (The micro-bug doesn't result in any erroneous behaviour currently, however.)

As such, I've slightly changed the implementations of these methods so that they always return a str, like the docstring promises.

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

Sorry, something went wrong.

Uh oh!

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

erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@AlexWaygood
Argument clinic: annotate post_parsing() and cleanup()
4cf5f61
@AlexWaygood AlexWaygood added the skip news label Jul 25, 2023
@AlexWaygood
AlexWaygood requested a review from erlend-aasland as a code owner July 25, 2023 10:19
@bedevere-bot bedevere-bot added the awaiting core review label Jul 25, 2023
@bedevere-bot bedevere-bot mentioned this pull request Jul 25, 2023
Add type annotations to clinic.py #104050
Closed
@AlexWaygood

AlexWaygood commented Jul 25, 2023 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

The alternative way of fixing this problem would be to do this (diff is relative to main):

Alternative fix
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 8c959ed4d0..e8e2710a18 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -3018,19 +3018,19 @@ def modify(self) -> str:
         """
         return ""

-    def post_parsing(self) -> str:
+    def post_parsing(self) -> str | None:
         """
         The C statements required to do some operations after the end of parsing but before cleaning up.
         Return a string containing this code indented at column 0.
-        If no operation is necessary, return an empty string.
+        If no operation is necessary, return an empty string or None.
         """
         return ""

-    def cleanup(self) -> str:
+    def cleanup(self) -> str | None:
         """
         The C statements required to clean up after this variable.
         Returns a string containing this code indented at column 0.
-        If no cleanup is necessary, returns an empty string.
+        If no cleanup is necessary, returns an empty string or None.
         """
         return ""

@@ -3660,10 +3660,11 @@ def converter_init(
         if NoneType in accept and self.c_default == "Py_None":
             self.c_default = "NULL"

-    def post_parsing(self):
+    def post_parsing(self) -> str | None:
         if self.encoding:
             name = self.name
             return f"PyMem_FREE({name});\n"
+        return None

     def parse_arg(self, argname: str, displayname: str) -> str:
         if self.format_unit == 's':
@@ -3841,11 +3842,12 @@ def converter_init(
                 fail("Py_UNICODE_converter: illegal 'accept' argument " + repr(accept))
         self.c_default = "NULL"

-    def cleanup(self):
+    def cleanup(self) -> str | None:
         if not self.length:
             return """\
 PyMem_Free((void *){name});
 """.format(name=self.name)
+        return None

     def parse_arg(self, argname: str, argnum: str) -> str:
         if not self.length:
All reactions

Sorry, something went wrong.

Uh oh!

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

@AlexWaygood
Merge branch 'main' into clinic-dodgy-methods
d0d4ca2
erlend-aasland
erlend-aasland approved these changes Jul 25, 2023
View reviewed changes

@erlend-aasland erlend-aasland left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks

Sorry, something went wrong.

Uh oh!

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

AlexWaygood reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Jul 25, 2023
@erlend-aasland

erlend-aasland commented Jul 25, 2023

Copy link
Copy Markdown
Contributor

FWIW, I agree that it is better to fix these microbugs instead of taking the str | None approach.

AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@AlexWaygood
AlexWaygood merged commit 33838fe into python:main Jul 25, 2023
@AlexWaygood
AlexWaygood deleted the clinic-dodgy-methods branch July 25, 2023 22:33
@bedevere-bot bedevere-bot removed the awaiting merge label Jul 25, 2023
jtcave pushed a commit to jtcave/cpython that referenced this pull request Jul 27, 2023
@AlexWaygood @jtcave
pythongh-104050: Argument clinic: annotate post_parsing() and `clea…
594b324
…nup()` (python#107225)
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.