@@ -429,16 +429,21 @@ def split(self) -> tuple[ResourcePath, str]:
429
429
ResourcePath rules.
430
430
tail : `str`
431
431
Last path component. Tail will be empty if path ends on a
432
- separator. Tail will never contain separators. It will be
433
- unquoted.
432
+ separator or if the URI is known to be associated with a directory.
433
+ Tail will never contain separators. It will be unquoted.
434
434
435
435
Notes
436
436
-----
437
437
Equivalent to `os.path.split` where head preserves the URI
438
- components.
438
+ components. In some cases this method can result in a file system
439
+ check to verify whether the URI is a directory or not (only if
440
+ ``forceDirectory`` was `None` during construction). For a scheme-less
441
+ URI this can mean that the result might change depending on current
442
+ working directory.
439
443
"""
440
444
if self .isdir ():
441
- # This is a directory so must return itself and the empty string.
445
+ # This is known to be a directory so must return itself and
446
+ # the empty string.
442
447
return self , ""
443
448
444
449
head , tail = self ._pathModule .split (self .path )
@@ -447,7 +452,7 @@ def split(self) -> tuple[ResourcePath, str]:
447
452
# The file part should never include quoted metacharacters
448
453
tail = urllib .parse .unquote (tail )
449
454
450
- # Schemeless is special in that it can be a relative path
455
+ # Schemeless is special in that it can be a relative path.
451
456
# We need to ensure that it stays that way. All other URIs will
452
457
# be absolute already.
453
458
forceAbsolute = self .isabs ()
@@ -482,7 +487,9 @@ def dirname(self) -> ResourcePath:
482
487
483
488
Notes
484
489
-----
485
- Equivalent of `os.path.dirname`.
490
+ Equivalent of `os.path.dirname`. If this is a directory URI it will
491
+ be returned unchanged. If the parent directory is always required
492
+ use `parent`.
486
493
"""
487
494
return self .split ()[0 ]
488
495
@@ -497,12 +504,16 @@ def parent(self) -> ResourcePath:
497
504
498
505
Notes
499
506
-----
500
- For a file-like URI this will be the same as calling `dirname()`.
507
+ For a file-like URI this will be the same as calling `dirname`.
508
+ For a directory-like URI this will always return the parent directory
509
+ whereas `dirname()` will return the original URI. This is consistent
510
+ with `os.path.dirname` compared to the `pathlib.Path` property
511
+ ``parent``.
501
512
"""
502
- # When self is file-like, return self.dirname()
503
- if not self . isdir ():
513
+ if self . dirLike is False :
514
+ # os.path.split() is slightly faster than calling Path().parent.
504
515
return self .dirname ()
505
- # When self is dir-like, return its parent directory,
516
+ # When self is dir-like, returns its parent directory,
506
517
# regardless of the presence of a trailing separator
507
518
originalPath = self ._pathLib (self .path )
508
519
parentPath = originalPath .parent
0 commit comments