Skip to content

Replace OrderedDict #1625

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

Merged
merged 3 commits into from
May 19, 2022
Merged

Conversation

living180
Copy link
Contributor

Replace most usages of OrderedDict with plain Python dicts since as of Python 3.7, dicts are guaranteed to maintain insertion order. Also make a couple of small improvements related to sorting dicts.

Since Python 3.7, regular dicts preserve insertion order, meaning most
usages of OrderedDict can be replaced with regular dicts.  Add a comment
for the one case that cannot in the DebugToolbar class.
return signing.Signer(salt=cls.salt).sign(
json.dumps({key: force_str(value) for key, value in items})
json.dumps({key: force_str(value) for key, value in data.items()})
Copy link
Member

Choose a reason for hiding this comment

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

I was a bit worried about this. I thought that maybe the string was hashed and that changing the order of keys may lead to invalid hashes but this doesn't seem to be the case since we're not hashing, only signing and unsigning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another consideration is that when the signing code was originally added, DjDT still supported Python 3.6, which although in practice maintained dict insertion order, did not guarantee it. So to guarantee that the tests were correct, it was probably necessary to sort the data before signing at that point in time (no longer necessary now that Python 3.7 is the minimum supported version).


DATA = {"value": "foo", "date": datetime(2020, 1, 1, tzinfo=timezone.utc)}
SIGNED_DATA = f'{{"date": "2020-01-01 00:00:00+00:00", "value": "foo"}}:{SIGNATURE}'
SIGNED_DATA = f'{{"value": "foo", "date": "2020-01-01 00:00:00+00:00"}}:{SIGNATURE}'
Copy link
Member

Choose a reason for hiding this comment

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

Detail: I'd prefer it if you reversed the order of value and date in the dictionary below; this would neatly show that the signature stays the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

living180 added 2 commits May 18, 2022 16:23
The signature does not depend on a canonical representation of the data,
so don't bother sorting it.  Tweak the data for the tests as a
consequence.  However, note that the tests are still deterministic since
as of version 3.7, Python guarantees dictionary order.
Passing a key argument of the form `key=lambda item: item[0]` to
sorted() when sorting dict items is unneeded, because tuples already
compare element-wise, and the first elements are known to be unique
since they are dictionary keys.
@living180 living180 force-pushed the replace_ordereddict branch from 5e6ae18 to bd4132f Compare May 18, 2022 13:23
@matthiask matthiask merged commit f071cff into django-commons:main May 19, 2022
@matthiask
Copy link
Member

Thanks!

@living180 living180 deleted the replace_ordereddict branch May 19, 2022 11:27
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.

2 participants