Skip to content

Commit 284279d

Browse files
committed
Refactored mkpath as a singledispatch function.
1 parent 45b1295 commit 284279d

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

distutils/dir_util.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Utility functions for manipulating directories and directory trees."""
44

5+
import functools
56
import itertools
67
import os
78
import pathlib
@@ -14,7 +15,8 @@
1415
_path_created = set()
1516

1617

17-
def mkpath(name, mode=0o777, verbose=True, dry_run=False):
18+
@functools.singledispatch
19+
def mkpath(name: pathlib.Path, mode=0o777, verbose=True, dry_run=False):
1820
"""Create a directory and any missing ancestor directories.
1921
2022
If the directory already exists (or if 'name' is the empty string, which
@@ -27,12 +29,6 @@ def mkpath(name, mode=0o777, verbose=True, dry_run=False):
2729

2830
global _path_created
2931

30-
# Detect a common bug -- name is None
31-
if not isinstance(name, str):
32-
raise DistutilsInternalError(f"mkpath: 'name' must be a string (got {name!r})")
33-
34-
name = pathlib.Path(name)
35-
3632
if str(name.absolute()) in _path_created:
3733
return
3834

@@ -51,6 +47,19 @@ def mkpath(name, mode=0o777, verbose=True, dry_run=False):
5147
return list(map(str, missing))
5248

5349

50+
@mkpath.register
51+
def _(name: str, *args, **kwargs):
52+
return mkpath(pathlib.Path(name), *args, **kwargs)
53+
54+
55+
@mkpath.register
56+
def _(name: None, *args, **kwargs):
57+
"""
58+
Detect a common bug -- name is None.
59+
"""
60+
raise DistutilsInternalError(f"mkpath: 'name' must be a string (got {name!r})")
61+
62+
5463
def create_tree(base_dir, files, mode=0o777, verbose=True, dry_run=False):
5564
"""Create all the empty directories under 'base_dir' needed to put 'files'
5665
there.

0 commit comments

Comments
 (0)