Skip to content

Commit 6504cc4

Browse files
handle formatting files with no formatting issues
1 parent f46b368 commit 6504cc4

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os, sys, json, datetime, math, random
2+
import requests
3+
from collections import defaultdict, OrderedDict
4+
from typing import List, Dict, Optional, Union, Tuple, Any
5+
import numpy as np
6+
import pandas as pd
7+
8+
# This is a poorly formatted Python file with many style violations
9+
10+
11+
class UnformattedExampleClass(object):
12+
def __init__(
13+
self,
14+
name,
15+
age=None,
16+
email=None,
17+
phone=None,
18+
address=None,
19+
city=None,
20+
state=None,
21+
zip_code=None,
22+
):
23+
self.name = name
24+
self.age = age
25+
self.email = email
26+
self.phone = phone
27+
self.address = address
28+
self.city = city
29+
self.state = state
30+
self.zip_code = zip_code
31+
self.data = {"name": name, "age": age, "email": email}
32+
33+
def get_info(self):
34+
return f"Name: {self.name}, Age: {self.age}"
35+
36+
def update_data(self, **kwargs):
37+
for key, value in kwargs.items():
38+
if hasattr(self, key):
39+
setattr(self, key, value)
40+
self.data.update(kwargs)
41+
42+
43+
def process_data(
44+
data_list, filter_func=None, transform_func=None, sort_key=None, reverse=False
45+
):
46+
if not data_list:
47+
return []
48+
if filter_func:
49+
data_list = [item for item in data_list if filter_func(item)]
50+
if transform_func:
51+
data_list = [transform_func(item) for item in data_list]
52+
if sort_key:
53+
data_list = sorted(data_list, key=sort_key, reverse=reverse)
54+
return data_list
55+
56+
57+
def calculate_statistics(numbers):
58+
if not numbers:
59+
return None
60+
mean = sum(numbers) / len(numbers)
61+
median = sorted(numbers)[len(numbers) // 2]
62+
variance = sum((x - mean) ** 2 for x in numbers) / len(numbers)
63+
std_dev = math.sqrt(variance)
64+
return {
65+
"mean": mean,
66+
"median": median,
67+
"variance": variance,
68+
"std_dev": std_dev,
69+
"min": min(numbers),
70+
"max": max(numbers),
71+
}

codeflash/code_utils/formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
def get_diff_output_by_black(filepath: str, unformatted_content: str) -> Optional[str]:
1717
try:
18-
from black import Mode, format_file_contents, output
18+
from black import Mode, format_file_contents, output, report
1919

2020
formatted_content = format_file_contents(src_contents=unformatted_content, fast=True, mode=Mode())
2121
return output.diff(unformatted_content, formatted_content, a_name=filepath, b_name=filepath)
22-
except ImportError:
22+
except (ImportError, report.NothingChanged):
2323
return None
2424

2525

tests/test_formatter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,7 @@ def test_formatting_file_with_many_diffs():
279279
def test_formatting_file_with_few_diffs():
280280
"""Test that files with few formatting errors are formatted (content changed)."""
281281
_run_formatting_test("few_formatting_errors.py", should_content_change=True)
282+
283+
def test_formatting_file_with_no_diffs():
284+
"""Test that files with no formatting errors are unchanged."""
285+
_run_formatting_test("no_formatting_errors.py", should_content_change=False)

0 commit comments

Comments
 (0)