Skip to content

Commit 5ed52c1

Browse files
authored
Prevent a TypeError in compat.py311.shutil_rmtree (#4382)
2 parents f91fa3d + 6e4cf9f commit 5ed52c1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

newsfragments/4382.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent a ``TypeError: 'NoneType' object is not callable`` when ``shutil_rmtree`` is called without an ``onexc`` parameter on Python<=3.11 -- by :user:`Avasam`

setuptools/compat/py311.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
import sys
1+
from __future__ import annotations
2+
23
import shutil
4+
import sys
5+
from typing import Any, Callable, TYPE_CHECKING
6+
7+
if TYPE_CHECKING:
8+
from _typeshed import StrOrBytesPath, ExcInfo
9+
10+
# Same as shutil._OnExcCallback from typeshed
11+
_OnExcCallback = Callable[[Callable[..., Any], str, BaseException], object]
312

413

5-
def shutil_rmtree(path, ignore_errors=False, onexc=None):
14+
def shutil_rmtree(
15+
path: StrOrBytesPath,
16+
ignore_errors: bool = False,
17+
onexc: _OnExcCallback | None = None,
18+
) -> None:
619
if sys.version_info >= (3, 12):
720
return shutil.rmtree(path, ignore_errors, onexc=onexc)
821

9-
def _handler(fn, path, excinfo):
10-
return onexc(fn, path, excinfo[1])
22+
def _handler(fn: Callable[..., Any], path: str, excinfo: ExcInfo) -> None:
23+
if onexc:
24+
onexc(fn, path, excinfo[1])
1125

1226
return shutil.rmtree(path, ignore_errors, onerror=_handler)

0 commit comments

Comments
 (0)