Skip to content

Commit

Permalink
Reformat after ruff update to 2025 style guide
Browse files Browse the repository at this point in the history
ruff 0.9.0 shipped a new style guide that introduces some formatting
changes
https://astral.sh/blog/ruff-v0.9.0

Rule A005 (stdlib-module-shadowing) is no longer in preview as of
v0.9.0, and now complains about our file_formats/csv.py now, so the
csv module is now ignored in pyproject.toml
https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md#090
  • Loading branch information
rebkwok committed Jan 13, 2025
1 parent e31d906 commit 2431ad3
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ehrql/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def add_create_dummy_tables(subparsers, environ, user_args):
By default these will be CSV files. To generate files in other formats add
`:<format>` to the directory name e.g.
{backtick_join('my_outputs' + format_directory_extension(e) for e in FILE_FORMATS)}
{backtick_join("my_outputs" + format_directory_extension(e) for e in FILE_FORMATS)}
"""
),
type=valid_output_directory_with_csv_default,
Expand Down
6 changes: 4 additions & 2 deletions ehrql/backends/tpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,12 @@ def emergency_care_attendances(self):
ec.EC_Ident AS id,
ec.Arrival_Date AS arrival_date,
ec.Discharge_Destination_SNOMED_CT COLLATE Latin1_General_BIN AS discharge_destination,
{", ".join(
{
", ".join(
f"diag.EC_Diagnosis_{i:02d} COLLATE Latin1_General_BIN AS diagnosis_{i:02d}"
for i in range(1, 25)
)}
)
}
FROM EC{{table_suffix}} AS ec
LEFT JOIN EC_Diagnosis{{table_suffix}} AS diag
ON ec.EC_Ident = diag.EC_Ident
Expand Down
6 changes: 3 additions & 3 deletions ehrql/docs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def render(docs_data, output_dir):
reference_index = Path(__file__).parents[2] / "docs/reference/language.md"
reference_index_text = reference_index.read_text()
for filename in section_filenames:
assert (
filename in reference_index_text
), f"{filename} not included by {reference_index}"
assert filename in reference_index_text, (
f"{filename} not included by {reference_index}"
)


def fix_whitespace(s):
Expand Down
6 changes: 3 additions & 3 deletions ehrql/docs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def get_subcommands(parser):
for action in parser._actions
if isinstance(action, argparse._SubParsersAction)
]
assert (
len(subparsers) == 1
), f"Expected exactly one subcommand parser, got: {subparsers}"
assert len(subparsers) == 1, (
f"Expected exactly one subcommand parser, got: {subparsers}"
)
subparser = subparsers[0]
return [
get_subcommand(action, subparser.choices[action.metavar])
Expand Down
2 changes: 1 addition & 1 deletion ehrql/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _truncate_html_table(table_repr: str, head: int | None, tail: int | None):
rows = [row for row in rows.split("<tr>") if row]
# compose an "ellipsis row" to mark the place of truncated rows
td_count = rows[0].count("<td>")
ellipsis_row = f"{'<td>...</td>'*td_count}</tr>"
ellipsis_row = f"{'<td>...</td>' * td_count}</tr>"

# Build the list of rows we need to include, with ellipsis rows where necessary
truncated_rows = []
Expand Down
2 changes: 1 addition & 1 deletion hooks/parent_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def on_page_markdown(markdown, page, **kwargs):
markdown = markdown.replace(
parent_snippet,
'\n\n!!! note "TO BE REPLACED IN FULL DOCS BUILD"\n\n\tThis snippet will be replaced in the main docs '
f'with the parent file {parent_snippet.lstrip("!!! parent_snippet:")}',
f"with the parent file {parent_snippet.lstrip('!!! parent_snippet:')}",
)
return markdown
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ extend-ignore = [
"E501", # line-too-long
"UP032", # replace `.format` with f-string
]
flake8-builtins.builtins-allowed-modules = ["csv"]

isort.lines-after-imports = 2
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def random_should_not_be_used():
"""
prev_state = random.getstate()
yield
assert (
random.getstate() == prev_state
), "Global random number generator was used in test."
assert random.getstate() == prev_state, (
"Global random number generator was used in test."
)


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions tests/generative/test_query_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ def run_test(query_engines, data, population, variable, recorder):
first_results = [pytest.approx(row, rel=1e-5) for row in first_results]

