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

Function accepts callable, but a typeshed custom Protocol was passed #13480

Closed
Andrew-Chen-Wang opened this issue Dec 4, 2024 · 2 comments · Fixed by #13478
Closed

Function accepts callable, but a typeshed custom Protocol was passed #13480

Andrew-Chen-Wang opened this issue Dec 4, 2024 · 2 comments · Fixed by #13478

Comments

@Andrew-Chen-Wang
Copy link

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

I have some code like:

await asyncio.to_thread(
    shutil.rmtree, connector_folder, ignore_errors=False, onerror=None
)

But I'm getting mypy errors like

Argument 1 to "to_thread" has incompatible type "_RmtreeType"; expected "Callable[[Path, bool, None], None]"  [arg-type]

Expected Behavior

I would assume this is fine this callable is fine and this is passing.

Actual Behavior

Argument 1 to "to_thread" has incompatible type "_RmtreeType"; expected "Callable[[Path, bool, None], None]"  [arg-type]

Your Environment

  • Mypy version used: 1.11.0
  • Mypy command-line flags: pointing to pyproject.toml config
  • Mypy configuration options from mypy.ini (and other config files): ignore_missing_imports = true
  • Python version used: 3.12

I do NOT have Typeshed installed in my pip list, but I saw in typeshed that it may be bundled with mypy? Again I don't see it installed, but curious to know why this wouldn't work.

@TeamSpen210
Copy link
Contributor

Indeed, mypy (and all other type checkers) bundle typeshed, since it’s where they get type definitions for everything in the standard library.

I can see the problem though: In 3.12, the stub specifies onerror as not accepting None. Though that’s the default anyway, so you could just omit it, or use the replacement onexc.

@Avasam
Copy link
Collaborator

Avasam commented Feb 8, 2025

You'll get this error only on Python 3.12+ because the typeshed definition didn't allow None for the onerror parameter on Python 3.12.

This is not a mypy issue and mypy is correctly interpreting the type here. Pyright has the same behaviour. The definition at

typeshed/stdlib/shutil.pyi

Lines 79 to 110 in 1d10860

if sys.version_info >= (3, 12):
@overload
@deprecated("The `onerror` parameter is deprecated. Use `onexc` instead.")
def __call__(
self,
path: StrOrBytesPath,
ignore_errors: bool,
onerror: _OnErrorCallback,
*,
onexc: None = None,
dir_fd: int | None = None,
) -> None: ...
@overload
@deprecated("The `onerror` parameter is deprecated. Use `onexc` instead.")
def __call__(
self,
path: StrOrBytesPath,
ignore_errors: bool = False,
*,
onerror: _OnErrorCallback,
onexc: None = None,
dir_fd: int | None = None,
) -> None: ...
@overload
def __call__(
self,
path: StrOrBytesPath,
ignore_errors: bool = False,
*,
onexc: _OnExcCallback | None = None,
dir_fd: int | None = None,
) -> None: ...
may be incorrect.

Do note however, that onerror is deprecated in favor of onexc

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 a pull request may close this issue.

3 participants