Skip to content

Commit dde6675

Browse files
authored
Merge pull request #409 from datacamp/bf/fix-black-formatting
[CP-902] Catch black formatting error
2 parents 2c80378 + 3d47e39 commit dde6675

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

pythonwhat/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def format_code(text):
88
mode = black.FileMode()
99
try:
1010
return black.format_file_contents(text, fast=True, mode=mode)
11-
except black.NothingChanged:
11+
except (black.NothingChanged, black.InvalidInput, black.CannotSplit):
1212
return text
1313

1414

tests/test_spec.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,69 @@ def car_wash(env):
191191
assert sct_payload["message"] == incorrect_msg
192192

193193

194+
def test_black_formatting_errors_catch(data):
195+
data["DC_CODE"] = """
196+
# Define a function called plot_timeseries
197+
def plot_timeseries(axes, x, y, color, xlabel, ylabel):
198+
199+
# Plot the inputs x,y in the provided color
200+
axes.plot(x, y, color=color)
201+
202+
# Set the x-axis label
203+
axes.set_xlabel(xlabel)
204+
205+
# Set the y-axis label
206+
axes.set_ylabel(ylabel, color=color)
207+
208+
# Set the colors tick params for y-axis
209+
axes.tick_params('y', colors=color)
210+
"""
211+
data["DC_SOLUTION"] = """
212+
# Define a function called plot_timeseries
213+
def plot_timeseries(axes, x, y, color, xlabel, ylabel):
214+
215+
# Plot the inputs x,y in the provided color
216+
axes.plot(x, y, color=color)
217+
218+
# Set the x-axis label
219+
axes.set_xlabel(xlabel)
220+
221+
# Set the y-axis label
222+
axes.set_ylabel(ylabel, color=color)
223+
224+
# Set the colors tick params for y-axis
225+
axes.tick_params('y', colors=color)
226+
"""
227+
data["DC_SCT"] = """
228+
msg1 = "Did you plot the x and y in the provided color?"
229+
msg2 = "Did you set the x-axis label?"
230+
msg3 = "Did you set the y-axis label?"
231+
msg4 = "Did you set the colors tick params for y-axis?"
232+
233+
Ex().check_function_def("plot_timeseries").check_body().multi(
234+
check_or(
235+
has_equal_ast(msg1, "axes.plot(x, y, color=color)", exact = False),
236+
has_equal_ast(msg1, "axes.plot(x, y, c=color)", exact = False)
237+
),
238+
check_or(
239+
has_equal_ast(msg2, "axes.set_xlabel(xlabel)", exact = False),
240+
has_equal_ast(msg2, "axes.set_xlabel(xlabel=xlabel)", exact = False)
241+
),
242+
check_or(
243+
has_equal_ast(msg3, "axes.set_ylabel(ylabel, color=color)", exact = False),
244+
has_equal_ast(msg3, "axes.set_ylabel(ylabel, c=color)", exact = False),
245+
has_equal_ast(msg3, "axes.set_ylabel(ylabel=ylabel, color=color)", exact = False),
246+
has_equal_ast(msg3, "axes.set_ylabel(ylabel=ylabel, c=color)", exact = False)
247+
),
248+
has_equal_ast(msg4, "axes.tick_params('y', colors=color)", exact = False),
249+
)
250+
251+
success_msg("Very good. Next, let's use this function!")
252+
"""
253+
sct_payload = helper.run(data)
254+
assert sct_payload["correct"]
255+
256+
194257
def test_has_equal_ast_simple_fail(data):
195258
data["DC_SCT"] = "Ex().has_equal_ast()"
196259
failing_submission(data)

0 commit comments

Comments
 (0)