Skip to content

Commit 4ad09bf

Browse files
feat: migrating linting/formatting exclusively to rust (#750)
* feat: migrating linting/formatting exclusively to rust * feat: added explicit linting/formatting rules * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: implemented PR corrections --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3648ebe commit 4ad09bf

File tree

7 files changed

+98
-97
lines changed

7 files changed

+98
-97
lines changed

.pre-commit-config.yaml

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
repos:
22

3-
- repo: https://github.com/psf/black
4-
rev: 24.10.0
3+
- repo: https://github.com/astral-sh/ruff-pre-commit
4+
rev: v0.7.3
55
hooks:
6-
- id: black
7-
8-
- repo: https://github.com/pycqa/isort
9-
rev: 5.13.2
10-
hooks:
11-
- id: isort
12-
13-
- repo: https://github.com/PyCQA/flake8
14-
rev: 7.1.1
15-
hooks:
16-
- id: flake8
6+
- id: ruff
7+
- id: ruff-format
178

189
- repo: https://github.com/codespell-project/codespell
1910
rev: v2.3.0
@@ -43,7 +34,7 @@ repos:
4334
- id: check-github-workflows
4435

4536
- repo: https://github.com/ansys/pre-commit-hooks
46-
rev: v0.4.4
37+
rev: v0.4.3
4738
hooks:
4839
- id: add-license-headers
4940
args:

README.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PyAnsys metapackage
22
===================
3-
|pyansys| |python| |pypi| |downloads| |GH-CI| |MIT| |black| |pre-commit|
3+
|pyansys| |python| |pypi| |downloads| |GH-CI| |MIT| |Ruff| |pre-commit|
44

55
.. |pyansys| image:: https://img.shields.io/badge/Py-Ansys-ffc107.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABDklEQVQ4jWNgoDfg5mD8vE7q/3bpVyskbW0sMRUwofHD7Dh5OBkZGBgW7/3W2tZpa2tLQEOyOzeEsfumlK2tbVpaGj4N6jIs1lpsDAwMJ278sveMY2BgCA0NFRISwqkhyQ1q/Nyd3zg4OBgYGNjZ2ePi4rB5loGBhZnhxTLJ/9ulv26Q4uVk1NXV/f///////69du4Zdg78lx//t0v+3S88rFISInD59GqIH2esIJ8G9O2/XVwhjzpw5EAam1xkkBJn/bJX+v1365hxxuCAfH9+3b9/+////48cPuNehNsS7cDEzMTAwMMzb+Q2u4dOnT2vWrMHu9ZtzxP9vl/69RVpCkBlZ3N7enoDXBwEAAA+YYitOilMVAAAAAElFTkSuQmCC
66
:target: https://docs.pyansys.com/
@@ -26,9 +26,9 @@ PyAnsys metapackage
2626
:target: https://opensource.org/licenses/MIT
2727
:alt: MIT
2828

29-
.. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=flat
30-
:target: https://github.com/psf/black
31-
:alt: Black
29+
.. |Ruff| image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
30+
:target: https://github.com/astral-sh/ruff
31+
:alt: Ruff
3232

3333
.. |pre-commit| image:: https://results.pre-commit.ci/badge/github/pyansys/pyansys/main.svg
3434
:target: https://results.pre-commit.ci/latest/github/pyansys/pyansys/main

doc/source/conf.py

+50-59
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,57 @@
22

33
from datetime import datetime
44
import os
5+
from pathlib import Path
56
import subprocess
67

78
from ansys_sphinx_theme import ansys_favicon, get_version_match
9+
import github
10+
import jinja2
11+
from PIL import Image
12+
import requests
813
import sphinx
914
from sphinx.builders.latex import LaTeXBuilder
15+
import toml
1016

1117
from pyansys import __version__ as pyansys_version
1218

13-
LaTeXBuilder.supported_image_types = ["image/png", "image/pdf", "image/svg+xml"] # noqa: E501
19+
# Declare constants
20+
GENERATED_DIR = Path(__file__).parent / "package_versions"
21+
22+
VERSIONS_TEMPLATE = """
23+
Package versions in PyAnsys {{ version }}
24+
============================{{ "=" * version|length }}
25+
26+
The PyAnsys packages delivered in version {{ version }} are:
27+
{{ ' ' }}
28+
{%- for entry in table %}
29+
{{ entry }}
30+
{%- endfor %}
31+
"""
32+
33+
INDEX_TEMPLATE = """
34+
Package versions
35+
================
36+
37+
Users can find below the list of PyAnsys packages available in the various
38+
PyAnsys metapackages. The tables shows the package versions available in each
39+
metapackage release.
40+
41+
.. toctree::
42+
:maxdepth: 3
43+
{{ ' ' }}
44+
{%- for version in versions %}
45+
version_{{ version }}
46+
{%- endfor %}
47+
"""
48+
49+
TMP_FILE = Path("tmp_pyproject.toml")
50+
51+
LaTeXBuilder.supported_image_types = [
52+
"image/png",
53+
"image/pdf",
54+
"image/svg+xml",
55+
] # noqa: E501
1456

1557
project = "pyansys"
1658
copyright = f"(c) {datetime.now().year} ANSYS, Inc. All rights reserved"
@@ -90,8 +132,8 @@
90132
# make rst_epilog a variable, so you can add other epilog parts to it
91133
rst_epilog = ""
92134
# Read link all targets from file
93-
with open("links.rst") as f:
94-
rst_epilog += f.read()
135+
with Path.open("links.rst") as file:
136+
rst_epilog += file.read()
95137

96138
# Ignore certain URLs
97139
linkcheck_ignore = [
@@ -123,39 +165,6 @@
123165

124166
def generate_rst_files(versions: list[str], tables: dict[str, list[str]]):
125167
"""Generate the .rst files for the package versions."""
126-
from pathlib import Path
127-
128-
import jinja2
129-
130-
GENERATED_DIR = Path(__file__).parent / "package_versions"
131-
132-
VERSIONS_TEMPLATE = """
133-
Package versions in PyAnsys {{ version }}
134-
============================{{ "=" * version|length }}
135-
136-
The PyAnsys packages delivered in version {{ version }} are:
137-
{{ ' ' }}
138-
{%- for entry in table %}
139-
{{ entry }}
140-
{%- endfor %}
141-
"""
142-
143-
INDEX_TEMPLATE = """
144-
Package versions
145-
================
146-
147-
Users can find below the list of PyAnsys packages available in the various
148-
PyAnsys metapackages. The tables shows the package versions available in each
149-
metapackage release.
150-
151-
.. toctree::
152-
:maxdepth: 3
153-
{{ ' ' }}
154-
{%- for version in versions %}
155-
version_{{ version }}
156-
{%- endfor %}
157-
"""
158-
159168
# Create Jinja2 environment
160169
jinja_env = jinja2.Environment(loader=jinja2.BaseLoader())
161170

@@ -174,23 +183,19 @@ def generate_rst_files(versions: list[str], tables: dict[str, list[str]]):
174183
output_filename = GENERATED_DIR / f"version_{version}.rst"
175184

176185
# Write the rendered content to the file
177-
with open(output_filename, "w") as f:
178-
f.write(rendered_content)
186+
output_filename.write_text(rendered_content, encoding="utf-8")
179187

180188
# Generate the index.rst file
181189
index_template = jinja_env.from_string(INDEX_TEMPLATE)
182190
rendered_index = index_template.render(versions=versions)
183191

184192
# Write the rendered content to the file
185193
output_filename = GENERATED_DIR / "index.rst"
186-
with open(output_filename, "w") as f:
187-
f.write(rendered_index)
194+
output_filename.write_text(rendered_index, encoding="utf-8")
188195

189196

190197
def get_documentation_link_from_pypi(library: str, version: str) -> str:
191198
"""Get the documentation link from PyPI for a specific library and version."""
192-
import requests
193-
194199
# Get the PyPI metadata for the library
195200
resp = requests.get(f"https://pypi.org/pypi/{library}/{version}/json")
196201
metadata = resp.json()
@@ -224,8 +229,6 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
224229
failure, it returns the default link. This is done on a best effort basis.
225230
226231
"""
227-
import requests
228-
229232
# First, let's check it is an official PyAnsys documentation link
230233
if "docs.pyansys.com" in docs_link:
231234
# Clean the link
@@ -247,15 +250,10 @@ def pyansys_multiversion_docs_link(docs_link: str, version: str) -> str:
247250

248251
def build_versions_table(branch: str) -> list[str]:
249252
"""Build the versions table for the PyAnsys libraries."""
250-
import requests
251-
import toml
252-
253-
TMP_FILE = "tmp_pyproject.toml"
254-
255253
# Download the pyproject.toml file
256254
resp = requests.get(f"https://raw.githubusercontent.com/ansys/pyansys/{branch}/pyproject.toml")
257-
with open("tmp_pyproject.toml", "wb") as f:
258-
f.write(resp.content)
255+
with TMP_FILE.open("wb") as file:
256+
file.write(resp.content)
259257

260258
# Load the pyproject.toml file using TOML parser
261259
pyproject_toml = toml.load(TMP_FILE)
@@ -290,7 +288,7 @@ def build_versions_table(branch: str) -> list[str]:
290288
]
291289

292290
# Delete the temporary file
293-
os.remove(TMP_FILE)
291+
TMP_FILE.unlink()
294292

295293
# Build the table
296294
table = []
@@ -329,8 +327,6 @@ def build_versions_table(branch: str) -> list[str]:
329327

330328
def get_release_branches_in_metapackage():
331329
"""Retrieve the release branches in the PyAnsys metapackage."""
332-
import github
333-
334330
# Get the PyAnsys metapackage repository
335331
g = github.Github(os.getenv("GITHUB_TOKEN", None))
336332
github_repo = g.get_repo("ansys/pyansys")
@@ -377,8 +373,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
377373
target_size : tuple[int, int]
378374
The target size of the output image as a tuple (width, height) in pixels.
379375
"""
380-
from PIL import Image
381-
382376
# Open the input image
383377
img = Image.open(input_image_path).convert("RGBA") # Ensure the image has an alpha channel
384378

@@ -415,7 +409,6 @@ def resize_with_background(input_image_path, output_image_path, target_size):
415409
def resize_thumbnails(app: sphinx.application.Sphinx):
416410
"""Resize all images in the current directory to 640x480 pixels."""
417411
# Process all images
418-
from pathlib import Path
419412

420413
thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails"
421414

@@ -426,8 +419,6 @@ def resize_thumbnails(app: sphinx.application.Sphinx):
426419

427420
def revert_thumbnails(app: sphinx.application.Sphinx, exception):
428421
"""Resize all images in the current directory to 640x480 pixels."""
429-
from pathlib import Path
430-
431422
thumbnail_dir = Path(__file__).parent.absolute() / "_static" / "thumbnails"
432423

433424
subprocess.run(["git", "checkout", "--", thumbnail_dir])

pyproject.toml

+27-6
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,32 @@ Documentation = "https://docs.pyansys.com"
110110
[tool.flit.module]
111111
name = "pyansys"
112112

113-
[tool.black]
113+
[tool.ruff]
114114
line-length = 100
115+
fix = true
115116

116-
[tool.isort]
117-
profile = "black"
118-
force_sort_within_sections = true
119-
line_length = 100
120-
src_paths = ["doc", "src"]
117+
[tool.ruff.format]
118+
quote-style = "double"
119+
indent-style = "space"
120+
docstring-code-format = true
121+
docstring-code-line-length = "dynamic"
122+
123+
[tool.ruff.lint]
124+
select = [
125+
"E", # pycodestyle, see https://docs.astral.sh/ruff/rules/#pycodestyle-e-w
126+
"F", # pyflakes, see https://docs.astral.sh/ruff/rules/#pyflakes-f
127+
"I", # isort, see https://docs.astral.sh/ruff/rules/#isort-i
128+
"N", # pep8-naming, see https://docs.astral.sh/ruff/rules/#pep8-naming-n
129+
"PTH", # flake8-use-pathlib, https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth
130+
"TD", # flake8-todos, https://docs.astral.sh/ruff/rules/#flake8-todos-td
131+
]
132+
ignore = [
133+
"TD002", # Missing author in TODOs comment
134+
]
135+
136+
[tool.ruff.lint.pydocstyle]
137+
convention = "numpy"
138+
139+
[tool.ruff.lint.isort]
140+
combine-as-imports = true
141+
force-sort-within-sections = true

src/pyansys/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
"""PyAnsys general package __init__ file."""
24+
2425
import importlib.metadata as importlib_metadata
2526

2627
# Read from the pyproject.toml

tools/catsitemap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def generate_sitemap_index(project_names: list, dest_path: Path) -> None:
103103

104104
if __name__ == "__main__":
105105
# Create path
106-
folder_path = Path(".") / "sitemaps"
106+
folder_path = Path() / "sitemaps"
107107
folder_path.mkdir(parents=True, exist_ok=True)
108108

109109
# Get actual valid URLS and corresponding project names

tools/links.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
python links.py
99
"""
1010

11-
import os
11+
from pathlib import Path
1212
import re
1313

14-
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
14+
ROOT_DIR = Path(__file__).parent.parent
1515
"""Root directory of the project relative to this file."""
1616

17-
PYPROJECT_TOML_FILE = os.path.join(ROOT_DIR, "pyproject.toml")
17+
PYPROJECT_TOML_FILE = ROOT_DIR / "pyproject.toml"
1818
"""Path to pyproject.toml file."""
1919

20-
DOCS_DIRECTORY = os.path.join(ROOT_DIR, "doc", "source")
20+
DOCS_DIRECTORY = ROOT_DIR / "doc" / "source"
2121
"""Path to the documentation source directory"""
2222

2323
LINKS = {
@@ -98,7 +98,7 @@ def retrieve_major_minor(package: str):
9898
The major and minor versions of the package.
9999
100100
"""
101-
with open(PYPROJECT_TOML_FILE, "r") as file:
101+
with PYPROJECT_TOML_FILE.open("r") as file:
102102
content = file.read()
103103
pattern = r"\b" + re.escape(package) + r"==(\d+)\.(\d+)"
104104
match = re.search(pattern, content)
@@ -122,23 +122,20 @@ def search_and_replace(link: str, new_link: str):
122122
The link to replace the existing one.
123123
"""
124124
# Traverse the docs directory
125-
for root, _, files in os.walk(DOCS_DIRECTORY):
125+
for root, _, files in DOCS_DIRECTORY.walk():
126126
# Skip the _static subdirectory
127-
if "_static" in root.split(os.sep):
127+
if "_static" in root.parts:
128128
continue
129129

130130
# Process the files
131131
for file in files:
132-
file_path = os.path.join(root, file)
133-
with open(file_path, "r") as f:
134-
content = f.read()
132+
file_path = root / file
133+
content = file_path.read_text(encoding="utf-8")
135134

136135
# Search for the link in the content, replace and save
137136
if link in content:
138137
new_content = content.replace(link, new_link)
139-
140-
with open(file_path, "w") as f:
141-
f.write(new_content)
138+
file_path.write_text(new_content, "utf-8")
142139

143140
print(f"Replaced '{link}' with '{new_link}' in file: {file_path}") # noqa: E501
144141

0 commit comments

Comments
 (0)