Skip to content

Black bindings #6951

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

Merged
merged 6 commits into from
Sep 27, 2022
Merged

Black bindings #6951

merged 6 commits into from
Sep 27, 2022

Conversation

jepler
Copy link

@jepler jepler commented Sep 27, 2022

As an alternative to #6950 arrange to run black on each "//|" stubs block within bindings and shared-bindings, so that the formatting is made consistent at every commit.

Reasons not to do this right now: It churns a LOT and I was not attentive enough to review everything.

These are treated as warnings by extract_pyi, so they don't stop
the build process.
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

I think this is a good idea, since it canonicalizes the documentation.

One thing I do not understand is the removal of blank lines. Are you running black on the entire extraction of //| lines in each .c file?

Here's a typical set of changes, where blank lines between the defs are removed. This is not what black would do if they were all in one file:
image

Running black manually on an extract of SleepMemory.c:

class SleepMemory:
    def __init__(self) -> None:
        """Not used. Access the sole instance through `alarm.sleep_memory`."""
        ...

    def __bool__(self) -> bool:
        """``sleep_memory`` is ``True`` if its length is greater than zero.
        This is an easy way to check for its existence.
        """
        ...

    def __len__(self) -> int:
        """Return the length. This is used by (`len`)"""
        ...

It ensures there are blank lines between the defs.

@dhalbert
Copy link
Collaborator

From @jepler in discord:

I run black on each individual snippet of //| code, then string the results back together, so black doesn't have its normal memory from block to block that lets it know to add a newline between two 'def's

But also .. black has a distinct style for .pyi files!!

@jepler
Copy link
Author

jepler commented Sep 27, 2022

I run each 'section' through black, so black has no section-to-section memory to draw on.

After Dan's earlier comment I revised the script so that it includes a blank "//|" at the end of each block of "//|". This reduces the churn somewhat, in raw lines of code.

Here are the differences to the generated stubs from this branch:

