From 4f1c9b61b65d9117e9e6b93601471234c7d12857 Mon Sep 17 00:00:00 2001 From: BENR0 Date: Thu, 9 Nov 2023 18:59:37 +0100 Subject: [PATCH] Fix can_be_local for pathlib.Path (#1419) --- fsspec/tests/test_utils.py | 21 +++++++++++++++++++++ fsspec/utils.py | 1 + 2 files changed, 22 insertions(+) diff --git a/fsspec/tests/test_utils.py b/fsspec/tests/test_utils.py index b5732e9f1..c7bad93bc 100644 --- a/fsspec/tests/test_utils.py +++ b/fsspec/tests/test_utils.py @@ -1,5 +1,6 @@ import io import sys +from pathlib import Path from unittest.mock import Mock import pytest @@ -8,6 +9,7 @@ from fsspec.utils import ( can_be_local, common_prefix, + get_protocol, infer_storage_options, merge_offset_ranges, mirror_from, @@ -333,6 +335,24 @@ def test_log(): assert logger.level == logging.DEBUG +@pytest.mark.parametrize( + "par", + [ + ("afile", "file"), + ("file://afile", "file"), + ("noproto://afile", "noproto"), + ("noproto::stuff", "noproto"), + ("simplecache::stuff", "simplecache"), + ("simplecache://stuff", "simplecache"), + ("s3://afile", "s3"), + (Path("afile"), "file"), + ], +) +def test_get_protocol(par): + url, outcome = par + assert get_protocol(url) == outcome + + @pytest.mark.parametrize( "par", [ @@ -342,6 +362,7 @@ def test_log(): ("noproto::stuff", False), ("simplecache::stuff", True), ("simplecache://stuff", True), + (Path("afile"), True), ], ) def test_can_local(par): diff --git a/fsspec/utils.py b/fsspec/utils.py index 34f1ad821..543bd89d7 100644 --- a/fsspec/utils.py +++ b/fsspec/utils.py @@ -436,6 +436,7 @@ def isfilelike(f: Any) -> TypeGuard[IO[bytes]]: def get_protocol(url: str) -> str: + url = stringify_path(url) parts = re.split(r"(\:\:|\://)", url, 1) if len(parts) > 1: return parts[0]