Skip to content

use tempfile.TemporaryDirectory#337

Merged
radoering merged 1 commit intopython-poetry:mainfrom
dimbleby:temporary-directory
Dec 21, 2025
Merged

use tempfile.TemporaryDirectory#337
radoering merged 1 commit intopython-poetry:mainfrom
dimbleby:temporary-directory

Conversation

@dimbleby
Copy link
Copy Markdown
Contributor

@dimbleby dimbleby commented Apr 30, 2022

cf python-poetry/poetry#5522

but note that there's a testcase in this repository:

def test_utils_helpers_temporary_directory_readonly_file():
. So if the pipeline passes that should give us confidence that this is good,

Summary by Sourcery

Replace custom temporary directory and removal helpers with direct use of the standard library’s TemporaryDirectory across the codebase and tests.

Enhancements:

  • Refactor wheel builder and test utilities to use tempfile.TemporaryDirectory with ignore_cleanup_errors instead of the custom temporary_directory helper.
  • Remove the robust_rmtree and temporary_directory helper functions from poetry.core.utils.helpers as they are no longer used.

Tests:

  • Update masonry API tests and test fixtures to work with raw TemporaryDirectory paths via pathlib.Path conversion.
  • Remove unit tests specific to the temporary_directory and robust_rmtree helpers that have been deleted.

@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.5% 0.5% Duplication

@dimbleby
Copy link
Copy Markdown
Contributor Author

Looks like Windows and python3.7 are not happy with this, https://bugs.python.org/issue26660 presumably.

if and when we eventually get to do this, should probably remove the unit test case also - it wouldn't make much sense to have a unit test for tempfile function.

@neersighted
Copy link
Copy Markdown
Member

Re: implementing this now, one option is python-poetry/poetry#5522 (comment)

@dimbleby dimbleby force-pushed the temporary-directory branch from 4bedfd5 to e48e797 Compare August 16, 2022 16:22
@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@dimbleby dimbleby force-pushed the temporary-directory branch from ab2682a to 64d7c86 Compare March 18, 2023 19:25
@github-advanced-security
Copy link
Copy Markdown

You have successfully added a new SonarCloud configuration ``. As part of the setup process, we have scanned this repository and found no existing alerts. In the future, you will see all code scanning alerts on the repository Security tab.

@dimbleby
Copy link
Copy Markdown
Contributor Author

dimbleby commented Mar 18, 2023

rebased on #566 so that we no longer need to care about Windows python 3.7 bugs

@dimbleby dimbleby force-pushed the temporary-directory branch 2 times, most recently from 7ecf628 to 21caebb Compare March 18, 2023 19:35
@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@dimbleby dimbleby force-pushed the temporary-directory branch from 2919f80 to 1f96d36 Compare April 25, 2023 10:00
@dimbleby dimbleby force-pushed the temporary-directory branch 2 times, most recently from 6f33977 to c6e9cb6 Compare May 20, 2023 13:25
@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@dimbleby dimbleby force-pushed the temporary-directory branch from c6e9cb6 to 8e58a44 Compare October 28, 2023 16:04
@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@dimbleby dimbleby marked this pull request as draft October 28, 2023 16:06
@dimbleby dimbleby force-pushed the temporary-directory branch from 8e58a44 to 33307e9 Compare March 2, 2024 14:26
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 2, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@dimbleby dimbleby force-pushed the temporary-directory branch from 33307e9 to 431fd3c Compare May 4, 2025 20:54
@dimbleby dimbleby force-pushed the temporary-directory branch from 431fd3c to bc9dcc3 Compare December 20, 2025 11:20
@dimbleby dimbleby marked this pull request as ready for review December 20, 2025 11:25
@dimbleby
Copy link
Copy Markdown
Contributor Author

dimbleby commented Dec 20, 2025

now partner to python-poetry/poetry#7680

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Dec 20, 2025

Reviewer's Guide

Refactors temporary directory handling to use tempfile.TemporaryDirectory (with ignore_cleanup_errors=True) directly throughout the codebase and tests, while removing the now-redundant helper utilities and their tests.

Sequence diagram for wheel build metadata generation using TemporaryDirectory

