Skip to content
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

Type checking issues #1252

Closed
blakedewey opened this issue Sep 7, 2023 · 9 comments
Closed

Type checking issues #1252

blakedewey opened this issue Sep 7, 2023 · 9 comments

Comments

@blakedewey
Copy link
Contributor

Hi Nibabel community,

Great job on getting some type checking into this package! I wanted to point out two scenarios that I use frequently that have run me into type checking issues based on 5.1.0.

  1. nb.load shows a return type of FileBasedImage. This is all well and good, but unfortunately FileBasedImage does not define get_fdata(). A common code example that I use (and see around) is:
obj = nb.load(filepath)
data = obj.get_fdata()
  1. nb.Nifti1Image doesn't want to take None for the affine. I previously saw this code used frequently:
nb.Nifti1Image(data_array, None, header)

Just bringing these to your attention. I'm also flexible to change up my code, if better patterns are recommended.

@blakedewey
Copy link
Contributor Author

Just saw this comment #1109 (comment). I think this change to SpatialImage would fix my first issue.

@effigies
Copy link
Member

effigies commented Sep 7, 2023

Have you seen #1220 (comment)?

@blakedewey
Copy link
Contributor Author

Ah. I had not. Good resource. I was wondering if that would have to be the case. It honestly is smarter code anyways, even if it is more verbose.

@effigies
Copy link
Member

effigies commented Sep 8, 2023

  1. nb.Nifti1Image doesn't want to take None for the affine. I previously saw this code used frequently:
nb.Nifti1Image(data_array, None, header)

I'm a bit surprised by this. We haven't typed the __init__ method at this point, so I would expect it to be Any. What type checker are you using, and what error do you see?

@blakedewey
Copy link
Contributor Author

This is in my IDE. I use PyCharm from JetBrains. The error I get is Expected type 'ndarray', got 'None' instead. I'm assuming it's doing some kind of inspection of how it's assigned somewhere in lieu of a known type.

@effigies
Copy link
Member

effigies commented Sep 8, 2023

Ah, I bet it's picking up on the SpatialImage.__init__:

def __init__(
self,
dataobj: ArrayLike,
affine: np.ndarray,
header: FileBasedHeader | ty.Mapping | None = None,
extra: ty.Mapping | None = None,
file_map: FileMap | None = None,
):

affine should be np.ndarray | None.

@blakedewey
Copy link
Contributor Author

Is that as easy of a PR as I think? I can submit fix, if so.

@effigies
Copy link
Member

effigies commented Sep 8, 2023

I think it should be, yes. Please go ahead.

@blakedewey
Copy link
Contributor Author

#1253 is open. Closing this issue as both concerns have been addressed.

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

No branches or pull requests

2 participants