8000 Improve invalid input type handling in `tomllib.loads` · Issue #124835 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Improve invalid input type handling in tomllib.loads #124835

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

Closed
hukkin opened this issue Oct 1, 2024 · 2 comments
Closed

Improve invalid input type handling in tomllib.loads #124835

hukkin opened this issue Oct 1, 2024 · 2 comments
Labels
type-feature A feature request or enhancement

Comments

@hukkin
Copy link
Contributor
hukkin commented Oct 1, 2024

Feature or enhancement

Proposal:

tomllib.loads only accepts str as input. Given bytes, it throws

TypeError: a bytes-like object is required, not 'str'

This is a bubbled up exception from the replace method on line

src = s.replace("\r\n", "\n")
which is otherwise fine, but the message has bytes and str the wrong way around, which can be very confusing. I propose catching the exception and raising a new TypeError with an improved message.

While at it, I also propose catching any AttributeErrors from this line of code and raising the same new TypeError instead. As a result, a sensible TypeError is raised in nearly all cases where incorrect type is passed in.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Original issue report by @mechsin on Tomli's issue tracker: hukkin/tomli#223
PR: #124587

Linked PRs

@StanFromIreland
Copy link
Contributor

Do we want to backport this change? It is partly a bug fix (error message is flipped). Maybe it could be split?

See also: #132655

@hauntsaninja
Copy link
Contributor
hauntsaninja commented Apr 17, 2025

I didn't backport because it can change the exception type (also because slightly less confusing error messages that aren't super critical are little more feature than bug, changes to error messages do sometimes break folks)

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

No branches or pull requests

3 participants
0