Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Commit 88c9a71

Browse files
committed
adapt to breaking change in pandas v1.2.0 making get_filepath_or_buffer private without major release (semver!?)
pandas-dev/pandas#37639 pandas-dev/pandas@6d1541e#diff-934d8564d648e7521db673c6399dcac98e45adfd5230ba47d3aabfcc21979febL247
1 parent 42c468e commit 88c9a71

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

.github/workflows/python.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ jobs:
4646
- 0.23.*
4747
- 0.24.*
4848
- 0.25.*
49-
- 1.1.*
49+
- 1.1.* # pandas.io.common.get_handle does not yet support urls
50+
- 1.*
5051
exclude:
5152
# https://travis-ci.org/github/fphammerle/freesurfer-stats/jobs/683777317#L208
5253
# https://github.com/pandas-dev/pandas/commit/18efcb27361478daa3118079ecb166c733691ecb#diff-2eeaed663bd0d25b7e608891384b7298R814
5354
- python-version: 3.5
5455
pandas-version: 1.1.*
56+
- python-version: 3.5
57+
pandas-version: 1.*
5558
- python-version: 3.7
5659
pandas-version: 0.21.*
5760
- python-version: 3.7

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- `AttributeError` when using pandas v1.2.0
810

911
## [1.2.0] - 2020-05-30
1012
### Added

freesurfer_stats/__init__.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,35 @@
6060

6161
def _get_filepath_or_buffer(
6262
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
6479
# path_or_buffer: typing.Union[str, pathlib.Path, typing.IO[typing.AnyStr],
6580
# s3fs.S3File, gcsfs.GCSFile]
6681
# https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/parsers.py#L436
6782
# https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/_typing.py#L30
83+
# pylint: disable=no-member; for python>=v1.2.0
6884
(path_or_buffer, _, _, *instructions) = pandas.io.common.get_filepath_or_buffer(
6985
path
7086
)
71-
if instructions: # pragma: no cover
87+
if instructions:
7288
# https://github.com/pandas-dev/pandas/blob/v0.25.3/pandas/io/common.py#L171
7389
assert len(instructions) == 1, instructions
7490
should_close = instructions[0]
75-
else: # pragma: no cover
91+
else:
7692
# https://github.com/pandas-dev/pandas/blob/v0.21.0/pandas/io/common.py#L171
7793
should_close = hasattr(path_or_buffer, "close")
7894
return path_or_buffer, should_close
@@ -220,13 +236,15 @@ def _read(self, stream: typing.TextIO) -> None:
220236
def read(cls, path: typing.Union[str, pathlib.Path]) -> "CorticalParcellationStats":
221237
path_or_buffer, should_close = _get_filepath_or_buffer(path)
222238
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"):
226245
stats._read(io.TextIOWrapper(path_or_buffer))
227246
else:
228247
with open(path_or_buffer, "r") as stream:
229-
# pylint: disable=protected-access
230248
stats._read(stream)
231249
finally:
232250
if should_close:

setup.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@
6868
packages=setuptools.find_packages(),
6969
install_requires=[
7070
"numpy<2",
71-
# pandas v1.2.0 made `get_filepath_or_buffer` private without releasing major version.
72-
# semver?!? not even mentioned in changelog
73-
# https://pandas.pydata.org/pandas-docs/stable/whatsnew/v1.2.0.html
74-
# https://github.com/pandas-dev/pandas/commit/6d1541e1782a7b94797d5432922e64a97934cfa4#diff-934d8564d648e7521db673c6399dcac98e45adfd5230ba47d3aabfcc21979febL247
75-
# TODO verify lower version constraint
76-
"pandas>=0.21,<1.2",
71+
# still hoping that pandas will stick to semantic versioning in the future
72+
"pandas>=0.21,<2",
7773
],
7874
setup_requires=["setuptools_scm"],
7975
tests_require=["pytest<5"],

0 commit comments

Comments
 (0)