From 81832e1dc73243e2b4737942bb865e56612678b5 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 4 Jan 2025 06:09:07 +0000 Subject: [PATCH] Use pathlib in further places --- sphinx/environment/collectors/asset.py | 2 +- sphinx/ext/autosummary/__init__.py | 2 +- sphinx/ext/autosummary/generate.py | 17 +++++++++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sphinx/environment/collectors/asset.py b/sphinx/environment/collectors/asset.py index 8c49da91c..d4cb24d11 100644 --- a/sphinx/environment/collectors/asset.py +++ b/sphinx/environment/collectors/asset.py @@ -93,7 +93,7 @@ class ImageCollector(EnvironmentCollector): # into a single directory) for imgpath in candidates.values(): app.env.note_dependency(imgpath) - if not os.access(os.path.join(app.srcdir, imgpath), os.R_OK): + if not os.access(app.srcdir / imgpath, os.R_OK): logger.warning( __('image file not readable: %s'), imgpath, diff --git a/sphinx/ext/autosummary/__init__.py b/sphinx/ext/autosummary/__init__.py index f103e4921..3b51817b9 100644 --- a/sphinx/ext/autosummary/__init__.py +++ b/sphinx/ext/autosummary/__init__.py @@ -886,7 +886,7 @@ def process_generate_options(app: Sphinx) -> None: ] for entry in genfiles[:]: - if not os.path.isfile(os.path.join(app.srcdir, entry)): + if not (app.srcdir / entry).is_file(): logger.warning(__('autosummary_generate: file not found: %s'), entry) genfiles.remove(entry) diff --git a/sphinx/ext/autosummary/generate.py b/sphinx/ext/autosummary/generate.py index f2ce63ed4..64dea102b 100644 --- a/sphinx/ext/autosummary/generate.py +++ b/sphinx/ext/autosummary/generate.py @@ -530,12 +530,15 @@ def generate_autosummary_docs( logger.info(__('[autosummary] writing to %s'), output_dir) if base_path is not None: - sources = [os.path.join(base_path, filename) for filename in sources] + base_path = Path(base_path) + source_paths = [base_path / filename for filename in sources] + else: + source_paths = list(map(Path, sources)) template = AutosummaryRenderer(app) # read - items = find_autosummary_in_files(sources) + items = find_autosummary_in_files(source_paths) # keep track of new files new_files: list[Path] = [] @@ -628,7 +631,9 @@ def generate_autosummary_docs( # -- Finding documented entries in files --------------------------------------- -def find_autosummary_in_files(filenames: list[str]) -> list[AutosummaryEntry]: +def find_autosummary_in_files( + filenames: Sequence[str | os.PathLike[str]], +) -> list[AutosummaryEntry]: """Find out what items are documented in source/*.rst. See `find_autosummary_in_lines`. @@ -643,7 +648,7 @@ def find_autosummary_in_files(filenames: list[str]) -> list[AutosummaryEntry]: def find_autosummary_in_docstring( name: str, - filename: str | None = None, + filename: str | os.PathLike[str] | None = None, ) -> list[AutosummaryEntry]: """Find out what items are documented in the given object's docstring. @@ -670,7 +675,7 @@ def find_autosummary_in_docstring( def find_autosummary_in_lines( lines: list[str], module: str | None = None, - filename: str | None = None, + filename: str | os.PathLike[str] | None = None, ) -> list[AutosummaryEntry]: """Find out what items appear in autosummary:: directives in the given lines. @@ -710,7 +715,7 @@ def find_autosummary_in_lines( if m: toctree = m.group(1) if filename: - toctree = os.path.join(os.path.dirname(filename), toctree) + toctree = str(Path(filename).parent / toctree) continue m = template_arg_re.match(line)