for other_name, other_results in results.items():
assert (
first_results == other_results
), f"Mismatch between {first_name} and {other_name}"
assert first_results == other_results, (
f"Mismatch between {first_name} and {other_name}"
)


def setup_test(data, population, variable):
Expand Down
10 changes: 2 additions & 8 deletions tests/integration/file_formats/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def test_read_rows_validates_categories(test_file):
# the schema but with CSV we can only validate individual values
errors = {
"dataset.arrow": (
"Unexpected categories in column 'c'\n"
" Categories: A, B\n"
" Expected: X, Y"
"Unexpected categories in column 'c'\n Categories: A, B\n Expected: X, Y"
),
"dataset.csv": "'A' not in valid categories: 'X', 'Y'",
"dataset.csv.gz": "'A' not in valid categories: 'X', 'Y'",
Expand All @@ -156,11 +154,7 @@ def test_read_rows_validates_categories_on_non_categorical_column(test_file):
column_specs = TEST_FILE_SPECS.copy()
column_specs["s"] = ColumnSpec(str, categories=("X", "Y"))

error = (
"Unexpected categories in column 's'\n"
" Categories: a, b\n"
" Expected: X, Y"
)
error = "Unexpected categories in column 's'\n Categories: a, b\n Expected: X, Y"

with pytest.raises(FileValidationError, match=error):
read_rows(test_file, column_specs)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/utils/test_mssql_log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def log(event, **kwargs):

assert results == [(1,), (3,)]

assert log_lines[0] == ("[info ] SQL:\n" " SELECT 1")
assert log_lines[0] == ("[info ] SQL:\n SELECT 1")

assert log_lines[1] == (
"[info ] 0 seconds: exec_cpu_ms=0 exec_elapsed_ms=0 exec_cpu_ratio=0.0 parse_cpu_ms=0 parse_elapsed_ms=0\n"
Expand Down
12 changes: 6 additions & 6 deletions tests/spec/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def test_build_section():
assert len(paragraphs) == 2
for paragraph in paragraphs:
if paragraph["id"] == "1.1":
assert (
paragraph["text"] == "this docstring should appear in the spec"
), "paragraph text not found when docstring present"
assert paragraph["text"] == "this docstring should appear in the spec", (
"paragraph text not found when docstring present"
)
continue
if paragraph["id"] == "1.2":
assert (
"text" not in paragraph
), "paragraph text found when no docstring present"
assert "text" not in paragraph, (
"paragraph text found when no docstring present"
)
continue
assert False, "expected paragraph ids not found"

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_truncate_table_bad_html():
"</table>"
)

expected = "<table>\n" "<thead>\n" "..."
expected = "<table>\n<thead>\n..."

truncated = truncate_table(bad_html, head=2, tail=None)
assert truncated == expected, truncated
5 changes: 1 addition & 4 deletions tests/unit/utils/test_sqlalchemy_query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ def test_generated_table_from_query():
)
table = GeneratedTable.from_query("some_table", query, schema="some_schema")
assert str(sqlalchemy.schema.CreateTable(table)).strip() == (
"CREATE TABLE some_schema.some_table (\n"
"\tnumber INTEGER, \n"
"\tstring VARCHAR\n"
")"
"CREATE TABLE some_schema.some_table (\n\tnumber INTEGER, \n\tstring VARCHAR\n)"
)


Expand Down

0 comments on commit 2431ad3

Please sign in to comment.