Skip to content

Commit 3b1a5b8

Browse files
committed
Fixes #25, #26, #29
1 parent dbc57f7 commit 3b1a5b8

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

upath/core.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
from abc import ABCMeta
12
import os
23
import pathlib
34
import urllib
45

56
from fsspec.registry import known_implementations, registry
7+
from fsspec.utils import stringify_path
68

79
from upath.registry import _registry
810

911

10-
class UPath(pathlib.Path):
12+
class UPathMeta(ABCMeta):
13+
def __instancecheck__(cls, instance):
14+
return isinstance(instance, pathlib.Path)
15+
16+
def __subclasscheck__(cls, subclass):
17+
return issubclass(subclass, pathlib.Path)
18+
19+
20+
class UPath(pathlib.Path, metaclass=UPathMeta):
1121
def __new__(cls, *args, **kwargs):
12-
if cls is UPath:
22+
if issubclass(cls, UPath):
1323
args_list = list(args)
1424
url = args_list.pop(0)
25+
url = stringify_path(url)
1526
parsed_url = urllib.parse.urlparse(url)
1627
for key in ["scheme", "netloc"]:
1728
val = kwargs.get(key)
@@ -38,4 +49,6 @@ def __new__(cls, *args, **kwargs):
3849
args = tuple(args_list)
3950
self = cls._from_parts_init(args, init=False)
4051
self._init(*args, **kwargs)
52+
else:
53+
self = super().__new__(*args, **kwargs)
4154
return self

upath/tests/conftest.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ def clear_registry():
4848

4949
@pytest.fixture()
5050
def tempdir(clear_registry):
51-
tempdir = tempfile.TemporaryDirectory()
52-
tempdir = tempdir.name
53-
return tempdir
51+
with tempfile.TemporaryDirectory() as tempdir:
52+
yield tempdir
5453

5554

5655
@pytest.fixture()
5756
def local_testdir(tempdir, clear_registry):
5857
tmp = Path(tempdir)
59-
tmp.mkdir()
58+
tmp.mkdir(exist_ok=True)
6059
folder1 = tmp.joinpath("folder1")
6160
folder1.mkdir()
6261
folder1_files = ["file1.txt", "file2.txt"]

upath/tests/test_core.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,34 @@ def test_multiple_backend_paths(local_testdir, s3, hdfs):
5252
path = f"hdfs:{local_testdir}"
5353
UPath(path, host=host, user=user, port=port)
5454
assert s3_path.joinpath("text1.txt")._url.scheme == "s3"
55+
56+
57+
def test_constructor_accept_path(local_testdir):
58+
path = UPath(pathlib.Path(local_testdir))
59+
assert str(path) == local_testdir
60+
61+
62+
def test_constructor_accept_upath(local_testdir):
63+
path = UPath(UPath(local_testdir))
64+
assert str(path) == local_testdir
65+
66+
67+
def test_subclass(local_testdir):
68+
class MyPath(UPath):
69+
pass
70+
71+
path = MyPath(local_testdir)
72+
assert str(path) == local_testdir
73+
assert issubclass(MyPath, UPath)
74+
assert isinstance(path, pathlib.Path)
75+
76+
77+
def test_instance_check(local_testdir):
78+
path = UPath(local_testdir)
79+
assert isinstance(path, UPath)
80+
81+
82+
def test_new_method(local_testdir):
83+
path = UPath.__new__(pathlib.Path, local_testdir)
84+
assert str(path) == local_testdir
85+
assert isinstance(path, pathlib.Path)

0 commit comments

Comments
 (0)