Browse Source

CorticalParcellationStats.read: adapt to get_filepath_or_buffer not returning should_close in pandas v0.21.0 & v0.22.0

Fabian Peter Hammerle 3 years ago
parent
commit
f81abee7ff
1 changed files with 16 additions and 4 deletions
  1. 16 4
      freesurfer_stats/__init__.py

+ 16 - 4
freesurfer_stats/__init__.py

@@ -183,11 +183,23 @@ class CorticalParcellationStats:
 
     @classmethod
     def read(cls, path: typing.Union[str, pathlib.Path]) -> "CorticalParcellationStats":
-        # support http, s3 & gcs
+        # path_or_buffer: typing.Union[str, pathlib.Path, typing.IO[typing.AnyStr],
+        #                              s3fs.S3File, gcsfs.GCSFile]
+        # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/parsers.py#L436
+        # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/_typing.py#L30
+        (
+            path_or_buffer,
+            _,
+            _,
+            *instructions,
+        ) = pandas.io.common.get_filepath_or_buffer(path)
         # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/common.py#L171
-        path_or_buffer, _, _, should_close = pandas.io.common.get_filepath_or_buffer(
-            path
-        )
+        # https://github.com/pandas-dev/pandas/blob/v0.21.0/pandas/io/common.py#L171
+        if instructions:
+            assert len(instructions) == 1, instructions
+            should_close = instructions[0]
+        else:
+            should_close = hasattr(path_or_buffer, "close")
         stats = cls()
         if hasattr(path_or_buffer, "readline"):
             # pylint: disable=protected-access