1- import os
21import pathlib
32import re
3+ from typing import Union
44import urllib
5- from abc import ABCMeta
65
76from fsspec .registry import (
87 get_filesystem_class ,
@@ -87,22 +86,11 @@ def __getattribute__(self, item):
8786 )
8887
8988
90- class PureUPath (pathlib .PurePath ):
91- _flavour = pathlib ._posix_flavour
92- __slots__ = ()
93-
94-
95- class UPathMeta (ABCMeta ):
96- def __instancecheck__ (cls , instance ):
97- return isinstance (instance , pathlib .Path )
98-
99- def __subclasscheck__ (cls , subclass ):
100- return issubclass (subclass , pathlib .Path )
101-
102-
103- class UPath (pathlib .Path , PureUPath , metaclass = UPathMeta ):
89+ class UPath (pathlib .Path ):
10490
10591 __slots__ = ("_url" , "_kwargs" , "_closed" , "_accessor" )
92+ _flavour = pathlib ._posix_flavour
93+ _default_accessor = _FSSpecAccessor
10694
10795 not_implemented = [
10896 "cwd" ,
@@ -114,60 +102,46 @@ class UPath(pathlib.Path, PureUPath, metaclass=UPathMeta):
114102 "owner" ,
115103 "readlink" ,
116104 ]
117- _default_accessor = _FSSpecAccessor
118105
119- def __new__ (cls , * args , ** kwargs ):
120- if issubclass (cls , UPath ):
121- args_list = list (args )
122- first = args_list .pop (0 )
123- if isinstance (first , pathlib .PurePath ):
124- # Create a (modified) copy, if first arg is a Path object
125- other = first
126- parts = args_list
127- drv , root , parts = other ._parse_args (parts )
128- drv , root , parts = other ._flavour .join_parsed_parts (
129- other ._drv , other ._root , other ._parts , drv , root , parts
130- )
106+ def __new__ (cls , * args , ** kwargs ) -> Union ["UPath" , pathlib .Path ]:
107+ args_list = list (args )
108+ first = args_list .pop (0 )
109+ if isinstance (first , pathlib .PurePath ):
110+ # Create a (modified) copy, if first arg is a Path object
111+ other = first
112+ parts = args_list
113+ drv , root , parts = other ._parse_args (parts )
114+ drv , root , parts = other ._flavour .join_parsed_parts (
115+ other ._drv , other ._root , other ._parts , drv , root , parts
116+ )
131117
132- new_kwargs = getattr (other , "_kwargs" , {}).copy ()
133- new_kwargs .pop ("_url" , None )
134- new_kwargs .update (kwargs )
118+ new_kwargs = getattr (other , "_kwargs" , {}).copy ()
119+ new_kwargs .pop ("_url" , None )
120+ new_kwargs .update (kwargs )
135121
136- return other .__class__ (
137- other ._format_parsed_parts (drv , root , parts ),
138- ** new_kwargs ,
139- )
122+ return other .__class__ (
123+ other ._format_parsed_parts (drv , root , parts ),
124+ ** new_kwargs ,
125+ )
140126
141- url = stringify_path (first )
142- parsed_url = urllib .parse .urlparse (url )
143- for key in ["scheme" , "netloc" ]:
144- val = kwargs .get (key )
145- if val :
146- parsed_url = parsed_url ._replace (** {key : val })
147- # treat as local filesystem, return PosixPath or WindowsPath
148- impls = list (registry ) + list (known_implementations .keys ())
149- if not parsed_url .scheme or parsed_url .scheme not in impls :
150- cls = (
151- pathlib .WindowsPath
152- if os .name == "nt"
153- else pathlib .PosixPath
154- )
155- self = cls ._from_parts (args )
156- if not self ._flavour .is_supported :
157- raise NotImplementedError (
158- "cannot instantiate %r on your system" % (cls .__name__ ,)
159- )
160- else :
161- import upath .registry
127+ url = stringify_path (first )
128+ parsed_url = urllib .parse .urlparse (url )
129+ for key in ["scheme" , "netloc" ]:
130+ val = kwargs .get (key )
131+ if val :
132+ parsed_url = parsed_url ._replace (** {key : val })
162133
163- cls = upath .registry ._registry [parsed_url .scheme ]
164- kwargs ["_url" ] = parsed_url
165- args_list .insert (0 , parsed_url .path )
166- args = tuple (args_list )
167- self = cls ._from_parts (args , ** kwargs )
168- else :
169- self = super ().__new__ (* args , ** kwargs )
170- return self
134+ fsspec_impls = list (registry ) + list (known_implementations .keys ())
135+ if parsed_url .scheme and parsed_url .scheme in fsspec_impls :
136+ import upath .registry
137+
138+ cls = upath .registry ._registry [parsed_url .scheme ]
139+ kwargs ["_url" ] = parsed_url
140+ args_list .insert (0 , parsed_url .path )
141+ return cls ._from_parts (tuple (args_list ), ** kwargs )
142+
143+ # treat as local filesystem, return PosixPath or WindowsPath
144+ return pathlib .Path (* args , ** kwargs )
171145
172146 def __getattr__ (self , item ):
173147 if item == "_accessor" :
0 commit comments