diff -rU1 circuitpython-stubs-old/audiomixer/__init__.pyi circuitpython-stubs-new/audiomixer/__init__.pyi
--- circuitpython-stubs-old/audiomixer/__init__.pyi	2022-09-27 14:25:39.000785270 -0500
+++ circuitpython-stubs-new/audiomixer/__init__.pyi	2022-09-27 14:25:23.256709877 -0500
@@ -78,2 +78,3 @@
        (<MixerVoice>,)"""
+
     def play(
diff -rU1 circuitpython-stubs-old/bitbangio/__init__.pyi circuitpython-stubs-new/bitbangio/__init__.pyi
--- circuitpython-stubs-old/bitbangio/__init__.pyi	2022-09-27 14:25:39.044785481 -0500
+++ circuitpython-stubs-new/bitbangio/__init__.pyi	2022-09-27 14:25:23.300710087 -0500
@@ -156,2 +156,3 @@
         The number of bytes read will be the length of ``out_buffer[in_start:in_end]``.
+
         :param int address: 7-bit device address
diff -rU1 circuitpython-stubs-old/busio/__init__.pyi circuitpython-stubs-new/busio/__init__.pyi
--- circuitpython-stubs-old/busio/__init__.pyi	2022-09-27 14:25:39.112785806 -0500
+++ circuitpython-stubs-new/busio/__init__.pyi	2022-09-27 14:25:23.368710413 -0500
@@ -175,2 +175,3 @@
         The number of bytes read will be the length of ``out_buffer[in_start:in_end]``.
+
         :param int address: 7-bit device address
diff -rU1 circuitpython-stubs-old/canio/__init__.pyi circuitpython-stubs-new/canio/__init__.pyi
--- circuitpython-stubs-old/canio/__init__.pyi	2022-09-27 14:25:39.144785960 -0500
+++ circuitpython-stubs-new/canio/__init__.pyi	2022-09-27 14:25:23.400710567 -0500
@@ -105,2 +105,3 @@
     """The current state of the bus. (read-only)"""
+
     def restart(self) -> None:
diff -rU1 circuitpython-stubs-old/circuitpython_stubs.egg-info/PKG-INFO circuitpython-stubs-new/circuitpython_stubs.egg-info/PKG-INFO
--- circuitpython-stubs-old/circuitpython_stubs.egg-info/PKG-INFO	2022-09-27 14:25:41.196795786 -0500
+++ circuitpython-stubs-new/circuitpython_stubs.egg-info/PKG-INFO	2022-09-27 14:25:25.436720316 -0500
@@ -2,3 +2,3 @@
 Name: circuitpython-stubs
-Version: 8.0.0b1.dev72
+Version: 8.0.0b1.dev162
 Summary: PEP 561 type stubs for CircuitPython
Only in circuitpython-stubs-new/dist: circuitpython-stubs-8.0.0b1.dev162.tar.gz
Only in circuitpython-stubs-old/dist: circuitpython-stubs-8.0.0b1.dev72.tar.gz
diff -rU1 circuitpython-stubs-old/_eve/__init__.pyi circuitpython-stubs-new/_eve/__init__.pyi
--- circuitpython-stubs-old/_eve/__init__.pyi	2022-09-27 14:25:38.832784466 -0500
+++ circuitpython-stubs-new/_eve/__init__.pyi	2022-09-27 14:25:23.092709092 -0500
@@ -13,2 +13,4 @@
 class _EVE:
+    def __init__(self) -> None:
+        """Create an _EVE object"""
     def register(self, o: object) -> None: ...
diff -rU1 circuitpython-stubs-old/frequencyio/__init__.pyi circuitpython-stubs-new/frequencyio/__init__.pyi
--- circuitpython-stubs-old/frequencyio/__init__.pyi	2022-09-27 14:25:39.308786745 -0500
+++ circuitpython-stubs-new/frequencyio/__init__.pyi	2022-09-27 14:25:23.564711352 -0500
@@ -5,2 +5,3 @@
 
+
 All classes change hardware state and should be deinitialized when they
diff -rU1 circuitpython-stubs-old/hashlib/__init__.pyi circuitpython-stubs-new/hashlib/__init__.pyi
--- circuitpython-stubs-old/hashlib/__init__.pyi	2022-09-27 14:25:39.336786879 -0500
+++ circuitpython-stubs-new/hashlib/__init__.pyi	2022-09-27 14:25:23.588711467 -0500
@@ -11,3 +11,3 @@
 
-def new(name, data=b"") -> hashlib.Hash:
+def new(name: str, data: bytes = b"") -> hashlib.Hash:
     """Returns a Hash object setup for the named algorithm. Raises ValueError when the named
diff -rU1 circuitpython-stubs-old/keypad/__init__.pyi circuitpython-stubs-new/keypad/__init__.pyi
--- circuitpython-stubs-old/keypad/__init__.pyi	2022-09-27 14:25:39.404787205 -0500
+++ circuitpython-stubs-new/keypad/__init__.pyi	2022-09-27 14:25:23.660711811 -0500
@@ -66,2 +66,3 @@
     ...
+
     def get(self) -> Optional[Event]:
diff -rU1 circuitpython-stubs-old/ps2io/__init__.pyi circuitpython-stubs-new/ps2io/__init__.pyi
--- circuitpython-stubs-old/ps2io/__init__.pyi	2022-09-27 14:25:39.528787798 -0500
+++ circuitpython-stubs-new/ps2io/__init__.pyi	2022-09-27 14:25:23.784712405 -0500
@@ -4,2 +4,3 @@
 
+
 .. warning:: This module is not available in some SAMD21 builds. See the
@@ -7,2 +8,3 @@
 
+
 All classes change hardware state and should be deinitialized when they
diff -rU1 circuitpython-stubs-old/pwmio/__init__.pyi circuitpython-stubs-new/pwmio/__init__.pyi
--- circuitpython-stubs-old/pwmio/__init__.pyi	2022-09-27 14:25:39.548787894 -0500
+++ circuitpython-stubs-new/pwmio/__init__.pyi	2022-09-27 14:25:23.804712501 -0500
@@ -4,2 +4,3 @@
 
