Skip to content

Conversation

jhlegarreta
Copy link
Contributor

@jhlegarreta jhlegarreta commented Jul 5, 2025

Call write once instead of looping by concatenating all strings that need to be written.

Avoid using writelines as ImageOpener objects do not have that attribute.

Fixes:

-                for val in bvals:
-                    fid.write(f'{val} ')
+                fid.writelines(f'{val} ' for val in bvals)

and other similar errors raised for example in:
https://github.com/nipy/nibabel/actions/runs/16092302666/job/45410655457?pr=1422#step:7:23

Documentation:
https://docs.astral.sh/ruff/rules/for-loop-writes/

Copy link

codecov bot commented Jul 5, 2025

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.34%. Comparing base (b5ecf48) to head (db08acf).
⚠️ Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
nibabel/cmdline/parrec2nii.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1423   +/-   ##
=======================================
  Coverage   95.34%   95.34%           
=======================================
  Files         209      209           
  Lines       29780    29776    -4     
  Branches     3357     3356    -1     
=======================================
- Hits        28393    28390    -3     
+ Misses        948      947    -1     
  Partials      439      439           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhlegarreta jhlegarreta force-pushed the FixStyleErrors branch 2 times, most recently from 4a5baf1 to 8e1fdb1 Compare July 5, 2025 22:32
@jhlegarreta jhlegarreta changed the title STYLE: Use writelines instead of write in a loop STYLE: Call write once instead of looping Jul 5, 2025
@jhlegarreta jhlegarreta force-pushed the FixStyleErrors branch 2 times, most recently from 3d7421b to e3961ec Compare July 5, 2025 22:59
Call `write` once instead of looping by concatenating all strings that
need to be written.

Avoid using `writelines` as `ImageOpener` objects do not have that
attribute.

Fixes:
```
-                for val in bvals:
-                    fid.write(f'{val} ')
+                fid.writelines(f'{val} ' for val in bvals)
```

and other similar errors raised for example in:
https://github.com/nipy/nibabel/actions/runs/16092302666/job/45410655457?pr=1422#step:7:23

Documentation:
https://docs.astral.sh/ruff/rules/for-loop-writes/
@jhlegarreta
Copy link
Contributor Author

Typecheck issues being addressed in PR #1424.

@effigies This is ready to be merged.

@effigies
Copy link
Member

@jhlegarreta Thanks for your patience. I've looked into this. While the bvals one is almost certainly a trivial increase in memory usage, the purpose of writing the blocks of zeros using a loop was to avoid increasing memory usage. The writelines() change recommended by ruff does not concatenate internally, instead making multiple os.write() calls, so passing a generator expression should not cause a slowdown or increase in memory use.

Applied in #1433.

@effigies effigies closed this Sep 18, 2025
@jhlegarreta jhlegarreta deleted the FixStyleErrors branch September 19, 2025 12:10
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