Skip to content

Commit

Permalink
Validate the dataclass is frozen when allow_unfrozen=True (Fix #406)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 572257669
  • Loading branch information
Conchylicultor authored and The etils Authors committed Oct 10, 2023
1 parent ea1f487 commit 05b1953
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Changelog follow https://keepachangelog.com/ format.

## [Unreleased]

* `edc`:
* Changed: Raise an error if `allow_unfrozen=` is used on a non-frozen
dataclass

## [1.5.1] - 2023-10-10

* `epath`:
* Fix `glob` issue when used with ffspec.

## [1.5.0] - 2023-09-19

* `ecolab`:
Expand Down Expand Up @@ -314,7 +323,8 @@ Changelog follow https://keepachangelog.com/ format.

* Automated github release

[Unreleased]: https://github.com/google/etils/compare/v1.5.0...HEAD
[Unreleased]: https://github.com/google/etils/compare/v1.5.1...HEAD
[1.5.0]: https://github.com/google/etils/compare/v1.5.0...v1.5.1
[1.5.0]: https://github.com/google/etils/compare/v1.4.1...v1.5.0
[1.4.1]: https://github.com/google/etils/compare/v1.4.0...v1.4.1
[1.4.0]: https://github.com/google/etils/compare/v1.3.0...v1.4.0
Expand Down
2 changes: 1 addition & 1 deletion etils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# A new PyPI release will be pushed everytime `__version__` is increased
# When changing this, also update the CHANGELOG.md
__version__ = '1.5.0'
__version__ = '1.5.1'

# Do NOT add anything to this file. This is left empty on purpose.
# Users should import subprojects directly.
5 changes: 5 additions & 0 deletions etils/edc/frozen_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

def add_unfrozen(cls: _Cls) -> _Cls:
"""Add the `frozen`, `unfrozen` methods."""
if not cls.__dataclass_params__.frozen:
raise ValueError(
'allow_unfrozen require the dataclass to be defined with'
f' `frozen=True`. For {cls}'
)
cls_frozen = getattr(cls, 'frozen', None)
cls_unfrozen = getattr(cls, 'has_unfrozen', None)

Expand Down
12 changes: 11 additions & 1 deletion etils/epath/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ def listdir(self, path: PathLike) -> list[str]:
return [os.path.basename(p) for p in paths if not p.endswith('~')]

def glob(self, path: PathLike) -> list[str]:
return self.fs(path).glob(path)
protocol = _get_protocol(path)
return [protocol + p for p in self.fs(path).glob(path)]

def makedirs(self, path: PathLike, *, exist_ok: bool = False) -> None:
return self.fs(path).makedirs(path, exist_ok=exist_ok)
Expand Down Expand Up @@ -457,6 +458,15 @@ def stat(self, path: PathLike) -> stat_utils.StatResult:
)


def _get_protocol(path: PathLike) -> str:
"""Extract the protocol."""
path = os.fspath(path)
if '://' in path:
return os.fspath(path).split('://', 1)[0] + '://'
else:
return ''


tf_backend = _TfBackend()
os_backend = _OsPathBackend()
fsspec_backend = _FileSystemSpecBackend()
11 changes: 11 additions & 0 deletions etils/epath/backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,17 @@ def _make_default_path(tmp_path: pathlib.Path):
(tmp_path / name).write_text('abc')


def test_get_protocol():
# pylint: disable=g-explicit-bool-comparison
assert epath.backend._get_protocol(epath.Path('aas/bbb/ccc')) == ''
assert epath.backend._get_protocol(epath.Path('/aas/bbb/ccc')) == ''
assert epath.backend._get_protocol(epath.Path('gs://aas/bbb/')) == 'gs://'
assert epath.backend._get_protocol('gcs://aas/bbb/ccc') == 'gcs://'
assert epath.backend._get_protocol('s3://aas/bbb/ccc') == 's3://'
assert epath.backend._get_protocol('/aas/bbb/ccc') == ''
# pylint: enable=g-explicit-bool-comparison


# Utils to test rename, replace, copy


Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ epath = [
]
epath-gcs = [
"gcsfs",
"etils[epath]",
]
epath-s3 = [
"s3fs",
"etils[epath]",
]
epy = [
"typing_extensions",
Expand Down

0 comments on commit 05b1953

Please sign in to comment.