-
Notifications
You must be signed in to change notification settings - Fork 42
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 TOMLDecodeError #238
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #238 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 510 527 +17
Branches 93 97 +4
=========================================
+ Hits 510 527 +17 ☔ View full report in Codecov by Sentry. |
0cd94a0
to
ce99b06
Compare
class TOMLDecodeError(ValueError):
"""An error raised if a document is not valid TOML."""
def __init__(self, msg: str = None, doc: str = None, pos: Pos = None, *args):
if (
args
or not isinstance(msg, str)
or isinstance(doc, str)
or not isinstance(pos, Pos)
):
warnings.warn(
"Free-form arguments for TOMLDecodeError are deprecated. "
"Please set 'msg', 'doc' and 'pos' arguments.",
DeprecationWarning,
stacklevel=2,
)
if pos is not None:
args = pos, *args
if doc is not None:
args = doc, *args
if msg is not None:
args = msg, *args
super().__init__(*args)
else:
lineno = doc.count("\n", 0, pos) + 1
if lineno == 1:
colno = pos + 1
else:
colno = pos - doc.rindex("\n", 0, pos)
if pos >= len(doc):
coord_repr = "end of document"
else:
coord_repr = f"line {lineno}, column {colno}"
errmsg = f"{msg} (at {coord_repr})"
super().__init__(self, errmsg)
self.msg = msg
self.doc = doc
self.pos = pos
self.lineno = lineno
self.colno = colno (Not sure if typechecker will allow |
I think that would break
Notably, the changing of If this is acceptable, let's do it. An alternative is what's currently in this PR, and allowing
I think this should be no issue at all. We can type annotate the non-deprecated signature, and
I agree. |
You could use a private sentinel, but this is overkill, the backwards compatibility policy isn't that strict. |
I read PEP 387 and my interpretation is it doesn't seem to be lenient in a way that's applicable here.
If a change is breaking, it seems very strict, no exceptions. Where it does seem lenient is interpreting whether or not a change is breaking:
If someone shows up and says we broke their I understand if in practice the PEP is applied pragmatically, and it's ok to break use cases that seem extremely rare or low importance, etc, but to me it seems like the PEP doesn't say it's allowed. Should it? |
Yup, there's a judgment call. The chances of someone using The original version of PR would break code that's not common, but it's likely to exist -- using one positional string (code copied from |
Thanks a lot for the reviews and comments! Yeah, I agree this makes sense. I was mainly wondering whether making such judgment calls should be explicitly allowed by the PEP, as my interpretation was they may currently not be. I've pushed another commit that includes a couple tests. As far as I'm aware, this revision doesn't break anything except the highly unlikely I'll make a sibling PR to cpython in the upcoming days. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was mainly wondering whether making such judgment calls should be explicitly allowed by the PEP, as my interpretation was they may currently not be.
Well, here I am essentially betting that no one, so far, wrote code that would be broken by this -- i.e. the “preexisting code” doesn't exist.
If I'm wrong, we'll need to revert.
Adds the same attributes to
TOMLDecodeError
thatJSONDecodeError
has.Created a discussion about this https://discuss.python.org/t/69546
edit: and a cpython issue python/cpython#126175