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

Use zip for iterating over the formatted values #206

Merged
merged 1 commit into from Jun 23, 2022

Conversation

okapies
Copy link
Contributor

@okapies okapies commented Jun 22, 2022

Fix to use zip for iterating over the formatted values. It prevents retrieving formatted_value by index especially when it is Iterable but not Sequence.

Ref. #204.

@aminalaee
Copy link
Owner

aminalaee commented Jun 22, 2022

I think we could also update is_iterable to isinstance(x, Sequence) or isinstance(x, InstrumentedList).
The main reason we switched from Sequence to Iterable was that SQLAlchemy InstrumentedList was not a Sequence and now the Iterable conflict with strings is making it more complicated.

Now this change is not very explicitly showing the root-cause that we're dealing with strings, I think we need to fix in the right place.

@okapies
Copy link
Contributor Author

okapies commented Jun 22, 2022

By definition, Sequence is a trait both of Reversible and Collection with __getitem__, index and count. In this sense, InstrumentedList is actually a Sequence even if it is not a subclass of it. And thanks to abc module, you don't need to check if the value is an InstrumentedList apart from inspecting it is a Sequence:

>>> from collections.abc import Sequence
>>> from sqlalchemy.orm.collections import InstrumentedList
>>> isinstance(InstrumentedList(), Sequence)
True

But, choosing the minimum interface for treating value is generally good. If we just need to iterate over the value to render, we can use Iterable not Sequence. It also gives a lot of flexibility to the users when implementing format_*.

On the other hand, if we plan to use __getitem__, __contains__ and __len__ (i.e. random access functionalities) in our implementation in the future, we should choose Sequence. It depends on the requirement for the design of this library.

@okapies
Copy link
Contributor Author

okapies commented Jun 22, 2022

Note that str is also a Sequence, so we still need to check if it is not a string or bytes.

>>> from collections.abc import Sequence
>>> isinstance("foo", Sequence)
True

@okapies
Copy link
Contributor Author

okapies commented Jun 22, 2022

Anyway, I think merging this PR will not affect your design decision because Iterable (accepted by zip) is less strict than Sequence.

@aminalaee
Copy link
Owner

@okapies Yes you're right about the definitions, my comment was not really accurate.
Previous instead of is_iterable it was is_list and we were checking isinstance(x, list) but because there was an issue distinguishing SQLAlchemy InstrumentedList we exactly switched to abc Sequence and Iterable.

But since isinstance(InstrumentedList(), list) == True then I'm not sure why it should be is_iterable when it can be is_list simply. Let me dig a bit into the older PRs.

@aminalaee
Copy link
Owner

I know this won't discard your PR, using zip would still make it more readable in template, it will just make the type checking of is_iterable or is_list strict.

@okapies
Copy link
Contributor Author

okapies commented Jun 23, 2022

Ok. Anyway, could you please release a new version including #204? It is a show-stopper for our app.

@aminalaee
Copy link
Owner

Ok. Anyway, could you please release a new version including #204? It is a show-stopper for our app.

Yes sure, I'll merge these and do the release.

Copy link
Owner

@aminalaee aminalaee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR 👍

@aminalaee aminalaee merged commit 6552759 into aminalaee:main Jun 23, 2022
@okapies okapies deleted the feature/iterable-formatted-value branch June 23, 2022 07:04
@okapies
Copy link
Contributor Author

okapies commented Jun 23, 2022

🙇

@aminalaee aminalaee mentioned this pull request Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants