Skip to content

Fix: indicate location of toml file in question when raising a TOMLDecodeError#10647

Closed
JaviMaligno wants to merge 3 commits intopython-poetry:mainfrom
JaviMaligno:fix/toml-error-location-10270
Closed

Fix: indicate location of toml file in question when raising a TOMLDecodeError#10647
JaviMaligno wants to merge 3 commits intopython-poetry:mainfrom
JaviMaligno:fix/toml-error-location-10270

Conversation

@JaviMaligno
Copy link
Copy Markdown
Contributor

@JaviMaligno JaviMaligno commented Dec 9, 2025

Fixes #10270

Summary by Sourcery

Bug Fixes:

  • Include the path to the problematic TOML configuration file in the runtime error raised when local configuration parsing fails.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 9, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Wraps reading of the local TOML configuration in a try/except so that TOML parsing errors are re-raised as PoetryRuntimeError with the path of the problematic config file included in the message.

Sequence diagram for error handling when loading local TOML configuration

sequenceDiagram
    actor Developer
    participant CLI as PoetryCLI
    participant Factory as Factory
    participant Config as Config
    participant LocalFile as LocalConfigFile
    participant TOML as TOMLParser

    Developer->>CLI: run_command()
    CLI->>Factory: create_poetry(io)
    Factory->>LocalFile: path
    Factory->>LocalFile: read()
    LocalFile->>TOML: parse_toml(content)
    alt TOML parsing succeeds
        TOML-->>LocalFile: parsed_data
        LocalFile-->>Factory: parsed_data
        Factory->>Config: merge(parsed_data)
        Config-->>Factory: merged_config
        Factory-->>CLI: poetry_instance
    else TOML parsing fails
        TOML-->>LocalFile: TOMLError
        LocalFile-->>Factory: TOMLError
        Factory->>Factory: catch TOMLError
        Factory-->>CLI: raise PoetryRuntimeError("Invalid TOML configuration file <path>: <TOMLError>")
        CLI-->>Developer: display error with file path
    end
Loading

File-Level Changes

Change Details Files
Handle TOML parsing errors when loading the local configuration file and surface them as PoetryRuntimeError including the config path.
  • Wrap the call that merges the local TOML configuration into a try/except block catching TOMLError.
  • On TOMLError, raise PoetryRuntimeError with a message that includes the invalid configuration file path and the original error message while preserving the exception cause.
src/poetry/factory.py

Assessment against linked issues

Issue Objective Addressed Explanation
#10270 When a TOMLDecodeError occurs while loading any TOML file relevant to Poetry (e.g., the main project pyproject.toml, path dependency pyproject.toml files, or Poetry configuration TOML files), the error message should indicate which TOML file path is invalid. The PR only wraps config.merge(local_config_file.read()) in factory.py and rethrows a PoetryRuntimeError that includes the path of local_config_file. This improves error messages for invalid TOML in the Poetry configuration file, but it does not change the TOML loading path shown in the issue stack trace (which goes through poetry/core/pyproject/toml.py and tomli/_parser.py for pyproject.toml files and path dependencies). Therefore, TOMLDecodeError messages for invalid pyproject.toml files—particularly in path dependencies, which are the main focus of the issue—still will not indicate which TOML file is at fault.

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

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 there - I've reviewed your changes - here's some feedback:

  • The PR title and issue reference mention TOMLDecodeError, but the code catches TOMLError; consider aligning the exception type (or explicitly documenting why the broader exception is needed) to avoid confusion.
  • Raising PoetryRuntimeError where a TOMLError was previously propagated may change error-handling behavior for callers; consider whether the original exception type should be preserved or wrapped in a way that keeps compatibility with existing exception handling.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The PR title and issue reference mention `TOMLDecodeError`, but the code catches `TOMLError`; consider aligning the exception type (or explicitly documenting why the broader exception is needed) to avoid confusion.
- Raising `PoetryRuntimeError` where a `TOMLError` was previously propagated may change error-handling behavior for callers; consider whether the original exception type should be preserved or wrapped in a way that keeps compatibility with existing exception handling.

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.

@radoering
Copy link
Copy Markdown
Member

The title says something about the location of the toml file but the code does not any such information if I do not miss anything. I do not think this fixes #10270.

@JaviMaligno
Copy link
Copy Markdown
Contributor Author

Thanks for the feedback @radoering! You're right that the error message was missing the file location.

I've updated the code to include self.project_directory in the error message:

except TOMLError as e:
    raise PoetryRuntimeError(
        f"Problem processing TOML configuration in {self.project_directory}: {e}"
    ) from e

Now when users encounter a TOML parsing error, they'll see which project directory has the issue, making it easier to identify the problematic file.

@radoering
Copy link
Copy Markdown
Member

I think this still does not solve the issue described in #10270:

The issue was in a toml file for one of this project's path dependencies.

However, there already has been such a fix in python-poetry/poetry-core#734. This fix is included in Poetry 2.0. The issue was reported with Poetry 1.7.1. Actually, I think the issue has already been fixed and your change does not add any additional information. I closed the issue and will close this PR. Thank you anyway for the effort.

@radoering radoering closed this Dec 12, 2025
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

indicate location of toml file in question when raising a TOMLDecodeError

3 participants