Skip to content

Commit cd7a184

Browse files
committed
Clean up parent to use dirLike
Document split can use the file system.
1 parent 3d2f18f commit cd7a184

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

python/lsst/resources/_resourcePath.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,21 @@ def split(self) -> tuple[ResourcePath, str]:
429429
ResourcePath rules.
430430
tail : `str`
431431
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.
434434
435435
Notes
436436
-----
437437
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.
439443
"""
440444
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.
442447
return self, ""
443448

444449
head, tail = self._pathModule.split(self.path)
@@ -447,7 +452,7 @@ def split(self) -> tuple[ResourcePath, str]:
447452
# The file part should never include quoted metacharacters
448453
tail = urllib.parse.unquote(tail)
449454

450-
# Schemeless is special in that it can be a relative path
455+
# Schemeless is special in that it can be a relative path.
451456
# We need to ensure that it stays that way. All other URIs will
452457
# be absolute already.
453458
forceAbsolute = self.isabs()
@@ -482,7 +487,9 @@ def dirname(self) -> ResourcePath:
482487
483488
Notes
484489
-----
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`.
486493
"""
487494
return self.split()[0]
488495

@@ -497,12 +504,16 @@ def parent(self) -> ResourcePath:
497504
498505
Notes
499506
-----
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``.
501512
"""
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.
504515
return self.dirname()
505-
# When self is dir-like, return its parent directory,
516+
# When self is dir-like, returns its parent directory,
506517
# regardless of the presence of a trailing separator
507518
originalPath = self._pathLib(self.path)
508519
parentPath = originalPath.parent

python/lsst/resources/tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def test_parents(self) -> None:
339339
self.assertEqual(derived_parent, parent)
340340
self.assertTrue(derived_parent.isdir())
341341
self.assertEqual(child_file.parent().parent(), parent)
342+
self.assertEqual(child_subdir.dirname(), child_subdir)
342343

343344
def test_escapes(self) -> None:
344345
"""Special characters in file paths."""

0 commit comments

Comments
 (0)