Skip to content
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

Added new test and changes to imr.py to address the issue #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion bowler/imr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@
Node,
)

def add_argument(node, arg_name, default_value=None, after=None):
if node.type == TOKEN.FUNC_DEF:
func_args = node.children[2]
func_args.append_child(Node(TOKEN.ARGUMENT, arg_name))

if default_value is not None:
func_args.append_child(Node(TOKEN.EQUALS, "="))
func_args.append_child(Node(TOKEN.NUMBER, default_value))

if after is not None:
# Find the position after which to add the argument
for i, child in enumerate(func_args.children):
if child.value == after:
pos = i + 1
break
else:
pos = len(func_args.children)

func_args.insert_child(pos, Node(TOKEN.ARGUMENT, arg_name))

return node

log = logging.getLogger(__name__)


Expand All @@ -47,7 +69,7 @@ def build(cls, leaf: Leaf, is_def: bool, **kwargs: Any) -> "FunctionArgument":

elif leaf.type == SYMBOL.tname:
kwargs["name"] = leaf.children[0].value
kwargs["annotation"] = leaf.children[-1].value
kwargs["annotation"] = leaf.children[-1]

elif leaf.type == TOKEN.EQUAL:
pass
Expand Down
16 changes: 16 additions & 0 deletions bowler/tests/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,19 @@ def add_bad_modifier(q):
self.assertIn(
"Only the last fixer/callback may return", error.call_args[0][0]
)

def test_function_with_type_hinting(self):
def f(x: 'List[str]'):
pass

def query_func(x):
return Query(x).select_function(f).add_argument("y", "5", True, "x")

self.run_bowler_modifiers(
[
("def f(x: List[str]): pass", "def f(x: List[str], y): pass"),
("def g(x: List[str]): pass", "def g(x: List[str]): pass"),
("f(['test'])", "f(['test'], 5)"),
],
query_func=query_func,
)