+
 All classes change hardware state and should be deinitialized when they
diff -rU1 circuitpython-stubs-old/storage/__init__.pyi circuitpython-stubs-new/storage/__init__.pyi
--- circuitpython-stubs-old/storage/__init__.pyi	2022-09-27 14:25:39.676788507 -0500
+++ circuitpython-stubs-new/storage/__init__.pyi	2022-09-27 14:25:23.932713114 -0500
@@ -6,2 +6,3 @@
 directly.
+
 For more information regarding using the `storage` module, refer to the `CircuitPython
diff -rU1 circuitpython-stubs-old/supervisor/__init__.pyi circuitpython-stubs-new/supervisor/__init__.pyi
--- circuitpython-stubs-old/supervisor/__init__.pyi	2022-09-27 14:25:39.700788622 -0500
+++ circuitpython-stubs-new/supervisor/__init__.pyi	2022-09-27 14:25:23.956713229 -0500
@@ -146,2 +146,3 @@
     This method must be called in boot.py to have any effect.
+
     Not available on boards without native USB support.
diff -rU1 circuitpython-stubs-old/usb/core/__init__.pyi circuitpython-stubs-new/usb/core/__init__.pyi
--- circuitpython-stubs-old/usb/core/__init__.pyi	2022-09-27 14:25:39.748788852 -0500
+++ circuitpython-stubs-new/usb/core/__init__.pyi	2022-09-27 14:25:24.008713478 -0500
@@ -22,3 +22,8 @@
 
-def find(find_all=False, *, idVendor=None, idProduct=None):
+def find(
+    find_all: bool = False,
+    *,
+    idVendor: Optional[int] = None,
+    idProduct: Optional[int] = None,
+) -> Device:
     """Find the first device that matches the given requirements or, if
@@ -50,3 +55,5 @@
 
-    def write(self, endpoint: int, data: ReadableBuffer, timeout=None) -> int:
+    def write(
+        self, endpoint: int, data: ReadableBuffer, timeout: Optional[int] = None
+    ) -> int:
         """Write data to a specific endpoint on the device.
@@ -59,3 +66,5 @@
         ...
-    def read(self, endpoint: int, size_or_buffer: array.array, timeout=None) -> int:
+    def read(
+        self, endpoint: int, size_or_buffer: array.array, timeout: Optional[int] = None
+    ) -> int:
         """Read data from the endpoint.
@@ -70,8 +79,8 @@
         self,
-        bmRequestType,
-        bRequest,
-        wValue=0,
-        wIndex=0,
+        bmRequestType: int,
+        bRequest: int,
+        wValue: int = 0,
+        wIndex: int = 0,
         data_or_wLength: Optional[array.array] = None,
-        timeout=None,
+        timeout: Optional[int] = None,
     ) -> int:
@@ -102,3 +111,3 @@
         ...
-    def detach_kernel_driver(self, interface: int):
+    def detach_kernel_driver(self, interface: int) -> None:
         """Stop CircuitPython from using the interface. If successful, you
@@ -110,3 +119,3 @@
         ...
-    def attach_kernel_driver(self, interface: int):
+    def attach_kernel_driver(self, interface: int) -> None:
         """Allow CircuitPython to use the interface if it wants to.

@jepler
Copy link
Author

jepler commented Sep 27, 2022

I revised this further, based on discord discussions:

  • Get heading/trailing lines closer to what they were before
  • but use black's "pyi style" in the formatted code, which notably removes blank lines between defs

Since black_bindings.py will pass each contiguous
"//|"-block to black independently, they must each be a fully
formed Python item.
@jepler
Copy link
Author

jepler commented Sep 27, 2022

.. updated again to ditch the trailing blank lines.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Nitpicking done ;) - thanks for this cleanup!

@dhalbert dhalbert merged commit 39492b3 into adafruit:main Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants