Skip to content

Commit

Permalink
fix issues accross different python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Jul 30, 2023
1 parent 98d8c93 commit 0487565
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
12 changes: 8 additions & 4 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,22 @@ def __new__(cls: type[PT], *args: str | PathLike, **kwargs: Any) -> PT:
else:
protocol = kwargs.get("scheme", protocol) or ""

upath_cls = get_upath_class(protocol=protocol)
if upath_cls is None:
raise ValueError(f"Unsupported filesystem: {parsed_url.scheme!r}")

for key in ["scheme", "netloc"]:
val = kwargs.get(key)
if val:
parsed_url = parsed_url._replace(**{key: val})

if not parsed_url.path:
parsed_url = parsed_url._replace(path="/") # ensure path has root
args_list.insert(0, parsed_url.path)

upath_cls = get_upath_class(protocol=protocol)
if upath_cls is None:
raise ValueError(f"Unsupported filesystem: {parsed_url.scheme!r}")
if not protocol:
args_list.insert(0, url)
else:
args_list.insert(0, parsed_url.path)

return upath_cls._from_parts( # type: ignore
args_list, url=parsed_url, **kwargs
Expand Down
7 changes: 0 additions & 7 deletions upath/implementations/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import WindowsPath
from typing import Any
from typing import Iterable
from urllib.parse import urlunsplit

from fsspec.implementations.local import LocalFileSystem

Expand Down Expand Up @@ -60,9 +59,6 @@ def path(self) -> str:

@classmethod
def _from_parts(cls, args, *, url=None, **kw):
if url:
args = list(args)
args[0] = urlunsplit(url._replace(scheme=url.scheme.upper()))
return super(UPath, cls)._from_parts(args)


Expand All @@ -89,7 +85,4 @@ def path(self) -> str:

@classmethod
def _from_parts(cls, args, *, url=None, **kw):
if url:
args = list(args)
args[0] = urlunsplit(url._replace(scheme=url.scheme.upper()))
return super(UPath, cls)._from_parts(args)
55 changes: 34 additions & 21 deletions upath/tests/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ def test_bytes(self):
P('a').relative_to(b'b')
with self.assertRaises(TypeError):
P('a').with_name(b'b')
with self.assertRaises(TypeError):
P('a').with_stem(b'b')
if sys.version_info >= (3, 9):
with self.assertRaises(TypeError):
P('a').with_stem(b'b')
with self.assertRaises(TypeError):
P('a').with_suffix(b'b')

Expand Down Expand Up @@ -494,15 +495,16 @@ def test_parents_common(self):
self.assertEqual(par[0], P('a/b'))
self.assertEqual(par[1], P('a'))
self.assertEqual(par[2], P('.'))
self.assertEqual(par[-1], P('.'))
self.assertEqual(par[-2], P('a'))
self.assertEqual(par[-3], P('a/b'))
self.assertEqual(par[0:1], (P('a/b'),))
self.assertEqual(par[:2], (P('a/b'), P('a')))
self.assertEqual(par[:-1], (P('a/b'), P('a')))
self.assertEqual(par[1:], (P('a'), P('.')))
self.assertEqual(par[::2], (P('a/b'), P('.')))
self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b')))
if sys.version_info >= (3, 10):
self.assertEqual(par[-1], P('.'))
self.assertEqual(par[-2], P('a'))
self.assertEqual(par[-3], P('a/b'))
self.assertEqual(par[0:1], (P('a/b'),))
self.assertEqual(par[:2], (P('a/b'), P('a')))
self.assertEqual(par[:-1], (P('a/b'), P('a')))
self.assertEqual(par[1:], (P('a'), P('.')))
self.assertEqual(par[::2], (P('a/b'), P('.')))
self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b')))
self.assertEqual(list(par), [P('a/b'), P('a'), P('.')])
with self.assertRaises(IndexError):
par[-4]
Expand All @@ -517,15 +519,16 @@ def test_parents_common(self):
self.assertEqual(par[0], P('/a/b'))
self.assertEqual(par[1], P('/a'))
self.assertEqual(par[2], P('/'))
self.assertEqual(par[-1], P('/'))
self.assertEqual(par[-2], P('/a'))
self.assertEqual(par[-3], P('/a/b'))
self.assertEqual(par[0:1], (P('/a/b'),))
self.assertEqual(par[:2], (P('/a/b'), P('/a')))
self.assertEqual(par[:-1], (P('/a/b'), P('/a')))
self.assertEqual(par[1:], (P('/a'), P('/')))
self.assertEqual(par[::2], (P('/a/b'), P('/')))
self.assertEqual(par[::-1], (P('/'), P('/a'), P('/a/b')))
if sys.version_info >= (3, 10):
self.assertEqual(par[-1], P('/'))
self.assertEqual(par[-2], P('/a'))
self.assertEqual(par[-3], P('/a/b'))
self.assertEqual(par[0:1], (P('/a/b'),))
self.assertEqual(par[:2], (P('/a/b'), P('/a')))
self.assertEqual(par[:-1], (P('/a/b'), P('/a')))
self.assertEqual(par[1:], (P('/a'), P('/')))
self.assertEqual(par[::2], (P('/a/b'), P('/')))
self.assertEqual(par[::-1], (P('/'), P('/a'), P('/a/b')))
self.assertEqual(list(par), [P('/a/b'), P('/a'), P('/')])
with self.assertRaises(IndexError):
par[-4]
Expand Down Expand Up @@ -634,6 +637,7 @@ def test_with_name_common(self):
self.assertRaises(ValueError, P('a/b').with_name, 'c/')
self.assertRaises(ValueError, P('a/b').with_name, 'c/d')

@pytest.mark.skipif(sys.version_info < (3, 9), reason="py39+")
def test_with_stem_common(self):
P = self.cls
self.assertEqual(P('a/b').with_stem('d'), P('a/d'))
Expand Down Expand Up @@ -747,6 +751,7 @@ def test_relative_to_common(self):
self.assertRaises(ValueError, p.relative_to, P(''), walk_up=True)
self.assertRaises(ValueError, p.relative_to, P('a'), walk_up=True)

@pytest.mark.skipif(sys.version_info < (3, 9), reason="py39+")
def test_is_relative_to_common(self):
P = self.cls
p = P('a/b')
Expand Down Expand Up @@ -798,7 +803,6 @@ def test_pickling_common(self):
class PurePosixPathTest(PurePathTest):
cls = pathlib.PurePosixPath

@pytest.mark.xfail(reason="requires __new__ refactor")
def test_drive_root_parts(self):
check = self._check_drive_root_parts
# Collapsing of excess leading slashes, except for the double-slash
Expand Down Expand Up @@ -1753,6 +1757,7 @@ def test_read_write_text(self):
self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')

@pytest.mark.skipif(sys.version_info < (3, 10), reason="py310+")
def test_write_text_with_newlines(self):
p = self.cls(BASE)
# Check that `\n` character change nothing
Expand Down Expand Up @@ -1901,6 +1906,7 @@ def _check(path, glob, expected):
_check(p, "dir*/*/../dirD/**/", ["dirC/dirD/../dirD"])
_check(p, "*/dirD/**/", ["dirC/dirD"])

@pytest.mark.skipif(sys.version_info < (3, 11), reason="py311+")
def test_rglob_common(self):
def _check(glob, expected):
self.assertEqual(sorted(glob), sorted(P(BASE, q) for q in expected))
Expand Down Expand Up @@ -2070,6 +2076,7 @@ def test_glob_permissions(self):
self.assertEqual(len(set(base.glob("*/fileC"))), 50)
self.assertEqual(len(set(base.glob("*/file*"))), 50)

@pytest.mark.skipif(sys.version_info < (3, 11), reason="py311+")
@os_helper.skip_unless_symlink
def test_glob_long_symlink(self):
# See gh-87695
Expand All @@ -2091,6 +2098,7 @@ def test_glob_above_recursion_limit(self):
with set_recursion_limit(recursion_limit):
list(base.glob('**'))

@pytest.mark.skipif(sys.version_info < (3, 9), reason="py39+")
@os_helper.skip_unless_symlink
def test_readlink(self):
P = self.cls(BASE)
Expand Down Expand Up @@ -2193,12 +2201,14 @@ def test_stat(self):
self.addCleanup(p.chmod, st.st_mode)
self.assertNotEqual(p.stat(), st)

@pytest.mark.skipif(sys.version_info < (3, 10), reason="py310+")
@os_helper.skip_unless_symlink
def test_stat_no_follow_symlinks(self):
p = self.cls(BASE) / 'linkA'
st = p.stat()
self.assertNotEqual(st, p.stat(follow_symlinks=False))

@pytest.mark.skipif(sys.version_info < (3, 10), reason="py310+")
def test_stat_no_follow_symlinks_nosymlink(self):
p = self.cls(BASE) / 'fileA'
st = p.stat()
Expand Down Expand Up @@ -2404,6 +2414,7 @@ def test_cwd(self):
p = self.cls.cwd()
self._test_cwd(p)

@pytest.mark.skipif(sys.version_info < (3, 11), reason="py311+")
def test_absolute_common(self):
P = self.cls

Expand Down Expand Up @@ -2515,6 +2526,7 @@ def test_chmod(self):
self.assertEqual(p.stat().st_mode, new_mode)

# On Windows, os.chmod does not follow symlinks (issue #15411)
@pytest.mark.skipif(sys.version_info < (3, 10), reason="py310+")
@only_posix
@os_helper.skip_unless_working_chmod
def test_chmod_follow_symlinks_true(self):
Expand Down Expand Up @@ -2573,6 +2585,7 @@ def test_rmdir(self):
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)

@pytest.mark.skipif(sys.version_info < (3, 10), reason="py310+")
@unittest.skipUnless(hasattr(os, "link"), "os.link() is not present")
def test_hardlink_to(self):
P = self.cls(BASE)
Expand Down

0 comments on commit 0487565

Please sign in to comment.