Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a TypeError in compat.py311.shutil_rmtree #4382

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Any, Callable, TYPE_CHECKING

if TYPE_CHECKING:
from _typeshed import StrOrBytesPath, ExcInfo

Comment on lines +5 to +8
Copy link
Contributor Author

@Avasam Avasam May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway we can simplify this?

The runtime fix is just the added conditional. Everything else is type hints.

I just combined imports. I could omit annotating the internal function and move StrOrBytesPath to a runtime alias with StrPath in setuptools._path.

# Same as shutil._OnExcCallback from typeshed
_OnExcCallback = Callable[[Callable[..., Any], str, BaseException], object]


def shutil_rmtree(path, ignore_errors=False, onexc=None):
def shutil_rmtree(
path: StrOrBytesPath,
ignore_errors: bool = False,
onexc: _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:
onexc(fn, path, excinfo[1])

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