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

Add attributes to tomllib.TOMLDecodeError #126175

Closed
hukkin opened this issue Oct 30, 2024 · 4 comments
Closed

Add attributes to tomllib.TOMLDecodeError #126175

hukkin opened this issue Oct 30, 2024 · 4 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@hukkin
Copy link
Contributor

hukkin commented Oct 30, 2024

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

@hukkin hukkin added the type-feature A feature request or enhancement label Oct 30, 2024
@hukkin
Copy link
Contributor Author

hukkin commented Oct 30, 2024

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.

@nineteendo
Copy link
Contributor

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

@picnixz picnixz added the stdlib Python modules in the Lib dir label Oct 30, 2024
@picnixz
Copy link
Contributor

picnixz commented Oct 30, 2024

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.

@hukkin
Copy link
Contributor Author

hukkin commented Oct 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants