Skip to content

Add attributes to tomllib.TOMLDecodeError #126175

Description

@hukkin

Feature or enhancement

Proposal:

Add the same attributes to tomllib.TOMLDecodeError that json.JSONDecodeError also has, namely: msg, doc, pos, lineno, colno. This will help user facing apps show customized errors to users.

This draft PR in Tomli shows the proposed changes.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/69546

Linked PRs

Activity

  1. hukkin commented on Oct 30, 2024

    @hukkin
    ContributorAuthor

    Pinging codeowners @hauntsaninja and @encukou

    Thoughts on my concern in Discourse about the change breaking TOMLDecodeError init with no arguments would be much appreciated:

    Unfortunately, this changes signature of TOMLDecodeError so breaks any users instantiating the error. Not sure why anyone would do that though? Some less breaking options are using keyword arguments, or dynamically injecting attributes after instantiation.

  2. nineteendo commented on Oct 30, 2024

    @nineteendo
    Contributor

    I would use keywords arguments defaulting to None. Also see PEP-387.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 30, 2024
  4. picnixz commented on Oct 30, 2024

    @picnixz
    Member

    Unfortunately, this changes signature of TOMLDecodeError so breaks any users instantiating the error. Not sure why anyone would do that though? Some less breaking options are using keyword arguments, or dynamically injecting attributes after instantiation.

    You can instantiate those errors if you create a custom tomllib extension (e.g., to handle extended syntax not supported by TOML but you still want to use the same exceptions for that).

    I would use keywords arguments defaulting to None

    Agreed.

  5. hukkin commented on Oct 30, 2024

    @hukkin
    ContributorAuthor

    Thanks all for the feedback! I've updated my PoC draft PR in Tomli to be fully backwards compatible. It also adds a deprecation warning for use of positional arguments, which is simple to remove if folks think it's unnecessary. With it I think we could eventually move to the same exact signature that json.JSONDecodeError has.

  6. added a commit that references this issue on Nov 13, 2024
  7. added a commit that references this issue on Dec 8, 2024
  8. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions