Skip to content

refactor: using Path method for joining paths. - #4101

Merged
germa89 merged 16 commits into
mainfrom
refactor/using-path-methods-to-join-paths
Jul 16, 2025
Merged

refactor: using Path method for joining paths.#4101
germa89 merged 16 commits into
mainfrom
refactor/using-path-methods-to-join-paths

Conversation

@germa89

@germa89 germa89 commented Jul 14, 2025

Copy link
Copy Markdown
Collaborator

Description

As the title. Refactor to use the / for joining paths.

Issue linked

NA but using #4079

Checklist

Summary by Sourcery

Refactor path handling to use pathlib.Path and the / operator across the codebase, update the directory property type and tests accordingly, and enhance decorator typing.

Enhancements:

  • Standardize file path manipulations by replacing os.path.join with pathlib.Path and the / operator
  • Change the directory property to return a pathlib.PurePath instead of a string
  • Introduce ParamSpec and TypeVar typing for the supress_logging decorator

Tests:

  • Update tests to use the new Path-based directory usage and adjust assertions accordingly

@germa89 germa89 self-assigned this Jul 14, 2025
Copilot AI review requested due to automatic review settings July 14, 2025 11:34
@germa89
germa89 requested a review from a team as a code owner July 14, 2025 11:34
@ansys-reviewer-bot

Copy link
Copy Markdown
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

@sourcery-ai

sourcery-ai Bot commented Jul 14, 2025

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR refactors the codebase to use pathlib.Path’s '/' operator for path joining and file operations, updates the directory property to return a PurePath, improves typing in the supress_logging decorator, and initializes the hostname attribute in MapdlGRPC.

Class diagram for updated directory handling and supress_logging decorator

classDiagram
    class MapdlCore {
        +directory: pathlib.PurePath
        +_wrap_directory(path: str) pathlib.PurePath
    }
    class MapdlGRPC {
        +_hostname: Optional[str]
        +directory: pathlib.PurePath
    }
    class MapdlExtended {
        +directory: pathlib.PurePath
    }
    class Parameters {
        +_mapdl: MapdlCore
    }
    class supress_logging {
        <<function>>
        +__call__(func: Callable[P, R]) -> Callable[P, R]
    }
    MapdlGRPC --|> MapdlCore
    Parameters --> MapdlCore : uses _mapdl
    MapdlExtended --|> MapdlCore
Loading

File-Level Changes

Change Details Files
Integrate pathlib.Path for path joining and file operations
  • Replaced os.path.join calls with Path / filename expressions
  • Updated os.path.isfile/os.path.isdir, glob.glob to accept Path objects
  • Adjusted open(), shutil.copy, shutil.copytree to work with Path inputs
src/ansys/mapdl/core/mapdl_grpc.py
src/ansys/mapdl/core/mapdl_core.py
src/ansys/mapdl/core/mapdl_extended.py
src/ansys/mapdl/core/parameters.py
tests/test_mapdl.py
tests/test_misc.py
tests/test_grpc.py
tests/test_krylov.py
tests/test_plotting.py
Change directory property to return pathlib.PurePath
  • Updated return type annotation of directory property
  • Adjusted internal logic to use PurePath instead of str
src/ansys/mapdl/core/mapdl_core.py
Enhance supress_logging decorator with proper typing
  • Introduced ParamSpec 'P' and TypeVar 'R'
  • Updated wrapper signature to use P.args, P.kwargs and return R
src/ansys/mapdl/core/misc.py
Initialize hostname attribute in MapdlGRPC
  • Added self._hostname: Optional[str] in the init method
src/ansys/mapdl/core/mapdl_grpc.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@germa89 germa89 changed the title refactor: using Path / method for joining paths. refactor: using Path method for joining paths. Jul 14, 2025
@germa89

germa89 commented Jul 14, 2025

Copy link
Copy Markdown
Collaborator Author

@pyansys-ci-bot LGTM

@pyansys-ci-bot pyansys-ci-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approving this PR because germa89 said so in here 😬

LGTM

@github-actions github-actions Bot added the enhancement Improve any current implemented feature label Jul 14, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors path handling to use the / operator on pathlib PurePath objects instead of os.path.join for joining paths across tests and core modules.

  • Replace os.path.join(...) with pathlib-style division in tests and source code
  • Change directory property return type from str to pathlib.PurePath
  • Update decorator typing in misc.py to use ParamSpec and TypeVar

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_plotting.py Use mapdl.directory / last_png for constructing PNG path
tests/test_misc.py Replace os.path.join with mapdl.directory / file_
tests/test_mapdl.py Refactor join logic to use pathlib division for file paths
tests/test_krylov.py Use mapdl.directory / ... when building full file path
tests/test_grpc.py Swap os.path.join for temp_dir / ... in download tests
src/ansys/mapdl/core/parameters.py Convert local filename to str(pathlib.PurePath / filename)
src/ansys/mapdl/core/misc.py Add ParamSpec/TypeVar imports and type hints for decorator
src/ansys/mapdl/core/mapdl_grpc.py Replace os.path.join with self.directory / ... and use glob on paths
src/ansys/mapdl/core/mapdl_extended.py Use self.directory / filename for listing
src/ansys/mapdl/core/mapdl_core.py Change directory return type to PurePath and update joins
Comments suppressed due to low confidence (4)

