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

Improve invalid input type handling in tomllib.loads #124835

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

Improve invalid input type handling in tomllib.loads #124835

hukkin opened this issue Oct 1, 2024 · 0 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

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

2 participants