Skip to content

Commit

Permalink
stubber: Merge overloads, fix adding unused overloads.
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <[email protected]>
  • Loading branch information
Josverl committed Aug 23, 2024
1 parent 2e4eeb0 commit 1a52519
Show file tree
Hide file tree
Showing 33 changed files with 262 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/stubber/codemod/merge_docstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,22 @@ def add_missed_overloads(self, updated_node: Mod_Class_T, stack_id: tuple) -> Mo
# insert each overload just after a function with the same name
# reversed to keep insertions in same order as in the docstub
for overload in reversed(missing_overloads):
matched = False
for i, node in enumerate(updated_body):
if (
isinstance(node, cst.FunctionDef)
and node.name.value == overload.name.value
):
matched = True
break
updated_body.insert(i + 1, overload)
if matched:
updated_body.insert(i + 1, overload)

if isinstance(updated_node, cst.Module):
updated_node = updated_node.with_changes(body=tuple(updated_body))
elif isinstance(updated_node, cst.ClassDef):
b1 = updated_node.body.with_changes(body=tuple(updated_body))
updated_node = updated_node.with_changes( body = b1)
updated_node = updated_node.with_changes(body=b1)

# cst.IndentedBlock(body=tuple(updated_body))) # type: ignore
return updated_node
Expand Down
12 changes: 12 additions & 0 deletions tests/codemods/codemod_test_cases/func_overload_101/output.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# fmt: off
"""
Overloaded functions from python docs
"""
from typing import overload

@overload
def process(response: None) -> None: ...
@overload
def process(response: int) -> tuple[int, str]: ...
@overload
def process(response: bytes) -> str: ...
22 changes: 22 additions & 0 deletions tests/codemods/codemod_test_cases/func_overload_add_2/output.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

@overload
def foo(value: int) -> None:
"""
Set foo value
First overload
"""
...


@overload
def foo(value: None) -> str:
"""
Get foo value
Second overload
"""
...
31 changes: 31 additions & 0 deletions tests/codemods/codemod_test_cases/func_overload_add_3/output.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

@overload
def foo(value: int) -> None:
"""
Set foo value
First overload
"""
...


@overload
def foo(value: None) -> str:
"""
Get foo value
Second overload
"""
...


@overload
def foo(value: str) -> None:
"""
Get foo string
Third overload
"""
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

@overload
def foo(value: int) -> None:
"""
Set foo value
First overload
"""
...


@overload
def foo(value: None) -> str:
"""
Get foo value
Second overload
"""
...


@overload
def foo(value: str) -> None:
"""
Get foo string
Third overload
"""
...


def process(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

@overload
def foo(value: int) -> None:
"""
Set foo value
First overload
"""
...


@overload
def foo(value: None) -> str:
"""
Get foo value
Second overload
"""
...


@overload
def foo(value: str) -> None:
"""
Get foo string
Third overload
"""
...


def bar(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# fmt: off
"""
Overloaded functions
"""

def bar(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Overloaded functions
"""

from typing import overload


@overload
def foo(value: int) -> None:
"""
Set foo value
First overload
"""
...


@overload
def foo(value: None) -> str:
"""
Get foo value
Second overload
"""
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

def bar(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# fmt: off
"""
Overloaded functions
"""
from typing import overload

def bar(): ...
22 changes: 22 additions & 0 deletions tests/codemods/codemod_test_cases/meth_overload_add_2/output.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fmt: off
"""
Overloaded methods
"""
from typing import overload

class Parrot:
@overload
def speak(number: int):
"""
Speak a number
First overload
"""
...

@overload
def speak(words: str):
"""
Speak a word
Second overload
"""
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# fmt: off
"""
Overloaded methods
"""

class Parrot:
def talk(): ...


class Dog:
def speak(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Overloaded methods
"""

from typing import overload


class Parrot:

@overload
def speak(number: int):
"""
Speak a number
First overload
"""
...

@overload
def speak(words: str):
"""
Speak a word
Second overload
"""
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# fmt: off
"""
Overloaded methods
"""
from typing import overload

class Parrot:
def talk(): ...


class Dog:
def speak(): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# fmt: off
"""
Overloaded methods
"""
from typing import overload

class Parrot:
def talk(): ...


class Dog:
def speak(): ...

0 comments on commit 1a52519

Please sign in to comment.