sequenceDiagram
    participant WheelBuilder
    participant tempfile
    participant FileSystem

    WheelBuilder->>tempfile: TemporaryDirectory(ignore_cleanup_errors=True)
    activate tempfile
    tempfile-->>WheelBuilder: temp_dir_path

    WheelBuilder->>WheelBuilder: prepare_metadata(temp_dir_path)
    WheelBuilder->>FileSystem: write dist_info metadata into temp_dir_path

    deactivate tempfile
    tempfile->>FileSystem: cleanup temp_dir_path (ignore cleanup errors)
Loading

Updated class diagram for wheel building and helper utilities

classDiagram
    class WheelBuilder {
        _metadata_directory
        build()
        prepare_metadata(destination)
        _copy_file_scripts(zip_file)
        _copy_dist_info(zip_file, metadata_directory)
    }

    class TemporaryDirectory {
        ignore_cleanup_errors
        __enter__()
        __exit__(exc_type, exc_value, traceback)
    }

    WheelBuilder ..> TemporaryDirectory : uses

    class HelpersBefore {
        temporary_directory(args, kwargs)
        robust_rmtree(path, max_timeout)
        _on_rm_error(func, path, exc_info)
    }

    class HelpersAfter {
        combine_unicode(string)
        module_name(name)
        parse_requires(requires)
        readme_content_type(path)
    }

    HelpersBefore <|-- HelpersAfter : refactor removes temp and rmtree helpers
Loading

File-Level Changes

Change Details Files
Replace custom temporary_directory helper with direct usage of tempfile.TemporaryDirectory configured with ignore_cleanup_errors=True, updating call sites accordingly.
  • Update masonry API tests to use TemporaryDirectory as a context manager alongside cwd for build outputs, wrapping returned paths with Path where needed.
  • Adjust test fixtures and test utilities to construct temporary directories with tempfile.TemporaryDirectory(prefix=..., ignore_cleanup_errors=True).
  • Change wheel builder to use TemporaryDirectory(ignore_cleanup_errors=True) when preparing metadata if no metadata directory is provided.
tests/masonry/test_api.py
tests/conftest.py
tests/testutils.py
src/poetry/core/masonry/builders/wheel.py
Remove obsolete temporary_directory and robust_rmtree helpers and their tests from the helpers module.
  • Delete temporary_directory context manager and robust_rmtree implementation, along with related imports and private helpers from helpers.py.
  • Remove tests that exercised temporary_directory and robust_rmtree behavior, including cleanup and retry semantics.
  • Tidy helpers tests by dropping now-unused imports and TYPE_CHECKING-only test machinery.
src/poetry/core/utils/helpers.py
tests/utils/test_helpers.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. 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

Copy link
Copy Markdown

@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 - I've left some high level feedback:

  • There’s quite a bit of repetition of TemporaryDirectory(ignore_cleanup_errors=True) across tests and wheel building; consider introducing a small local helper/context manager in the test suite (and possibly in builders) to reduce duplication and make future changes to the tempdir behavior easier.
  • In places like tests/masonry/test_api.py, you repeatedly wrap tmp_dir with Path(tmp_dir) after the TemporaryDirectory context; assigning tmp_path = Path(tmp_dir) once per block would simplify the code and avoid redundant conversions.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- There’s quite a bit of repetition of `TemporaryDirectory(ignore_cleanup_errors=True)` across tests and wheel building; consider introducing a small local helper/context manager in the test suite (and possibly in builders) to reduce duplication and make future changes to the tempdir behavior easier.
- In places like `tests/masonry/test_api.py`, you repeatedly wrap `tmp_dir` with `Path(tmp_dir)` after the `TemporaryDirectory` context; assigning `tmp_path = Path(tmp_dir)` once per block would simplify the code and avoid redundant conversions.

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.

@dimbleby dimbleby force-pushed the temporary-directory branch from bc9dcc3 to cd6923b Compare December 20, 2025 11:39
@radoering radoering merged commit 129c2bc into python-poetry:main Dec 21, 2025
19 checks passed
@dimbleby dimbleby deleted the temporary-directory branch December 21, 2025 10:21
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