-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1384 from RandallPittmanOrSt/disable_dataset_iter…
…ation Disable Dataset iteration and membership operations (raise error on __iter__ and __contains__)
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import netCDF4 | ||
|
||
FILE_NAME = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name | ||
|
||
|
||
class TestNoIterNoContains(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.file = FILE_NAME | ||
with netCDF4.Dataset(self.file, "w") as dataset: | ||
# just create a simple variable | ||
dataset.createVariable("var1", int) | ||
|
||
def tearDown(self) -> None: | ||
os.remove(self.file) | ||
|
||
def test_no_iter(self) -> None: | ||
"""Verify that iteration is explicitly not supported""" | ||
with netCDF4.Dataset(self.file, "r") as dataset: | ||
with self.assertRaises(TypeError): | ||
for _ in dataset: # type: ignore # type checker catches that this doesn't work | ||
pass | ||
|
||
def test_no_contains(self) -> None: | ||
"""Verify the membership operations are explicity not supported""" | ||
with netCDF4.Dataset(self.file, "r") as dataset: | ||
with self.assertRaises(TypeError): | ||
_ = "var1" in dataset | ||
|
||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |