Skip to content

Commit

Permalink
Add constructor to ReadOnlyPath('xyz')
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 362304623
  • Loading branch information
Conchylicultor authored and copybara-github committed Mar 11, 2021
1 parent 6239509 commit bbce98c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tensorflow_datasets/core/utils/generic_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from tensorflow_datasets.core.utils import generic_path
from tensorflow_datasets.core.utils import gpath
from tensorflow_datasets.core.utils import type_utils


def test_windows_encoding():
Expand Down Expand Up @@ -59,3 +60,14 @@ class MyPath(gpath.PosixGPath):
my_path = generic_path.as_path('my_path://abc')
assert isinstance(my_path, MyPath)
assert generic_path.as_path(my_path) is my_path


def test_as_path_new():

p0 = type_utils.ReadWritePath('some/path/to/xyz')
p1 = type_utils.ReadOnlyPath('some/path/to/xyz')
p2 = generic_path.as_path('some/path/to/xyz')
assert p0 == p1
assert p0 == p2
assert type(p0) is type(p1)
assert type(p0) is type(p2)
7 changes: 7 additions & 0 deletions tensorflow_datasets/core/utils/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class ReadOnlyPath(PurePath, Protocol):
documentation.
"""

def __new__(cls: Type[T], *args: PathLike) -> T:
if cls in (ReadOnlyPath, ReadWritePath):
from tensorflow_datasets.core.utils import generic_path # pytype: disable=import-error # pylint: disable=g-import-not-at-top
return generic_path.as_path(*args)
else:
return super().__new__(cls, *args)

@abc.abstractmethod
def exists(self) -> bool:
"""Returns True if self exists."""
Expand Down

0 comments on commit bbce98c

Please sign in to comment.