Skip to content

Commit

Permalink
Merge pull request #216 from 5j9/main
Browse files Browse the repository at this point in the history
Use '.' as the default value for Path constructor
  • Loading branch information
jaraco authored Dec 4, 2023
2 parents 5421509 + 42c4778 commit 12caf1a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions newsfragments/216.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use '.' as the default path.
5 changes: 4 additions & 1 deletion path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ class Path(str):
.. seealso:: :mod:`os.path`
"""

def __init__(self, other=''):
def __new__(cls, other='.'):
return super().__new__(cls, other)

def __init__(self, other='.'):
if other is None:
raise TypeError("Invalid initial value for path: None")
with contextlib.suppress(AttributeError):
Expand Down
10 changes: 8 additions & 2 deletions test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_relpath(self):
d = Path('D:\\')
assert d.relpathto(boz) == boz

def test_construction_without_args(self):
"""
Path class will construct a path to current directory when called with no arguments.
"""
assert Path() == '.'

def test_construction_from_none(self):
""" """
with pytest.raises(TypeError):
Expand Down Expand Up @@ -424,15 +430,15 @@ def test_chroot(monkeypatch):
results = []
monkeypatch.setattr(os, 'chroot', results.append)
Path().chroot()
assert results == ['']
assert results == [Path()]


@pytest.mark.skipif("not hasattr(Path, 'startfile')")
def test_startfile(monkeypatch):
results = []
monkeypatch.setattr(os, 'startfile', results.append)
Path().startfile()
assert results == ['']
assert results == [Path()]


class TestScratchDir:
Expand Down

0 comments on commit 12caf1a

Please sign in to comment.