src/ansys/mapdl/core/mapdl_core.py:526

  • Changing the directory property return type from str to pathlib.PurePath is a breaking API change. Consider preserving backward compatibility or clearly documenting this impact.
    def directory(self) -> pathlib.PurePath:

src/ansys/mapdl/core/mapdl_grpc.py:2128

  • In _get_file_path, you now assign a Path but the method signature indicates a str return. Consider casting to str(...) or updating the return annotation to avoid type inconsistencies.
                filename = self.directory / fname

src/ansys/mapdl/core/mapdl_grpc.py:2534

  • [nitpick] Passing a pathlib.Path directly into glob.glob may rely on implicit conversion. For clarity and consistency, wrap the path in str(...).
                )

tests/test_plotting.py:1024

  • [nitpick] This makes last_png a Path when local but leaves it a str for remote. Consider wrapping in str(...) to keep the returned type consistent across both branches.
        last_png = mapdl.directory / last_png

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @germa89 - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • glob.glob expects a string pattern, not a Path object. (link)

General comments:

  • The directory property now returns a PurePath, but many downstream operations (like .exists() or .resolve()) aren’t available on PurePath—consider returning a full pathlib.Path instead to preserve filesystem methods.
  • There’s still a mix of os.path and pathlib calls scattered throughout—refactor to use Path methods exclusively (e.g. Path.glob, Path.is_file, Path.exists) for consistency and clarity.
  • You’ve introduced ParamSpec in the supress_logging decorator which requires Python 3.10+; if you need to maintain earlier Python support, consider importing ParamSpec from typing_extensions or providing a fallback.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The directory property now returns a PurePath, but many downstream operations (like `.exists()` or `.resolve()`) aren’t available on PurePath—consider returning a full pathlib.Path instead to preserve filesystem methods.
- There’s still a mix of os.path and pathlib calls scattered throughout—refactor to use Path methods exclusively (e.g. Path.glob, Path.is_file, Path.exists) for consistency and clarity.
- You’ve introduced ParamSpec in the `supress_logging` decorator which requires Python 3.10+; if you need to maintain earlier Python support, consider importing ParamSpec from typing_extensions or providing a fallback.

## Individual Comments

### Comment 1
<location> `src/ansys/mapdl/core/mapdl_core.py:2535` </location>
<code_context>
     def _screenshot_path(self):
         """Return last filename based on the current jobname"""
-        filenames = glob.glob(os.path.join(self.directory, f"{self.jobname}*.png"))
+        filenames = glob.glob(self.directory / f"{self.jobname}*.png")
         filenames.sort()
         return filenames[-1]
</code_context>

<issue_to_address>
glob.glob expects a string pattern, not a Path object.

Convert the Path to a string with str() before passing it to glob.glob to avoid a TypeError.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/ansys/mapdl/core/mapdl_core.py Outdated
Comment thread tests/test_mapdl.py
Comment thread tests/test_mapdl.py
Comment thread tests/test_misc.py
Comment thread tests/test_plotting.py
Comment thread src/ansys/mapdl/core/mapdl_core.py Outdated
Comment thread src/ansys/mapdl/core/mapdl_grpc.py
Comment thread src/ansys/mapdl/core/mapdl_core.py Outdated
germa89 and others added 2 commits July 14, 2025 13:50
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@germa89
germa89 enabled auto-merge (squash) July 14, 2025 12:27
@codecov

codecov Bot commented Jul 15, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 76.59574% with 11 lines in your changes missing coverage. Please review.

Project coverage is 91.82%. Comparing base (1350840) to head (de9731f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4101      +/-   ##
==========================================
+ Coverage   91.73%   91.82%   +0.08%     
==========================================
  Files         187      187              
  Lines       15042    15047       +5     
==========================================
+ Hits        13799    13817      +18     
+ Misses       1243     1230      -13     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@germa89
germa89 merged commit c3a21b4 into main Jul 16, 2025
74 of 77 checks passed
@germa89
germa89 deleted the refactor/using-path-methods-to-join-paths branch July 16, 2025 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improve any current implemented feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants