Skip to content

Commit

Permalink
Prevent a TypeError: 'NoneType' object is not callable when ``shu…
Browse files Browse the repository at this point in the history
…til_rmtree`` is called without an ``onexc`` parameter on Python<=3.11
  • Loading branch information
Avasam committed May 22, 2024
1 parent 52d7324 commit 0832489
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions newsfragments/4382.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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`
22 changes: 18 additions & 4 deletions setuptools/compat/py311.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import sys
from __future__ import annotations

import shutil
import sys
from typing import TYPE_CHECKING
from collections.abc import Callable
from typing import Any

if TYPE_CHECKING:
from _typeshed import StrOrBytesPath, ExcInfo


def shutil_rmtree(path, ignore_errors=False, onexc=None):
def shutil_rmtree(
path: StrOrBytesPath,
ignore_errors: bool = False,
onexc: shutil._OnExcCallback | None = None,
) -> None:
if sys.version_info >= (3, 12):
return shutil.rmtree(path, ignore_errors, onexc=onexc)

def _handler(fn, path, excinfo):
return onexc(fn, path, excinfo[1])
def _handler(fn: Callable[..., Any], path: str, excinfo: ExcInfo) -> None:
if onexc:
# type-ignore fixed in https://github.com/python/typeshed/pull/12002
onexc(fn, path, excinfo[1]) # type: ignore[arg-type]

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

0 comments on commit 0832489

Please sign in to comment.