nibabel icon indicating copy to clipboard operation
nibabel copied to clipboard

pylance warning reportPrivateImportUsage

Open pcasl opened this issue 4 months ago • 10 comments

import nibabel data_path = 'path/to/your/data.nii' img = nibabel.load(data_path)

It raises up warning around the load. Any suggestions? Thanks

pcasl avatar Aug 11 '25 01:08 pcasl

What version of nibabel is installed?

effigies avatar Aug 11 '25 14:08 effigies

python3.13 nibabel 5.3.2

pcasl avatar Aug 12 '25 00:08 pcasl

Alright. Do you have a reproduction, including pylance invocation? I've never used the tool, so any help in getting started would be appreciated.

effigies avatar Aug 12 '25 00:08 effigies

in the vscode, when I use the script above, it shows up the warming. The problem is similar when I used obj.get_data(), nib.Nifti1Image, obj.affine, and nib.save

import nibabel
data_path = 'path/to/your/data.nii'
img = nibabel.load(data_path)
Image Image

pcasl avatar Aug 12 '25 01:08 pcasl

I would ask the pylance developers how to reproduce this more directly. vscode is too complex to be a useful reproduction.

effigies avatar Aug 12 '25 01:08 effigies

I think adding an __all__ to __init__.py would resolve this warning (by explicitly making the intended symbols public), but I don't know if there's a design reason to not set __all__ here?

shnizzedy avatar Sep 24 '25 15:09 shnizzedy

The design reason is that nibabel is old and comes from the consenting adults phase of Python, and people will depend on any unintentional behavior of import nibabel as nib, such as things that are not explicitly imported still being in the namespace. Does __all__ preserve that? I've never tried to retrofit it into an old codebase that people are depending on to not change.

effigies avatar Sep 24 '25 15:09 effigies

Does all preserve that?

If __all__ literally includes all the public symbols in __init__.py, the resulting namespace from import nibabel as nib should be the same.

And I should clarify the __all__ should fix the warnings for nib.load and nib.save but not for the methods and attributes ― those would need typing stubs that specify all the attributes that could arise from things like dynamic type detection, which could be quite a lift.

shnizzedy avatar Sep 24 '25 16:09 shnizzedy

Given that this is just a way to satisfy a type checker, I would suggest that a PR add an __init__.pyi. That should make pylance happy without any risk of failing to export something (the namespace is pretty large). If it's in a stub, it may not need __all__ to be added, so it might be a pretty easy copy-paste job.

For improving the type hints on nibabel objects: https://github.com/nipy/nibabel/discussions/1220

effigies avatar Sep 24 '25 16:09 effigies

I think an __init__.pyi is exactly the way to go (and an __all__ in the *.pyi should satisfy the linter). I'll put in a PR shortly.

shnizzedy avatar Sep 24 '25 16:09 shnizzedy