Skip to content

Return type overwritten by incorrectly-synthesised type #10309

@layday

Description

@layday

The shutil.move function is defined in the typeshed as:

def move(src: StrPath, dst: StrPath, copy_function: _CopyFn = ...) -> _PathReturn: ...

... where StrPath is an alias of str | os.PathLike[str] and _PathReturn is an alias of Any.

Previously, pyright would not attempt to synthesise the return type and move would consistently return Any. As of 1.1.399, the dst parameter appears to act like a type variable and the return type of move is the union of _PathReturn, str and the type accepted by PathLike:

$ cat test.py
import typing
from pathlib import Path
from shutil import move

typing.reveal_type(move)
typing.reveal_type(move('abc', 'def'))
typing.reveal_type(move(Path('abc'), Path('def')))
$ npx -y pyright@1.1.398 test.py
/[...]/test.py
  /[...]/test.py:5:20 - information: Type of "move" is "(src: str | PathLike[str], dst: str | PathLike[str], copy_function: ((str, str) -> object) | ((str | PathLike[str], str | PathLike[str]) -> object) = ...) -> Any"
  /[...]/test.py:6:20 - information: Type of "move('abc', 'def')" is "Any"
  /[...]/test.py:7:20 - information: Type of "move(Path('abc'), Path('def'))" is "Any"
0 errors, 0 warnings, 3 informations
$ npx -y pyright test.py
/[...]/test.py
  /[...]/test.py:5:20 - information: Type of "move" is "(src: str | PathLike[str], dst: _StrPathT@move, copy_function: ((str, str) -> object) | ((str | PathLike[str], str | PathLike[str]) -> object) = ...) -> (_StrPathT@move | str | Any)"
  /[...]/test.py:6:20 - information: Type of "move('abc', 'def')" is "str | Any"
  /[...]/test.py:7:20 - information: Type of "move(Path('abc'), Path('def'))" is "Path | str | Any"
0 errors, 0 warnings, 3 informations

This is surprising and does not reflect how move behaves at runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intendedbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions