Skip to content

Commit 758d204

Browse files
Comply with B ruff rules (#372)
* Comply with `B` ruff rules * Refactor away shallow badge adding and removal functions Add test for non-markdown README case. * Add test for show sonarqube config case with no key * Add test for readme interface adding badges * Add test for indentation error hint in bitbucket CI yaml
1 parent bb97936 commit 758d204

File tree

20 files changed

+121
-68
lines changed

20 files changed

+121
-68
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ line-length = 88
8686

8787
lint.select = [
8888
"A",
89+
"B",
8990
"C4",
9091
"D",
9192
"E4",

src/usethis/_core/badge.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ def equivalent_to(self, other: Self) -> bool:
3333
return self.name == other.name
3434

3535

36-
RUFF_BADGE = Badge(
37-
markdown="[![Ruff](<https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json>)](<https://github.com/astral-sh/ruff>)"
38-
)
39-
PRE_COMMIT_BADGE = Badge(
40-
markdown="[![pre-commit](<https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit>)](<https://github.com/pre-commit/pre-commit>)"
41-
)
42-
43-
4436
def get_pypi_badge() -> Badge:
4537
try:
4638
name = get_name()
@@ -56,36 +48,24 @@ def get_pypi_badge() -> Badge:
5648
)
5749

5850

59-
def get_badge_order() -> list[Badge]:
60-
return [
61-
get_pypi_badge(),
62-
RUFF_BADGE,
63-
PRE_COMMIT_BADGE,
64-
]
65-
66-
67-
def add_pypi_badge():
68-
add_badge(get_pypi_badge())
69-
70-
71-
def add_ruff_badge():
72-
add_badge(RUFF_BADGE)
73-
74-
75-
def add_pre_commit_badge():
76-
add_badge(PRE_COMMIT_BADGE)
77-
78-
79-
def remove_pypi_badge():
80-
remove_badge(get_pypi_badge())
51+
def get_ruff_badge() -> Badge:
52+
return Badge(
53+
markdown="[![Ruff](<https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json>)](<https://github.com/astral-sh/ruff>)"
54+
)
8155

8256

83-
def remove_ruff_badge():
84-
remove_badge(RUFF_BADGE)
57+
def get_pre_commit_badge() -> Badge:
58+
return Badge(
59+
markdown="[![pre-commit](<https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit>)](<https://github.com/pre-commit/pre-commit>)"
60+
)
8561

8662

87-
def remove_pre_commit_badge():
88-
remove_badge(PRE_COMMIT_BADGE)
63+
def get_badge_order() -> list[Badge]:
64+
return [
65+
get_pypi_badge(),
66+
get_ruff_badge(),
67+
get_pre_commit_badge(),
68+
]
8969

9070

9171
def add_badge(badge: Badge) -> None:
@@ -95,7 +75,7 @@ def add_badge(badge: Badge) -> None:
9575
path = _get_markdown_readme_path()
9676
except FileNotFoundError as err:
9777
err_print(err)
98-
raise typer.Exit(code=1)
78+
raise typer.Exit(code=1) from None
9979

10080
prerequisites: list[Badge] = []
10181
for _b in get_badge_order():

src/usethis/_core/show.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def show_sonarqube_config() -> None:
2323
print(get_sonar_project_properties())
2424
except UsethisError as err:
2525
err_print(err)
26-
raise typer.Exit(code=1)
26+
raise typer.Exit(code=1) from None

src/usethis/_integrations/github/tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_github_latest_tag(owner: str, repo: str) -> str:
2828
response.raise_for_status() # Raise an error for HTTP issues
2929
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as err:
3030
msg = f"Failed to fetch tags from GitHub API: {err}"
31-
raise GitHubTagError(msg)
31+
raise GitHubTagError(msg) from None
3232

3333
tags = response.json()
3434

src/usethis/_integrations/pre_commit/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def add_repo(repo: LocalRepo | UriRepo) -> None:
7171
hook_idx = _HOOK_ORDER.index(hook_name)
7272
except ValueError:
7373
msg = f"Hook '{hook_name}' not recognized"
74-
raise NotImplementedError(msg)
74+
raise NotImplementedError(msg) from None
7575
precedents = _HOOK_ORDER[:hook_idx]
7676

7777
# Find the last of the precedents in the existing hooks

src/usethis/_integrations/pyproject_toml/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def set_pyproject_value(
4141
toml_document=pyproject, id_keys=id_keys, value=value, exists_ok=exists_ok
4242
)
4343
except TOMLValueAlreadySetError as err:
44-
raise PyprojectTOMLValueAlreadySetError(err)
44+
raise PyprojectTOMLValueAlreadySetError(err) from None
4545

4646
PyprojectTOMLManager().commit(pyproject)
4747

@@ -58,7 +58,7 @@ def remove_pyproject_value(
5858
pyproject = remove_toml_value(toml_document=pyproject, id_keys=id_keys)
5959
except TOMLValueMissingError as err:
6060
if not missing_ok:
61-
raise PyprojectTOMLValueMissingError(err)
61+
raise PyprojectTOMLValueMissingError(err) from None
6262
# Otherwise, no changes are needed so skip the write step.
6363
return
6464
PyprojectTOMLManager().commit(pyproject)

src/usethis/_integrations/pyproject_toml/io_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def read_file(self) -> None:
9191
self._content = parse(self._path.read_text())
9292
except FileNotFoundError:
9393
msg = "'pyproject.toml' not found in the current directory."
94-
raise PyprojectTOMLNotFoundError(msg)
94+
raise PyprojectTOMLNotFoundError(msg) from None
9595
except TOMLKitError as err:
9696
msg = f"Failed to decode 'pyproject.toml': {err}"
9797
raise PyprojectTOMLDecodeError(msg) from None

src/usethis/_integrations/pyproject_toml/name.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def get_name() -> str:
1616
name = TypeAdapter(str).validate_python(project_dict["name"])
1717
except KeyError:
1818
msg = "The 'project.name' value is missing from 'pyproject.toml'."
19-
raise PyprojectTOMLProjectNameError(msg)
19+
raise PyprojectTOMLProjectNameError(msg) from None
2020
except ValidationError as err:
2121
msg = (
2222
f"The 'project.name' value in 'pyproject.toml' is not a valid string: {err}"
2323
)
24-
raise PyprojectTOMLProjectNameError(msg)
24+
raise PyprojectTOMLProjectNameError(msg) from None
2525

2626
return name
2727

@@ -33,9 +33,9 @@ def get_description() -> str:
3333
description = TypeAdapter(str).validate_python(project_dict["description"])
3434
except KeyError:
3535
msg = "The 'project.description' value is missing from 'pyproject.toml'."
36-
raise PyprojectTOMLProjectDescriptionError(msg)
36+
raise PyprojectTOMLProjectDescriptionError(msg) from None
3737
except ValidationError as err:
3838
msg = f"The 'project.description' value in 'pyproject.toml' is not a valid string: {err}"
39-
raise PyprojectTOMLProjectDescriptionError(msg)
39+
raise PyprojectTOMLProjectDescriptionError(msg) from None
4040

4141
return description

src/usethis/_integrations/pyproject_toml/project.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def get_project_dict() -> dict[str, Any]:
2020
project = TypeAdapter(dict).validate_python(pyproject["project"])
2121
except KeyError:
2222
msg = "The 'project' section is missing from 'pyproject.toml'."
23-
raise PyprojectTOMLProjectSectionError(msg)
23+
raise PyprojectTOMLProjectSectionError(msg) from None
2424
except ValidationError as err:
2525
msg = f"The 'project' section in 'pyproject.toml' is not a valid map: {err}"
26-
raise PyprojectTOMLProjectSectionError(msg)
26+
raise PyprojectTOMLProjectSectionError(msg) from None
2727

2828
return project

src/usethis/_integrations/pyproject_toml/requires_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ def get_requires_python() -> SpecifierSet:
1919
)
2020
except KeyError:
2121
msg = "The 'project.requires-python' value is missing from 'pyproject.toml'."
22-
raise MissingRequiresPythonError(msg)
22+
raise MissingRequiresPythonError(msg) from None
2323

2424
return SpecifierSet(requires_python)

0 commit comments

Comments
 (0)