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

Fix tabulate col size in case of empty cell #7988

Merged
merged 2 commits into from
Apr 9, 2020

Conversation

McSinyx
Copy link
Contributor

@McSinyx McSinyx commented Apr 6, 2020

Previously, the size is no less than len(str(None)) == 4.

This commit also add type hint and docstring to the function.

Edit: I'm not sure if this change is trivial, please tell me if news is required.

@McSinyx McSinyx force-pushed the tabulate branch 3 times, most recently from 8fff440 to 692b39c Compare April 6, 2020 10:31
Previously, the size is no less than len(str(None)) == 4.
This commit also add type hint and docstring to the function.
@xavfernandez
Copy link
Member

xavfernandez commented Apr 6, 2020

Do we agree that this case never happens in pip's code base ?
Each column has at least an header with a length > 4.
(if that is indeed the case, it means that this change is not user facing and qualifies as "trivial")

A simple parametrized test would also be a nice touch 👼

@McSinyx
Copy link
Contributor Author

McSinyx commented Apr 7, 2020

Do we agree that this case never happens in pip's code base? Each column has at least an header with a length > 4.

Sorry I did not give the context, I was preparing for GH-7975 and there might be (I don't think it's settled) some narrow columns. Additionally, should we move the function to utils.misc?

A simple parametrized test would also be a nice touch 👼

That won't be a problem!

@xavfernandez
Copy link
Member

Moving it to utils.misc is a good idea if you plan to use it for pip find/discover (and don't forget the tests ;) )

@uranusjr
Copy link
Member

uranusjr commented Apr 7, 2020

If I’m not mistaken, the problem here is basically len(str(None)) == 4, right? Would it be good enough to add type hints so the caller always passes in an empty string, not None?

Scratch that, I see the problem here is zip_longest returning None by default. The fix (suppling fillvalue) makes sense to me.

@McSinyx
Copy link
Contributor Author

McSinyx commented Apr 7, 2020

That was the problem, which will be fixedby this PR. Consider the current implementation:

sizes = [0] * max(len(x) for x in vals)
for row in vals:
    sizes = [max(s, len(str(c))) for s, c in zip_longest(sizes, row)]

None comes from zip_longest, and I'm not aware of an elegant way to hint dynamic length of multiple iterables (i.e. the rows). Plus in this PR tries to simplify the logic which at that time was probably more of a proof of concept.

Fly away writing some tests for this after a full day of long and hard lectures.

Edit: Just want to point out how long it takes me to respond that uranusjr found the anwser himself 😆

@pradyunsg pradyunsg added the type: enhancement Improvements to functionality label Apr 8, 2020
@pradyunsg
Copy link
Member

Trivial is fine, since there's not really going to be any signficant user facing changes here IMO.

@McSinyx
Copy link
Contributor Author

McSinyx commented Apr 9, 2020

Since the change is quite trivial (pun intended), may I get this merged now?

@xavfernandez xavfernandez merged commit ea9cb06 into pypa:master Apr 9, 2020
@xavfernandez
Copy link
Member

Thanks @McSinyx

@McSinyx McSinyx deleted the tabulate branch April 19, 2020 08:11
@lock lock bot added the auto-locked Outdated issues that have been locked by automation label May 20, 2020
@lock lock bot locked as resolved and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation type: enhancement Improvements to functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants