[css] Address PR feedback for "Did you mean" suggestions

This commit is contained in:
Olivier Philippon
2022-05-11 10:37:32 +01:00
parent e37036c816
commit aa7b19a193
5 changed files with 22 additions and 34 deletions

View File

@@ -85,7 +85,7 @@ class ColorParseError(Exception):
message (str): the error message
suggested_color (str | None): a close color we can suggest. Defaults to None.
"""
super().__init__(*[message])
super().__init__(message)
self.suggested_color = suggested_color

View File

@@ -70,13 +70,13 @@ class HelpText:
Attributes:
summary (str): A succinct summary of the issue.
bullets (Iterable[Bullet]): Bullet points which provide additional
context around the issue. These are rendered below the summary.
bullets (Iterable[Bullet] | None): Bullet points which provide additional
context around the issue. These are rendered below the summary. Defaults to None.
"""
def __init__(self, summary: str, *, bullets: Iterable[Bullet]) -> None:
def __init__(self, summary: str, *, bullets: Iterable[Bullet] = None) -> None:
self.summary = summary
self.bullets = bullets
self.bullets = bullets or []
def __rich_console__(
self, console: Console, options: ConsoleOptions

View File

@@ -145,13 +145,13 @@ def property_invalid_value_help_text(
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
bullets = []
summary = f"Invalid CSS property [i]{property_name}[/]"
if suggested_property_name:
suggested_property_name = _contextualize_property_name(
suggested_property_name, context
)
bullets.append(Bullet(f'Did you mean "{suggested_property_name}"?'))
return HelpText(f"Invalid CSS property [i]{property_name}[/]", bullets=bullets)
summary += f'. Did you mean "{suggested_property_name}"?'
return HelpText(summary)
def spacing_wrong_number_of_values_help_text(
@@ -319,17 +319,15 @@ def color_property_help_text(
HelpText: Renderable for displaying the help text for this property
"""
property_name = _contextualize_property_name(property_name, context)
summary = f"Invalid value for the [i]{property_name}[/] property"
suggested_color = (
error.suggested_color if error and isinstance(error, ColorParseError) else None
)
if suggested_color:
summary += f'. Did you mean "{suggested_color}"?'
return HelpText(
summary=f"Invalid value for the [i]{property_name}[/] property",
summary=summary,
bullets=[
*(
[Bullet(f'Did you mean "{suggested_color}"?')]
if suggested_color
else []
),
Bullet(
f"The [i]{property_name}[/] property can only be set to a valid color"
),

View File

@@ -788,7 +788,7 @@ class ColorProperty:
help_text=color_property_help_text(
self.name, context="inline", error=error
),
) from error
)
if obj.set_rule(self.name, parsed_color):
obj.refresh()
else:

View File

@@ -90,17 +90,10 @@ def test_did_you_mean_for_css_property_names(
_, help_text = err.value.errors.rules[0].errors[0] # type: Any, HelpText
displayed_css_property_name = css_property_name.replace("_", "-")
assert (
help_text.summary == f"Invalid CSS property [i]{displayed_css_property_name}[/]"
)
expected_bullets_length = 1 if expected_property_name_suggestion else 0
assert len(help_text.bullets) == expected_bullets_length
if expected_property_name_suggestion is not None:
expected_suggestion_message = (
f'Did you mean "{expected_property_name_suggestion}"?'
)
assert help_text.bullets[0].markup == expected_suggestion_message
expected_summary = f"Invalid CSS property [i]{displayed_css_property_name}[/]"
if expected_property_name_suggestion:
expected_summary += f'. Did you mean "{expected_property_name_suggestion}"?'
assert help_text.summary == expected_summary
@pytest.mark.parametrize(
@@ -134,14 +127,11 @@ def test_did_you_mean_for_color_names(
_, help_text = err.value.errors.rules[0].errors[0] # type: Any, HelpText
displayed_css_property_name = css_property_name.replace("_", "-")
assert (
help_text.summary
== f"Invalid value for the [i]{displayed_css_property_name}[/] property"
expected_error_summary = (
f"Invalid value for the [i]{displayed_css_property_name}[/] property"
)
first_bullet = help_text.bullets[0]
if expected_color_suggestion is not None:
expected_suggestion_message = f'Did you mean "{expected_color_suggestion}"?'
assert first_bullet.markup == expected_suggestion_message
else:
assert "Did you mean" not in first_bullet.markup
expected_error_summary += f'. Did you mean "{expected_color_suggestion}"?'
assert help_text.summary == expected_error_summary