Skip to content

Fix archive-users example#51

Open
mhxion wants to merge 1 commit intoelabftw:masterfrom
mhxion:fix-archive-users
Open

Fix archive-users example#51
mhxion wants to merge 1 commit intoelabftw:masterfrom
mhxion:fix-archive-users

Conversation

@mhxion
Copy link
Contributor

@mhxion mhxion commented Mar 19, 2026

Following the issue #50: I updated the body that reflects the latest openAPI spec, so archiving the user works.

Summary by CodeRabbit

  • Documentation
    • Updated the user archival example script to demonstrate an improved implementation approach for archiving users.

@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

Walkthrough

The archival operation within the user script undergoes restructuring. The method transitions from dispatching a simple action parameter to submitting a team-based payload structure, introducing a USER_TEAM_ID constant and repositioning the archived state specification within an is_archived target field. Minor formatting adjustments accompany these functional modifications.

Changes

Cohort / File(s) Summary
User Archival API Refactor
examples/18-archive-users.py
Modified the patch request payload from a simple {'action': 'archive'} to a team-based structure with patchuser2team containing team and is_archived target fields. Introduced USER_TEAM_ID = 1 constant. Updated string formatting from single to double quotes and adjusted import ordering.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

An archive request, restructured with care,
From action to team, a new schema fair,
Constants emerge like stars in the night,
One file transformed, the path forward bright.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix archive-users example' directly and specifically addresses the main change in the pull request, which updates the archive-users example to work with the latest OpenAPI specification.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can use TruffleHog to scan for secrets in your code with verification capabilities.

Add a TruffleHog config file (e.g. trufflehog-config.yml, trufflehog.yml) to your project to customize detectors and scanning behavior. The tool runs only when a config file is present.

@mhxion mhxion mentioned this pull request Mar 19, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
examples/18-archive-users.py (1)

31-31: A minor observation, if I may.

There appears to be an extra blank line here (line 31) that creates inconsistent spacing before the usersApi initialization. The blank line at line 22 after USER_TEAM_ID and this one create slightly uneven visual grouping.

✨ Suggested cleanup
 USER_TEAM_ID = 1

-
 # Load the users api
 usersApi = elabapi_python.UsersApi(api_client)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/18-archive-users.py` at line 31, Remove the extra blank line that
creates inconsistent spacing before the usersApi initialization to keep visual
grouping consistent with the earlier blank line after USER_TEAM_ID; locate the
block where USER_TEAM_ID is defined and where usersApi is instantiated (symbols:
USER_TEAM_ID and usersApi) and delete the redundant empty line so spacing is
uniform.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@examples/02-patch-metadata-per-category.py`:
- Around line 53-58: The try/except around the call that does
json.dumps(metadata) is incorrect because json.dumps raises TypeError for
non-serializable objects (not JSONDecodeError); update the exception handling
around itemsApi.patch_item(..., body={"metadata": json.dumps(metadata)}) to
either remove the try/except entirely (since metadata is a static simple dict)
or catch TypeError instead of JSONDecodeError, and if you remove the block also
remove any now-unused JSONDecodeError import.

---

Nitpick comments:
In `@examples/18-archive-users.py`:
- Line 31: Remove the extra blank line that creates inconsistent spacing before
the usersApi initialization to keep visual grouping consistent with the earlier
blank line after USER_TEAM_ID; locate the block where USER_TEAM_ID is defined
and where usersApi is instantiated (symbols: USER_TEAM_ID and usersApi) and
delete the redundant empty line so spacing is uniform.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 915a2a14-d1e2-49e4-a3af-55b2886bacb6

📥 Commits

Reviewing files that changed from the base of the PR and between 08e3189 and bac6ea7.

📒 Files selected for processing (2)
  • examples/02-patch-metadata-per-category.py
  • examples/18-archive-users.py

Comment on lines +53 to +58
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except JSONDecodeError as e:
raise ValueError(
"'metadata' passed to API request body contains invalid JSON."
) from e
Copy link

@coderabbitai coderabbitai bot Mar 19, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Number One, we have a logic error in our exception handling.

The json.dumps() function serializes Python objects to JSON strings—it does not raise JSONDecodeError. That exception is raised by json.loads() when decoding invalid JSON. When json.dumps() encounters a non-serializable object (e.g., a datetime or custom class), it raises TypeError.

Since metadata here is a static dictionary with only basic types (strings, integers, booleans, lists, dicts), json.dumps() will never fail in practice. This try-except block is therefore unreachable code. If the intent is to guard against future changes to metadata containing non-serializable values, you should catch TypeError instead.

🛠️ Proposed fix: catch the correct exception or remove unreachable handling

Option 1: Catch the correct exception type

-        except JSONDecodeError as e:
-            raise ValueError(
-                "'metadata' passed to API request body contains invalid JSON."
-            ) from e
+        except TypeError as e:
+            raise ValueError(
+                "'metadata' contains a value that cannot be serialized to JSON."
+            ) from e

Option 2: Remove the unnecessary error handling entirely (since metadata is static)

     if not item.metadata:
         print(f"Patching item {item.id}")
-        try:
-            itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
-        except JSONDecodeError as e:
-            raise ValueError(
-                "'metadata' passed to API request body contains invalid JSON."
-            ) from e
+        itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})

If Option 2 is chosen, you may also remove the unused import:

 import json
-from json import JSONDecodeError
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except JSONDecodeError as e:
raise ValueError(
"'metadata' passed to API request body contains invalid JSON."
) from e
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except TypeError as e:
raise ValueError(
"'metadata' contains a value that cannot be serialized to JSON."
) from e
Suggested change
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except JSONDecodeError as e:
raise ValueError(
"'metadata' passed to API request body contains invalid JSON."
) from e
if not item.metadata:
print(f"Patching item {item.id}")
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
Suggested change
try:
itemsApi.patch_item(item.id, body={"metadata": json.dumps(metadata)})
except JSONDecodeError as e:
raise ValueError(
"'metadata' passed to API request body contains invalid JSON."
) from e
import json
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/02-patch-metadata-per-category.py` around lines 53 - 58, The
try/except around the call that does json.dumps(metadata) is incorrect because
json.dumps raises TypeError for non-serializable objects (not JSONDecodeError);
update the exception handling around itemsApi.patch_item(..., body={"metadata":
json.dumps(metadata)}) to either remove the try/except entirely (since metadata
is a static simple dict) or catch TypeError instead of JSONDecodeError, and if
you remove the block also remove any now-unused JSONDecodeError import.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point @bot. @NicolasCARPi Let me know if you want me to fix this as well.

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@mhxion mhxion force-pushed the fix-archive-users branch from bac6ea7 to 2adb8af Compare March 19, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant