Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply use-pathlib rules #10060

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

Secrus
Copy link
Member

@Secrus Secrus commented Jan 16, 2025

Since we use os.path.join as a string glue method, I silenced those cases.

Summary by Sourcery

Refactor file operations to use pathlib.

Enhancements:

  • Replace open() with Path.read_text() and Path.write_text() for file I/O.
  • Use Path.mkdir() instead of os.mkdir.
  • Use Path.exists() instead of os.path.exists().
  • Use Path.is_symlink() instead of os.path.islink().
  • Use Path.unlink() instead of os.unlink().
  • Use Path.name instead of os.path.basename().

Copy link

sourcery-ai bot commented Jan 16, 2025

Reviewer's Guide by Sourcery

This pull request refactors file operations in the tests/packages/test_locker.py, tests/utils/env/test_env_manager.py, tests/console/commands/test_config.py, src/poetry/utils/helpers.py, src/poetry/utils/env/env_manager.py, tests/installation/test_chef.py, tests/utils/test_cache.py, src/poetry/inspection/info.py, src/poetry/packages/locker.py, tests/console/commands/env/helpers.py, tests/console/commands/self/test_add_plugins.py, tests/console/commands/self/test_self_command.py, tests/console/commands/test_init.py, tests/installation/test_wheel_installer.py, pyproject.toml, src/poetry/config/config.py, src/poetry/masonry/builders/editable.py, src/poetry/utils/env/virtual_env.py, tests/console/commands/self/test_remove_plugins.py, tests/installation/test_executor.py, tests/masonry/builders/test_editable_builder.py, and tests/utils/env/test_env.py files to use the pathlib module. It also updates the pyproject.toml to include the flake8-use-pathlib plugin.

Class diagram showing pathlib migration changes

classDiagram
    class Path {
      +exists()
      +chmod()
      +unlink()
      +is_symlink()
      +parent
      +readlink()
      +joinpath()
      +open()
    }

    class OldImplementation {
      +os.path.exists()
      +os.chmod()
      +os.unlink()
      +os.path.islink()
      +os.path.dirname()
      +os.readlink()
      +os.path.join()
      +open()
    }

    note for Path "New pathlib.Path implementation"
    note for OldImplementation "Previous os.path implementation"

    OldImplementation ..> Path : Migrated to
Loading

File-Level Changes

Change Details Files
Replaced file operations with pathlib equivalents.
  • Used Path.read_text() and Path.write_text() for file reading and writing.
  • Used Path.mkdir() for directory creation.
  • Used Path.unlink() for file deletion.
  • Used pathlib methods for path manipulation.
  • Used Path.exists() for file existence checks.
  • Used Path.is_symlink() for symlink checks.
  • Used Path.open() for opening files.
  • Used Path.joinpath() for joining paths.
tests/packages/test_locker.py
tests/utils/env/test_env_manager.py
src/poetry/utils/helpers.py
src/poetry/utils/env/env_manager.py
tests/installation/test_chef.py
tests/utils/test_cache.py
src/poetry/inspection/info.py
src/poetry/packages/locker.py
tests/console/commands/env/helpers.py
tests/console/commands/self/test_add_plugins.py
tests/console/commands/self/test_self_command.py
tests/console/commands/test_init.py
tests/installation/test_wheel_installer.py
src/poetry/masonry/builders/editable.py
src/poetry/utils/env/virtual_env.py
tests/console/commands/self/test_remove_plugins.py
tests/installation/test_executor.py
tests/masonry/builders/test_editable_builder.py
tests/utils/env/test_env.py
Added flake8-use-pathlib plugin to pyproject.toml.
  • Included flake8-use-pathlib in the extend-select section.
pyproject.toml
Updated os.path.join usage in config command tests.
  • Added noqa comment to silence flake8-use-pathlib warning.
tests/console/commands/test_config.py
src/poetry/config/config.py

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Secrus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

src/poetry/utils/env/env_manager.py Outdated Show resolved Hide resolved
@Secrus Secrus marked this pull request as draft January 16, 2025 14:00
@Secrus
Copy link
Member Author

Secrus commented Jan 16, 2025

@sourcery-ai review

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Secrus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

@Secrus Secrus requested a review from a team January 18, 2025 16:44
@Secrus Secrus marked this pull request as ready for review January 18, 2025 16:45
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We have skipped reviewing this pull request. It looks like we've already reviewed the commit fe0df70 in this pull request.

Copy link
Member

@abn abn left a comment

Choose a reason for hiding this comment

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

Some initial comments.

@@ -110,7 +110,7 @@ class Config:
"virtualenvs": {
"create": True,
"in-project": None,
"path": os.path.join("{cache-dir}", "virtualenvs"),
"path": os.path.join("{cache-dir}", "virtualenvs"), # noqa: PTH118
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why we can't replace this?

Copy link
Member Author

Choose a reason for hiding this comment

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

from what I understand it would be str(Path(...)) which doesn't look like a good use of resources, while os.path operates on strings all the way

Copy link
Member

Choose a reason for hiding this comment

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

Add a comment maybe to avoid someone swapping it later.

But I reckon since it's not always a valid path (eg: template) it's okay.

@@ -493,14 +493,14 @@ def create_venv(
# but others can symlink *to* the venv Python,
# so we can't just use sys.executable.
# So we just check every item in the symlink tree (generally <= 3)
p = os.path.normcase(sys.executable)
p = Path(os.path.normcase(sys.executable))
Copy link
Member

Choose a reason for hiding this comment

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

Is normcase required with Path?

paths.append(p)

p_venv = os.path.normcase(str(venv))
if any(p.startswith(p_venv) for p in paths):
if any(str(p).startswith(p_venv) for p in paths):
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this use relative?

@@ -194,7 +194,7 @@ def test_add_existing_plugin_warns_about_no_operation(
installed: TestRepository,
) -> None:
pyproject = SelfCommand.get_default_system_pyproject_file()
with open(pyproject, "w", encoding="utf-8", newline="") as f:
with pyproject.open("w", encoding="utf-8", newline="") as f:
Copy link
Member

Choose a reason for hiding this comment

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

Can we use write_text? Similar in a few other places.

Copy link
Member

Choose a reason for hiding this comment

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

Only after dropping support for Python 3.9, I assume:

Changed in version 3.10: The newline parameter was added.

https://docs.python.org/3/library/pathlib.html#pathlib.Path.write_text

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly this. I can add a TODO for when we drop 3.9 to change that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants