Skip to content

Make shutil.rmtree.onexc parameter optional #12002

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

Merged
merged 4 commits into from
May 22, 2024

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented May 22, 2024

Noticed while adding pyright checks to setuptools (pypa/setuptools#4192)

Doc reference: https://docs.python.org/3/library/shutil.html#shutil.rmtree

I also tested locally that the parameter is indeed optional.


I also changed the Exception type in _OnExcCallback for baseException so it works with ExcInfo:

image

Argument of type "_OnExcCallback | None" cannot be assigned to parameter "onexc" of type "_OnExcCallback" in function "__call__"
  Type "_OnExcCallback | None" is incompatible with type "_OnExcCallback"
    Type "None" is incompatible with type "_OnExcCallback"

Argument 3 has incompatible type "BaseException"; expected "Exception"

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: Callable[..., Any], path: str, excinfo: ExcInfo) -> None:
        if onexc:
            onexc(fn, path, excinfo[1])

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

xref pypa/setuptools#4382

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Comment on lines 92 to 100
@overload
def __call__(
self, path: StrOrBytesPath, ignore_errors: bool = False, *, onexc: _OnExcCallback, dir_fd: int | None = None
self,
path: StrOrBytesPath,
ignore_errors: bool = False,
*,
onexc: _OnExcCallback | None = None,
dir_fd: int | None = None,
) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure why we need this and the first overload. The first overload seems redundant.

Copy link
Collaborator Author

@Avasam Avasam May 22, 2024

Choose a reason for hiding this comment

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

Agreed, given the params are kw only and optional. Good catch!

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@Avasam Avasam requested a review from srittau May 22, 2024 19:19
@srittau srittau merged commit 50cbca4 into python:main May 22, 2024
64 checks passed
@Avasam Avasam deleted the rmtree-onexc-optional branch May 22, 2024 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants