Skip to content

Commit

Permalink
stubber: Docstubs - Fix/undo removal of positional only /) from par…
Browse files Browse the repository at this point in the history
…ameters

Signed-off-by: Jos Verlinde <[email protected]>
  • Loading branch information
Josverl committed Aug 25, 2024
1 parent 4a5a79a commit 7d5a599
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/stubber/rst/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class Fix:
"machine.ADC": ["ATTN_0DB:int = ..."], # uses Pin
"machine.RTC": ["from machine import IDLE"], # uses Pin
"machine.UART": ["from machine import IDLE"], # uses Pin
"network": ["from abc import ABC"], # for AbstractNIC
"network": ["from typing import Protocol"], # for AbstractNIC
"rp2": ["from .PIO import PIO"], #
"pyb": ["from .UART import UART"], # uses Pin
"pyb.Switch": ["from .Pin import Pin"], # uses Pin
Expand All @@ -269,7 +269,12 @@ class Fix:
Fix(r"\**", "*"), # change weirdly written wildcards \* --> *
Fix(r"/*", "*"), # change weirdly written wildcards /* --> *
Fix(r"**", "*"), # change weirdly written wildcards ** --> *
Fix(r"/)", ")"), # strange terminator in machine.USBDevice `USBDevice.active(self, [value] /)`
# do not remove / , this indicates positional only notation before the ,/
# RE to insert missing , before /
Fix(from_=r"(\w+.*?[^,])\s*/", to=r"\1 ,/", is_re=True),
Fix(", ,/", ", /"), # remove double commas ( cause by the above fix) its a kludge
# Fix("]=None /)", "]=None, /)")
# ref: https://regex101.com/r/crVQfA/1
Fix("'param'", "param"), # loose notation in documentation
# illegal keywords
Fix(
Expand Down Expand Up @@ -545,7 +550,7 @@ class Fix:
# array
"array": "List",
# network
"AbstractNIC": "ABC",
"AbstractNIC": "Protocol",
}


Expand Down
18 changes: 13 additions & 5 deletions tests/rst/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,16 @@ def test_rst_parse_class_10(line: str):
# ESPNow extra notation
("ESPNow.config('param') (ESP32 only)", "ESPNow.config(param)"),
# machine.USBDevice
# Extranous / `USBDevice.active(self, [value] /)`
("USBDevice.active(self, [value]/)", "USBDevice.active(self, value: Optional[Any]=None)"),
# Positional Only indicator should not be removed / `USBDevice.active(self, [value] /)`
(
"USBDevice.active(self, [value]/)",
"USBDevice.active(self, value: Optional[Any]=None ,/)",
),
(
"USBDevice.active(self, [value] /)",
"USBDevice.active(self, value: Optional[Any]=None ,/)",
),
# USBDevice.active(self, [value] /)
],
)
def test_fix_param(param_in, expected):
Expand Down Expand Up @@ -378,7 +386,7 @@ def test_doc_pyright_obscured_definitions(pyright_results, capsys):
def test_doc_deepsleep_stub(rst_stubs):
"Deepsleep stub is generated"
content = read_stub(rst_stubs / "machine", "__init__.pyi")
# return type omitted as this is tested seperately
# return type omitted as this is tested separately
found = any(line.startswith("def deepsleep(time_ms") for line in content)
assert (
found
Expand Down Expand Up @@ -425,8 +433,8 @@ def test_doc_class_not_function_def(rst_stubs: Path, modulename: str, classname:
content = read_stub(rst_stubs / modulename, "__init__.pyi")
if content == [] and modulename[0] == "u":
# module name change to select.py in v1.17+
filename = filename[1:]
content = read_stub(rst_stubs, filename)
modulename = modulename[1:]
content = read_stub(rst_stubs, modulename)
if content == []:
assert f"module {modulename} was not stubbed"
found = any(line.startswith(f"def {classname}") for line in content)
Expand Down

0 comments on commit 7d5a599

Please sign in to comment.