Skip to content

Commit

Permalink
Merge pull request #183 from otetard/add-python-3.13-support
Browse files Browse the repository at this point in the history
Add Python 3.13 compatibility
  • Loading branch information
liormizr authored Nov 9, 2024
2 parents 568b0ce + 877cc4f commit a13888b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, "3.10", 3.11, 3.12]
python-version: [3.8, 3.9, "3.10", 3.11, 3.12, 3.13]
steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
Expand Down
44 changes: 40 additions & 4 deletions s3path/current_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def register_configuration_parameter(
raise TypeError(f'parameters argument have to be a dict type. got {type(path)}')
if parameters is None and resource is None and glob_new_algorithm is None:
raise ValueError('user have to specify parameters or resource arguments')
if glob_new_algorithm is False and sys.version_info >= (3, 13):
raise ValueError('old glob algorithm can only be used by python versions below 3.13')
accessor.configuration_map.set_configuration(
path,
resource=resource,
Expand All @@ -59,6 +61,9 @@ class PureS3Path(PurePath):
_flavour = flavour
__slots__ = ()

if sys.version_info >= (3, 13):
parser = _flavour

def __init__(self, *args):
super().__init__(*args)

Expand All @@ -70,7 +75,10 @@ def __init__(self, *args):
new_parts.remove(part)

self._raw_paths = new_parts
self._load_parts()
if sys.version_info >= (3, 13):
self._drv, self._root, self._tail_cached = self._parse_path(self._raw_path)
else:
self._load_parts()

@classmethod
def from_uri(cls, uri: str):
Expand Down Expand Up @@ -276,7 +284,7 @@ def is_mount(self) -> Literal[False]:
return False


class S3Path(_PathNotSupportedMixin, Path, PureS3Path):
class S3Path(_PathNotSupportedMixin, PureS3Path, Path):
def stat(self, *, follow_symlinks: bool = True) -> accessor.StatResult:
"""
Returns information about this path (similarly to boto3's ObjectSummary).
Expand Down Expand Up @@ -433,6 +441,27 @@ def iterdir(self): # todo: -> Generator[S3Path, None, None]:
for name in accessor.listdir(self):
yield self._make_child_relpath(name)

def _make_child_relpath(self, name):
# _make_child_relpath was removed from Python 3.13 in
# 30f0643e36d2c9a5849c76ca0b27b748448d0567
if sys.version_info < (3, 13):
return super()._make_child_relpath(name)

path_str = str(self)
tail = self._tail
if tail:
path_str = f'{path_str}{self._flavour.sep}{name}'
elif path_str != '.':
path_str = f'{path_str}{name}'
else:
path_str = name
path = self.with_segments(path_str)
path._str = path_str
path._drv = self.drive
path._root = self.root
path._tail_cached = tail + [name]
return path

def open(
self,
mode: Literal['r', 'w', 'rb', 'wb'] = 'r',
Expand All @@ -452,26 +481,30 @@ def open(
errors=errors,
newline=newline)

def glob(self, pattern: str): # todo: -> Generator[S3Path, None, None]:
def glob(self, pattern: str, *, case_sensitive=None, recurse_symlinks=False): # todo: -> Generator[S3Path, None, None]:
"""
Glob the given relative pattern in the Bucket / key prefix represented by this path,
yielding all matching files (of any kind)
"""
self._absolute_path_validation()
general_options = accessor.configuration_map.get_general_options(self)
glob_new_algorithm = general_options['glob_new_algorithm']
if sys.version_info >= (3, 13):
glob_new_algorithm = True
if not glob_new_algorithm:
yield from super().glob(pattern)
return
yield from self._glob(pattern)

def rglob(self, pattern: str): # todo: -> Generator[S3Path, None, None]:
def rglob(self, pattern: str, *, case_sensitive=None, recurse_symlinks=False): # todo: -> Generator[S3Path, None, None]:
"""
This is like calling S3Path.glob with "**/" added in front of the given relative pattern
"""
self._absolute_path_validation()
general_options = accessor.configuration_map.get_general_options(self)
glob_new_algorithm = general_options['glob_new_algorithm']
if sys.version_info >= (3, 13):
glob_new_algorithm = True
if not glob_new_algorithm:
yield from super().rglob(pattern)
return
Expand Down Expand Up @@ -670,6 +703,9 @@ class VersionedS3Path(PureVersionedS3Path, S3Path):
<< VersionedS3Path('/<bucket>/<key>', version_id='<version_id>')
"""

def __init__(self, *args, version_id):
super().__init__(*args)


def _is_wildcard_pattern(pat):
# Whether this pattern needs actual matching using fnmatch, or can
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
],
)
17 changes: 12 additions & 5 deletions tests/test_path_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ def test_glob_nested_folders_issue_no_120(s3_mock):
assert list(path.glob("further/*")) == [S3Path('/my-bucket/s3path-test/nested/further/test.txt')]


