Skip to content

Commit

Permalink
add extra newline to execute when returning dictionary (microsoft#24784)
Browse files Browse the repository at this point in the history
Resolves: microsoft#22469
Need one more extra line to "execute" on behalf of user when returning
dictionary.
  • Loading branch information
anthonykim1 authored Feb 5, 2025
1 parent b4e1ddb commit d5b19e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python_files/normalizeSelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ def normalize_lines(selection):

# Insert a newline between each top-level statement, and append a newline to the selection.
source = "\n".join(statements) + "\n"
# If selection ends with trailing dictionary or list, remove last unnecessary newline.
if selection[-2] == "}" or selection[-2] == "]":
source = source[:-1]
# If the selection contains trailing return dictionary, insert newline to trigger execute.
if check_end_with_return_dict(selection):
source = source + "\n"
except Exception:
# If there's a problem when parsing statements,
# append a blank line to end the block and send it as-is.
Expand All @@ -134,6 +138,11 @@ def normalize_lines(selection):
min_key = None


def check_end_with_return_dict(code):
stripped_code = code.strip()
return stripped_code.endswith("}") and "return {" in stripped_code.strip()


def check_exact_exist(top_level_nodes, start_line, end_line):
return [
node
Expand Down
47 changes: 47 additions & 0 deletions python_files/tests/test_normalize_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,50 @@ def test_list_comp(self):
result = normalizeSelection.normalize_lines(src)

assert result == expected

def test_return_dict(self):
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
def get_dog(name, breed):
return {'name': name, 'breed': breed}
"""
)

expected = textwrap.dedent(
"""\
def get_dog(name, breed):
return {'name': name, 'breed': breed}
"""
)

result = normalizeSelection.normalize_lines(src)

assert result == expected

def test_return_dict2(self):
importlib.reload(normalizeSelection)
src = textwrap.dedent(
"""\
def get_dog(name, breed):
return {'name': name, 'breed': breed}
dog = get_dog('Ahri', 'Pomeranian')
print(dog)
"""
)

expected = textwrap.dedent(
"""\
def get_dog(name, breed):
return {'name': name, 'breed': breed}
dog = get_dog('Ahri', 'Pomeranian')
print(dog)
"""
)

result = normalizeSelection.normalize_lines(src)

assert result == expected

0 comments on commit d5b19e7

Please sign in to comment.