-
Notifications
You must be signed in to change notification settings - Fork 60
Fix #668: Safely handle empty custom_atlases.conf file #681
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
base: main
Are you sure you want to change the base?
Fix #668: Safely handle empty custom_atlases.conf file #681
Conversation
b2d73aa to
49b8741
Compare
alessandrofelder
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kalpana-pardeep-rassani
I unfortunately haven't had time for an in-depth review (hopefully by the end of this week) - apologies. I have some high-level comments for you though which are hopefully helpful. Let me know what you think.
brainglobe_atlasapi/list_atlases.py
Outdated
| # --- FIX APPLIED HERE --- | ||
| # Safely get the custom atlases data. If the 'atlases' section does not | ||
| # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {} | ||
| # instead of crashing. | ||
| custom_atlas_data = custom_atlases.get("atlases", {}) | ||
|
|
||
| return {**official_atlases["atlases"], **custom_atlas_data} | ||
|
|
||
|
|
||
| # The get_atlases_lastversions function below this should remain unchanged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # --- FIX APPLIED HERE --- | |
| # Safely get the custom atlases data. If the 'atlases' section does not | |
| # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {} | |
| # instead of crashing. | |
| custom_atlas_data = custom_atlases.get("atlases", {}) | |
| return {**official_atlases["atlases"], **custom_atlas_data} | |
| # The get_atlases_lastversions function below this should remain unchanged. | |
| # Safely get the custom atlases data. If the 'atlases' section does not | |
| # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {} | |
| # instead of crashing. | |
| custom_atlas_data = custom_atlases.get("atlases", {}) | |
| return {**official_atlases["atlases"], **custom_atlas_data} | |
I think some of these comments relate to the PR, and not the code itself - so I don't think they belong in the code. Do you agree (I may have missed something)?
brainglobe_atlasapi/list_atlases.py
Outdated
| return table | ||
|
|
||
|
|
||
| def test_empty_config_file_no_crash(tmp_path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test looks reasonable at first glance but I think it should go somewhere in the tests folder with the other tests. It will be picked up by pytest there!
brainglobe_atlasapi/list_atlases.py
Outdated
| """ | ||
|
|
||
| import re | ||
| import warnings # Adding this line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| import warnings # Adding this line | |
| import warnings |
I don't think this comment is useful - it is very generic and not related to the line of code AFAICT
cc46f34 to
6d53dd6
Compare
|
Thank you very much for taking the time to review this, especially with
your busy schedule! I really appreciate the high-level comments.
I have just applied an update to the branch to address this:
Removed all explanatory comments
Integrated the safe-access logic (.get("atlases", {})) cleanly inside the
correct function body to maintain the original code structure.
Request for Further Clarification
If you have a moment, could you please provide a tiny bit more detail on
whether this new structure resolves the issue? Also, if you see any
problems with the warning placement or the logic in the unit test that I
added, please let me know.
I am committed to ensuring this fix is merged correctly and ready for your
final approval! Thank you again for the guidance.
…On Mon, Dec 1, 2025, 7:31 PM Alessandro Felder ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Thanks @kalpana-pardeep-rassani
<https://github.com/kalpana-pardeep-rassani>
I unfortunately haven't had time for an in-depth review (hopefully by the
end of this week) - apologies. I have some high-level comments for you
though which are hopefully helpful. Let me know what you think.
------------------------------
In brainglobe_atlasapi/list_atlases.py
<#681 (comment)>
:
> + # --- FIX APPLIED HERE ---
+ # Safely get the custom atlases data. If the 'atlases' section does not
+ # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {}
+ # instead of crashing.
+ custom_atlas_data = custom_atlases.get("atlases", {})
+
+ return {**official_atlases["atlases"], **custom_atlas_data}
+
+
+# The get_atlases_lastversions function below this should remain unchanged.
⬇️ Suggested change
- # --- FIX APPLIED HERE ---
- # Safely get the custom atlases data. If the 'atlases' section does not
- # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {}
- # instead of crashing.
- custom_atlas_data = custom_atlases.get("atlases", {})
-
- return {**official_atlases["atlases"], **custom_atlas_data}
-
-
-# The get_atlases_lastversions function below this should remain unchanged.
+ # Safely get the custom atlases data. If the 'atlases' section does not
+ # exist (e.g., file is empty), custom_atlases.get("atlases", {}) returns {}
+ # instead of crashing.
+ custom_atlas_data = custom_atlases.get("atlases", {})
+
+ return {**official_atlases["atlases"], **custom_atlas_data}
I think some of these comments relate to the PR, and not the code itself -
so I don't think they belong in the code. Do you agree (I may have missed
something)?
------------------------------
In brainglobe_atlasapi/list_atlases.py
<#681 (comment)>
:
> @@ -228,3 +241,32 @@ def add_atlas_to_row(atlas, info, table, show_local_path=False):
table.add_row(*row)
return table
+
+
+def test_empty_config_file_no_crash(tmp_path):
This test looks reasonable at first glance but I think it should go
somewhere in the tests folder with the other tests. It will be picked up
by pytest there!
------------------------------
In brainglobe_atlasapi/list_atlases.py
<#681 (comment)>
:
> @@ -4,6 +4,7 @@
"""
import re
+import warnings # Adding this line
⬇️ Suggested change
-import warnings # Adding this line
+import warnings
I don't think this comment is useful - it is very generic and not related
to the line of code AFAICT
—
Reply to this email directly, view it on GitHub
<#681 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BGKRCRMWIMQLRVL4QW4IJ6L37RGMTAVCNFSM6AAAAACNPIEYL2VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTKMRVGI3TKMZRGM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
6d53dd6 to
2749295
Compare
|
I don't think you've addressed any of my comments with your latest changes @kalpana-pardeep-rassani Most importantly, AFAICT, the test you wrote is in the wrong place, and will therefore not be run by |
… all test and pre-commit style issues
708a630 to
b4e3c86
Compare
|
those 'windows' and 'mac' (pull requests) are cancelled during test, how do i tackle this issue, please guide.. and also these skipped pull requesets what to do with them? |
|
I've re-run the tests. |
alessandrofelder
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kalpana-pardeep-rassani - there is more work needed on the tests. They could be simpler. I hope my comments are helpful.
| return dict(official_atlases["atlases"]) | ||
| return {**official_atlases["atlases"], **custom_atlases["atlases"]} | ||
|
|
||
| # --- FIX APPLIED HERE --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # --- FIX APPLIED HERE --- |
This will not mean anything after this issue is fixed - it's too generic and makes the code harder to interpret. So I think it should be remove.
|
|
||
| def test_get_downloaded_atlases(): | ||
| @pytest.fixture | ||
| def mock_atlas_dir(tmp_path, mocker): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the functionality in this fixture is already implemented in https://github.com/brainglobe/brainglobe-atlasapi/blob/main/tests/conftest.py and ready for you to use here, so I would suggest
- removing it here
- refactoring your test to use the existing functionality
so we don't have to look after duplicate code.
| assert expected_print in captured.out | ||
|
|
||
|
|
||
| def test_empty_custom_config_no_crash(tmp_path: Path, mocker): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is sufficient to only add this test, and not change any of the other tests - they test things that are unrelated to the issue we are trying to fix.
I also think this test could be a lot simpler. You only need to check that
- when you have an empty
custom_atlases.conffile...
*... then the function doesn't crash, and possibly returns some existing atlases that are not custom.
It shouldn't need to be this long, or involve so many mocks. You can re-use some existing fixtures from conftest.py as I suggested in a previous comment.
|
Hi @kalpana-pardeep-rassani are you able to get back to this PR? |
Before submitting a pull request (PR), please read the contributing guide.
Please fill out as much of this template as you can, but if you have any problems or questions, just leave a comment and we will help out :)
Description
What is this PR
Why is this PR needed?
The current implementation crashes with a
KeyError: 'atlases'if the localcustom_atlases.conffile exists but is empty (contains no sections). This prevents thebrainglobe listcommand from running.What does this PR do?
This PR modifies the
get_all_atlases_lastversionsfunction inbrainglobe_atlasapi/list_atlases.pyto use a safe.get("atlases", {})method when reading the custom atlas configuration. This ensures that an empty dictionary is used if the 'atlases' section is missing, thus preventing the crash.References
Fixes #668
How has this PR been tested?
The fix was tested locally by ensuring that the
brainglobe listcommand runs successfully without crashing when the~/.brainglobe/custom_atlases.conffile is present but completely empty.Is this a breaking change?
No. This is a fix for an existing bug and does not change any public API or required functionality.
Does this PR require an update to the documentation?
No.
Checklist:
Note on Test Failures:
All code changes related to Issue #668 (empty config file) have been verified locally. When running the full test suite, I encountered 2 ConnectionErrors (due to the GIN server being unreachable) and 1 FileNotFoundError (due to missing local test data), which are external to this fix. The patch itself is clean.