Skip to content

feat: implement platform-specific path handling in mapdl.directory - #4079

Merged
germa89 merged 6 commits into
mainfrom
feat/using-path-objects-on-directory
Jul 7, 2025
Merged

feat: implement platform-specific path handling in mapdl.directory#4079
germa89 merged 6 commits into
mainfrom
feat/using-path-objects-on-directory

Conversation

@germa89

@germa89 germa89 commented Jul 7, 2025

Copy link
Copy Markdown
Collaborator

Description

As the title.

So we can do:

>>> err_file = mapdl.directory / 'file.err'
PurePosixPath('/local/simulation/file.err')

Issue linked

Close #4078

Checklist

Summary by Sourcery

Introduce OS-aware path handling for mapdl.directory by wrapping paths in pathlib.PureWindowsPath or pathlib.PurePosixPath, and update tests accordingly

New Features:

  • Implement platform-specific path handling in mapdl.directory by returning PureWindowsPath or PurePosixPath based on the running OS

Enhancements:

  • Wrap directory getter and setter to always normalize internal paths through the new _wrap_directory helper

Tests:

  • Update existing tests to use pathlib.PurePath comparisons instead of Path
  • Add test_directory_pathlib to verify that mapdl.directory returns a PurePath and supports OS-specific path joining

Copilot AI review requested due to automatic review settings July 7, 2025 10:45
@germa89
germa89 requested a review from a team as a code owner July 7, 2025 10:45
@germa89 germa89 self-assigned this Jul 7, 2025
@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 7, 2025

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR introduces platform-aware path handling by wrapping the Mapdl working directory string in an OS-specific pathlib.PurePath object via a new helper and updates the directory getter/setter to use it, with corresponding adjustments to existing tests and a new test for join behavior.

Class diagram for updated Mapdl directory path handling

classDiagram
    class MapdlCore {
        +platform: str
        +_path: pathlib.PurePath
        +directory: property
        +directory(path: Union[str, pathlib.Path])
        +_wrap_directory(path: str) pathlib.PurePath
    }
    MapdlCore --> "1" pathlib.PurePath : uses
    class pathlib.PurePath
    class pathlib.PureWindowsPath
    class pathlib.PurePosixPath
    pathlib.PureWindowsPath --|> pathlib.PurePath
    pathlib.PurePosixPath --|> pathlib.PurePath
    MapdlCore ..> pathlib.PureWindowsPath : returns if platform=="windows"
    MapdlCore ..> pathlib.PurePosixPath : returns if platform=="linux" or fallback
Loading

Class diagram for _wrap_directory helper method

classDiagram
    class MapdlCore {
        +_wrap_directory(path: str) pathlib.PurePath
    }
    class pathlib.PureWindowsPath
    class pathlib.PurePosixPath
    MapdlCore ..> pathlib.PureWindowsPath : returns on Windows
    MapdlCore ..> pathlib.PurePosixPath : returns on Linux/Other
Loading

File-Level Changes

Change Details Files
Introduce _wrap_directory helper to return OS-specific PurePath
  • Added _wrap_directory(path) method to determine OS and return PureWindowsPath or PurePosixPath
  • Included fallback warning for unknown OS
  • Centralized path conversion logic in one place
src/ansys/mapdl/core/mapdl_core.py
Refactor directory property to leverage the wrapper
  • Replaced direct assignments to self._path with calls to _wrap_directory in getter
  • Updated setter to wrap incoming paths before storing
  • Adjusted return type to PurePath instead of raw string
src/ansys/mapdl/core/mapdl_core.py
Revise tests for PurePath return and joining behavior
  • Converted assertions to use pathlib.Path or PurePath instead of Path alone
  • Updated existing tests to compare str(mapdl.directory) with expected paths
  • Added new test_directory_pathlib to verify return type and OS-specific join behavior
tests/test_mapdl.py

Assessment against linked issues

Issue Objective Addressed Explanation
#4078 Return a Pathlib.Path object from mapdl.directory that takes into account the OS of MAPDL.
#4078 Allow users to manipulate MAPDL paths using / operator.

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

@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 they look great!


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 tests/test_mapdl.py
Comment thread tests/test_mapdl.py Outdated
@github-actions github-actions Bot added the new feature Request or proposal for a new feature label Jul 7, 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 adds platform-aware path handling for the mapdl.directory property, so it now returns a PurePath specific to Windows or POSIX and updates tests to use pathlib.

  • Introduces _wrap_directory to produce PureWindowsPath or PurePosixPath depending on mapdl.platform.
  • Updates the directory getter/setter to call _wrap_directory instead of storing raw strings.
  • Refactors tests to import pathlib and assert against PurePath behavior.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/test_mapdl.py Switched from Path import to pathlib, added a new test to assert PurePath behavior.
src/ansys/mapdl/core/mapdl_core.py Added _wrap_directory and updated directory getter/setter to return PurePath objects.
Comments suppressed due to low confidence (1)

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

  • The directory property is annotated to return str but now returns a pathlib.PurePath. Update the return type annotation to pathlib.PurePath (or Union[str, pathlib.PurePath]) to reflect the new behavior.
            path = path.replace("\\", "/")

Comment thread tests/test_mapdl.py
@codecov

codecov Bot commented Jul 7, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.80%. Comparing base (e45c8a4) to head (b78c04b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4079      +/-   ##
==========================================
- Coverage   91.84%   91.80%   -0.05%     
==========================================
  Files         187      187              
  Lines       15023    15032       +9     
==========================================
+ Hits        13798    13800       +2     
- Misses       1225     1232       +7     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@germa89

germa89 commented Jul 7, 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

@germa89
germa89 enabled auto-merge (squash) July 7, 2025 16:13
@germa89
germa89 merged commit 2512f7c into main Jul 7, 2025
@germa89
germa89 deleted the feat/using-path-objects-on-directory branch July 7, 2025 16:19
germa89 added a commit that referenced this pull request Jul 12, 2025
…4079)

* feat: implement platform-specific path handling in _MapdlCore and update tests

* chore: adding changelog file 4079.miscellaneous.md [dependabot-skip]

* fix: handle uninitialized MAPDL platform in _wrap_directory method

* Update tests/test_mapdl.py

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* fix: avoid string operations on Path object

* test: enhance directory path handling with platform-specific parameterization

---------

Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new feature Request or proposal for a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MAPDL path manipulation

3 participants