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

add extra newline to execute when returning dictionary #24784

Merged
merged 3 commits into from
Feb 5, 2025
Merged
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
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
Loading