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 a limit to string.zfill so it will raise an error to lengths bigger that the int in the brackets #120350

Closed
Unknownuserfrommars opened this issue Jun 11, 2024 · 4 comments
Labels
type-feature A feature request or enhancement

Comments

@Unknownuserfrommars
Copy link

Unknownuserfrommars commented Jun 11, 2024

Feature or enhancement

Proposal:

In the string.zfill() function, it won't process string-numbers (e.g.: "1234") with a length bigger than the input int.

# Will do:
# 1.
a = 123
a_zfill = str(a).zfill(5)
# Returns: '00123'
# 2.
b = 1234
b_zfill = str(b).zfill(4)
# Returns: '1234'
# Quite Weird:
c = 123456
c_zfill = str(c).zfill(4)
# Still returns: '123456'

This is WEIRD. the zfill value is 4, but the length is 6. It should raise an error. ValueError, probably.

Only thing: it didn't.

Sometimes when processing datasets, we have to align the digits. However, with this, we can't know if we actually did it right. For example, str.zfill(4) works for most but 1 value in a dataset. We won't be able to know that. (C'mon, no one want's to bother using max(len(dataset[column])) everytime, right?) Raising an error is straightforward.
Example:
ValueError: Your zfill value is {num}, but the max length is {max_len}, which is bigger.
There u go, fast, straightforward, easy-to-fix.

:)

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:

No response

@Unknownuserfrommars Unknownuserfrommars added the type-feature A feature request or enhancement label Jun 11, 2024
@ericvsmith
Copy link
Member

This would break existing, working code, so we cannot make this change.

I'd suggest you write a wrapper that you can call which raises an exception for the cases you're concerned about.

Another option would be to add a parameter to str.zfill, which if set, would raise an exception if it detects the condition you're concerned about. I don't think this would be accepted, but you never know. If you want to pursue this, you should start a discussion at https://discuss.python.org/c/ideas/6

@sobolevn
Copy link
Member

sobolevn commented Jun 11, 2024

This is a breaking change, it would need to have at least a flag, something like .zfill(' ', strict=True) with False as default.

Update: oups, I missed that @ericvsmith said the same thing, sorry :)

@serhiy-storchaka
Copy link
Member

str.zfill() is an old and weird method, and I do not think that it would be added now if it did not exist. Printf-style and new style formatting are more powerful.

But in this case it is consistent with other str methods like just() and formatting operations. None of them raise an exception if the result is larger than the width. It is considered better to get an overflow in the formatted output than do not get any output. If you want to handle this differently, write your own code.

@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2024
@sobolevn
Copy link
Member

sobolevn commented Jun 12, 2024

@Unknownuserfrommars if you still want to pursue this, please open a discussion first: https://discuss.python.org/c/ideas/6

Looks like it is not so straight-forward :)

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

4 participants