Pylance thinks that an index into an H5 file does not create a group

47 Views Asked by At

The following minimal example works fine:

import h5py

f = h5py.File("mytestfile.hdf5", "w")
f.create_group("hello") # Pylance error here
f["hello"].create_dataset("hello", data=1)

g = f.create_group("bye")
g.create_dataset("bye", data=2)

f.close()

However, on the indicated, line, Pylance throws an error, saying Cannot access member "create_dataset" for type "Dataset" Member "create_dataset" is unknown (And the same error again on the same line for "Datatype" instead of "Dataset".) Note that this error does not occur on the later line that runs the same create_dataset function.

Even adding an assert isinstance(f["hello"], h5py.Group) before the erroring line does not solve the issue.

Why does Pylance think that f["hello"] is a Datatype or Dataset and not a Group?

I've also looked at the codebase to see if I can find where the indexing magic method happens and can't easily find it to see if it's typed correctly and/or fix it.

How can I get Pylance to understand that f["hello"] is a group that can create a dataset?

1

There are 1 best solutions below

0
Pro Q On

After seeing that Pylance thinks that k in k = f["hello"] is of type Group | Dataset | Datatype, I realized what is going on.

First of all, clearly Pylance does think that f["hello"] can be a Group, it just also thinks that it could be a Dataset or Datatype and the create_dataset method doesn't exist for those types, so it rightly throws an error.

The assertion method of fixing it that I tried really should work (I'd appreciate a comment explaining why this is hard to do / isn't the default behavior in static type checking), but the static type checking is not that advanced yet.

Rather, if you do k = f["hello"], and then follow that up with the assertion assert isinstance(k, h5py.Group), and then do k.create_dataset("hello", data=1), no error will by thrown by Pylance.

You can also solve it with a cast, like k = cast(h5py.Group, f["hello"]), and then, again, no error is thrown by the create_dataset.