Skip to content

Use [] and {} instead of list() and dict()#8748

Merged
ericspod merged 2 commits intoProject-MONAI:devfrom
benediktjohannes:patch-5
Apr 4, 2026
Merged

Use [] and {} instead of list() and dict()#8748
ericspod merged 2 commits intoProject-MONAI:devfrom
benediktjohannes:patch-5

Conversation

@benediktjohannes
Copy link
Copy Markdown
Contributor

Description

See also #8747

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 39c85328-5eee-42f4-bfe3-fb019ba692c3

📥 Commits

Reviewing files that changed from the base of the PR and between 8dafee7 and 9764d2b.

📒 Files selected for processing (1)
  • monai/transforms/inverse.py
✅ Files skipped from review due to trivial changes (1)
  • monai/transforms/inverse.py

📝 Walkthrough

Walkthrough

Updated monai/transforms/inverse.py in track_transform_meta to use literal container initializers when reading nested trace-status data: statuses = pend.get(TraceKeys.STATUSES, {}) and messages = statuses.get(TraceStatusKeys.PENDING_DURING_APPLY, []). The code then appends the pending message, writes it back to statuses[TraceStatusKeys.PENDING_DURING_APPLY], stores statuses into info[TraceKeys.STATUSES], and pushes the applied operation. No API or behavior changes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning Description references a related issue but lacks the required 'Description' section explaining the changes. Only checklist items are provided. Add a brief description explaining why list() and dict() are being replaced with [] and {} in the Description section.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately summarizes the main change: replacing list() and dict() calls with literal forms [] and {}.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@benediktjohannes
Copy link
Copy Markdown
Contributor Author

@coderabbitai do you agree with these changes?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 22, 2026

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get the diff for the PR
git diff origin/dev...HEAD -- monai/transforms/inverse.py

Length of output: 792


🏁 Script executed:

#!/bin/bash
# Get more context around the changed lines
cat monai/transforms/inverse.py | grep -n -B 5 -A 5 "get(TraceKeys.STATUSES"

Length of output: 551


🏁 Script executed:

#!/bin/bash
# Check if there are any other occurrences of dict() or list() in the same file
rg -n '\b(dict|list)\(\)' monai/transforms/inverse.py

Length of output: 0


@benediktjohannes Yes, I agree with these changes. Using [] and {} instead of list() and dict() is:

  1. More Pythonic - follows PEP 8 style guidelines
  2. Slightly faster - literal syntax avoids name lookups
  3. More concise - improves readability

The changes are functionally equivalent and safe in this context (default arguments to .get() methods). The PR correctly identified and updated all instances in the file.

@benediktjohannes
Copy link
Copy Markdown
Contributor Author

Wow! I am amazed how cool this tool is, I thought that this would definitely not work, just to chat here with the AI, very nice and helpful for workflows! Appreciate it! 👍

ericspod added a commit that referenced this pull request Feb 27, 2026
…r.py (#8749)

### Description
See also #8747 and #8748 

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
@ericspod
Copy link
Copy Markdown
Member

Hi @benediktjohannes I commented in another PR about replacing names like this with literal syntax. I suppose this saves a name lookup but I wonder if the interpreter doesn't replace these things automatically anyway. Do you actually see any speedup here? There's probably many other places in MONAI that should have this done as well.

@benediktjohannes
Copy link
Copy Markdown
Contributor Author

Hi @benediktjohannes I commented in another PR about replacing names like this with literal syntax. I suppose this saves a name lookup but I wonder if the interpreter doesn't replace these things automatically anyway. Do you actually see any speedup here? There's probably many other places in MONAI that should have this done as well.

I think (back then (it's already been 4 weeks now, so I'm not quite sure), I've searched for all those list() things) and it should be worth merging, see here https://towardsdatascience.com/no-and-list-are-different-in-python-8940530168b0/

Copy link
Copy Markdown
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

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

We can merge this too, but as I said it's something that would only matter in very high frequency code.

@benediktjohannes
Copy link
Copy Markdown
Contributor Author

Thanks, See #8747 please

@ericspod ericspod merged commit 55c724f into Project-MONAI:dev Apr 4, 2026
26 checks passed
ericspod added a commit that referenced this pull request Apr 5, 2026
### Description
Follow-up for #8748 and so on (I'll combine these changes in future 👍)

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [ ] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [ ] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

Signed-off-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
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