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

frozenset[str] fields in API get shuffled #1348

Open
srabraham opened this issue Oct 31, 2024 · 2 comments
Open

frozenset[str] fields in API get shuffled #1348

srabraham opened this issue Oct 31, 2024 · 2 comments
Assignees

Comments

@srabraham
Copy link
Member

It's kind of unfortunate that we end up shuffling collections in API fields. For example, I noticed this in the ranger_handles field in the incidents endpoint (like this one: https://ranger-ims-staging.burningman.org/ims/api/events/2023/incidents/), and it's presumably the same for incident_types (https://ranger-ims-staging.burningman.org/ims/api/incident_types/).

Even if you start with sorted values in _db.py, they eventually get shuffled before encoding, because frozenset[str] are unordered, e.g. below

FYI @wsanchez

>>> for val in frozenset(("a","b","c")):
...     print(val)
...
a
c
b
@srabraham
Copy link
Member Author

This should be about as easy as replacing the freezeStrings function. frozenset(some_collection) jumbles up the order the collection. The below approach maintains the order and still returns an immutable, hashable collection with duplicates removed.

def freezeStrings(strings: Iterable[str]) -> tuple[str]:
    deduped = {s: None for s in strings}
    return tuple(s for s in deduped)

@srabraham
Copy link
Member Author

Probably the right move is this change to _converters.py, so that fields can still be made immutable and hashable, but without losing ordering (or having duplicates removed).

This change might just work on its own, but there's a bunch of cleanup to do, mostly switching type hints. Also this would touch the auth component, which makes me want to tread carefully.

diff --git a/src/ims/model/_convert.py b/src/ims/model/_convert.py
index 14b817ca..500c78fe 100644
--- a/src/ims/model/_convert.py
+++ b/src/ims/model/_convert.py
@@ -20,19 +20,19 @@
 Converters
 """

-from collections.abc import Iterable
+from collections.abc import Iterable, Sequence
 from datetime import datetime as DateTime


 __all__ = ()


-def freezeIntegers(integers: Iterable[int]) -> frozenset[int]:
-    return frozenset(integers)
+def freezeIntegers(integers: Iterable[int]) -> Sequence[int]:
+    return tuple(i for i in integers)


-def freezeStrings(strings: Iterable[str]) -> frozenset[str]:
-    return frozenset(strings)
+def freezeStrings(strings: Iterable[str]) -> Sequence[str]:
+    return tuple(s for s in strings)

srabraham added a commit that referenced this issue Nov 12, 2024
These currently show up in the order they're returned from the API,
which is effectively random, due to this issue:
#1348

We might as well do client-side sorting even if we also do something
on the server-side too.
srabraham added a commit that referenced this issue Nov 12, 2024
These currently show up in the order they're returned from the API,
which is effectively random, due to this issue:
#1348

We might as well do client-side sorting even if we also do something
on the server-side too.
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

No branches or pull requests

1 participant