Skip to content

Commit 0c5466a

Browse files
authored
Upath add from_uri (#423)
* upath: add from_uri classmethod * upath.extensions: add from_uri * upath: apply workaround on local not on core * typesafety: check from_uri
1 parent 4ef02a3 commit 0c5466a

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

typesafety/test_upath_signatures.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,13 @@
897897
898898
reveal_type(UPath.home()) # N: Revealed type is "upath.core.UPath"
899899
900+
- case: upath_classmethod_from_uri
901+
disable_cache: false
902+
main: |
903+
from upath import UPath
904+
905+
reveal_type(UPath.from_uri("uri")) # N: Revealed type is "upath.core.UPath"
906+
900907
- case: upath_method_relative_to
901908
disable_cache: false
902909
main: |
@@ -1084,3 +1091,4 @@
10841091
# Class methods
10851092
reveal_type({{ cls }}.cwd()) # N: Revealed type is "{{ module }}.{{ cls }}"
10861093
reveal_type({{ cls }}.home()) # N: Revealed type is "{{ module }}.{{ cls }}"
1094+
reveal_type({{ cls }}.from_uri("uri")) # N: Revealed type is "{{ module }}.{{ cls }}"

upath/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,10 @@ def __reduce__(self):
11171117
kwargs["_relative_base"] = self._relative_base
11181118
return _make_instance, (type(self), args, kwargs)
11191119

1120+
@classmethod
1121+
def from_uri(cls, uri: str, **storage_options: Any) -> Self:
1122+
return cls(uri, **storage_options)
1123+
11201124
def as_uri(self) -> str:
11211125
if self._relative_base is not None:
11221126
raise ValueError(

upath/extensions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def root(self) -> str:
344344
def __reduce__(self):
345345
return type(self)._from_upath, (self.__wrapped__,)
346346

347+
@classmethod
348+
def from_uri(cls, uri: str, **storage_options: Any) -> Self:
349+
return cls(uri, **storage_options)
350+
347351
def as_uri(self) -> str:
348352
return self.__wrapped__.as_uri()
349353

upath/implementations/local.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ def full_match(self, pattern: str) -> bool:
394394
# todo: revisit
395395
return self.match(pattern)
396396

397+
@classmethod
398+
def from_uri(cls, uri: str, **storage_options: Any) -> Self:
399+
return UPath(uri, **storage_options) # type: ignore[return-value]
400+
397401
if sys.version_info < (3, 12):
398402

399403
def is_junction(self) -> bool:

0 commit comments

Comments
 (0)