Repository navigation
Add attributes to tomllib.TOMLDecodeError #126175
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on 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.
I would use keywords arguments defaulting to None. Also see PEP-387.
Reacted by Petr Viktorin, Taneli Hukkinen and Bénédikt Tran- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on 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
tomllibextension (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.
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.JSONDecodeErrorhas.- added a commit that references this issue
on Nov 13, 2024
Feature or enhancement
Proposal:
Add the same attributes to
tomllib.TOMLDecodeErrorthatjson.JSONDecodeErroralso 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
__init__args #126428