|
60 | 60 |
|
61 | 61 | def _get_filepath_or_buffer(
|
62 | 62 | path: typing.Union[str, pathlib.Path]
|
63 |
| -) -> typing.Tuple[typing.Any, bool]: # (pandas._typing.FileOrBuffer, bool) |
| 63 | +) -> typing.Tuple[ |
| 64 | + typing.Any, bool # pandas._typing.FileOrBuffer, bool) |
| 65 | +]: # pragma: no cover |
| 66 | + # can't check coverage due to pandas version branching. |
| 67 | + # pipeline tests against multiple pandas versions. |
| 68 | + if not hasattr(pandas.io.common, "get_filepath_or_buffer"): |
| 69 | + # pandas.io.common.get_filepath_or_buffer was made private in v1.2.0: |
| 70 | + # https://github.com/pandas-dev/pandas/commit/6d1541e1782a7b94797d5432922e64a97934cfa4#diff-934d8564d648e7521db673c6399dcac98e45adfd5230ba47d3aabfcc21979febL247 |
| 71 | + # semver?!? breaking change not even mentioned in changelog: |
| 72 | + # https://pandas.pydata.org/pandas-docs/stable/whatsnew/v1.2.0.html |
| 73 | + # new wrapper: get_handle |
| 74 | + # https://github.com/pandas-dev/pandas/blob/v1.2.0/pandas/io/common.py#L490 |
| 75 | + # pandas v1.1's get_handle does not yet support urls |
| 76 | + # pylint: disable=no-member; for python<v1.2.0 |
| 77 | + io_handle = pandas.io.common.get_handle(path, "r") |
| 78 | + return io_handle.handle, True |
64 | 79 | # path_or_buffer: typing.Union[str, pathlib.Path, typing.IO[typing.AnyStr],
|
65 | 80 | # s3fs.S3File, gcsfs.GCSFile]
|
66 | 81 | # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/parsers.py#L436
|
67 | 82 | # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/_typing.py#L30
|
| 83 | + # pylint: disable=no-member; for python>=v1.2.0 |
68 | 84 | (path_or_buffer, _, _, *instructions) = pandas.io.common.get_filepath_or_buffer(
|
69 | 85 | path
|
70 | 86 | )
|
71 |
| - if instructions: # pragma: no cover |
| 87 | + if instructions: |
72 | 88 | # https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/common.py#L171
|
73 | 89 | assert len(instructions) == 1, instructions
|
74 | 90 | should_close = instructions[0]
|
75 |
| - else: # pragma: no cover |
| 91 | + else: |
76 | 92 | # https://github.com/pandas-dev/pandas/blob/v0.21.0/pandas/io/common.py#L171
|
77 | 93 | should_close = hasattr(path_or_buffer, "close")
|
78 | 94 | return path_or_buffer, should_close
|
@@ -220,13 +236,15 @@ def _read(self, stream: typing.TextIO) -> None:
|
220 | 236 | def read(cls, path: typing.Union[str, pathlib.Path]) -> "CorticalParcellationStats":
|
221 | 237 | path_or_buffer, should_close = _get_filepath_or_buffer(path)
|
222 | 238 | stats = cls()
|
223 |
| - try: |
224 |
| - if hasattr(path_or_buffer, "readline"): |
225 |
| - # pylint: disable=protected-access |
| 239 | + try: # pragma: no cover |
| 240 | + # can't check coverage due to pandas version branching. |
| 241 | + # pylint: disable=protected-access; false-positive for ._read |
| 242 | + if isinstance(path_or_buffer, io.TextIOWrapper): # pandas>=v1.2.0 |
| 243 | + stats._read(path_or_buffer) |
| 244 | + elif hasattr(path_or_buffer, "readline"): |
226 | 245 | stats._read(io.TextIOWrapper(path_or_buffer))
|
227 | 246 | else:
|
228 | 247 | with open(path_or_buffer, "r") as stream:
|
229 |
| - # pylint: disable=protected-access |
230 | 248 | stats._read(stream)
|
231 | 249 | finally:
|
232 | 250 | if should_close:
|
|
0 commit comments