@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_glob_old_algo(s3_mock, enable_old_glob):
test_glob(s3_mock)


@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_glob_nested_folders_issue_no_115_old_algo(s3_mock, enable_old_glob):
test_glob_nested_folders_issue_no_115(s3_mock)

Expand Down Expand Up @@ -245,14 +247,17 @@ def test_glob_nested_folders_issue_no_179(s3_mock):
S3Path('/my-bucket/s3path/nested/further/andfurther')]


@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_glob_issue_160_old_algo(s3_mock, enable_old_glob):
test_glob_issue_160(s3_mock)


@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_glob_issue_160_weird_behavior_old_algo(s3_mock, enable_old_glob):
test_glob_issue_160_weird_behavior(s3_mock)


@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_glob_nested_folders_issue_no_179_old_algo(s3_mock, enable_old_glob):
test_glob_nested_folders_issue_no_179(s3_mock)

Expand Down Expand Up @@ -285,7 +290,8 @@ def test_rglob(s3_mock):
S3Path('/test-bucket/test_pathlib.py')]


def test_rglob_new_algo(s3_mock, enable_old_glob):
@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_rglob_old_algo(s3_mock, enable_old_glob):
test_rglob(s3_mock)


Expand Down Expand Up @@ -313,7 +319,8 @@ def test_accessor_scandir(s3_mock):
S3Path('/test-bucket/test_pathlib.py')]


def test_accessor_scandir_new_algo(s3_mock, enable_old_glob):
@pytest.mark.skipif(sys.version_info >= (3, 13), reason="requires python3.12 or lower")
def test_accessor_scandir_old_algo(s3_mock, enable_old_glob):
test_accessor_scandir(s3_mock)


Expand Down Expand Up @@ -470,15 +477,15 @@ def test_iterdir(s3_mock):
object_summary.put(Body=b'test data')

s3_path = S3Path('/test-bucket/docs')
assert sorted(s3_path.iterdir()) == [
S3Path('/test-bucket/docs/Makefile'),
assert sorted(s3_path.iterdir()) == sorted([
S3Path('/test-bucket/docs/_build'),
S3Path('/test-bucket/docs/_static'),
S3Path('/test-bucket/docs/_templates'),
S3Path('/test-bucket/docs/conf.py'),
S3Path('/test-bucket/docs/index.rst'),
S3Path('/test-bucket/docs/make.bat'),
]
S3Path('/test-bucket/docs/Makefile'),
])


def test_iterdir_on_buckets(s3_mock):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_s3path_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ def test_issue_123():
new_resource, _ = accessor.configuration_map.get_configuration(path)
assert new_resource is s3
assert new_resource is not old_resource


@pytest.mark.skipif(sys.version_info < (3, 13), reason="requires python3.13 or higher")
def test_register_configuration_parameter_old_algo():
with pytest.raises(ValueError):
register_configuration_parameter(PureS3Path('/'), glob_new_algorithm=False)

0 comments on commit a13888b

Please sign in to comment.