Skip to content

Allow os.readlink to accept path-like objects #1441

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 2 commits into from
Jul 1, 2017

Conversation

rhysparry
Copy link
Contributor

No description provided.

@@ -319,7 +319,7 @@ def mkdir(path: _PathType, mode: int = ...) -> None: ...
def makedirs(path: _PathType, mode: int = ...,
exist_ok: bool = ...) -> None: ...
def pathconf(path: _PathType, name: Union[str, int]) -> int: ... # Unix only
def readlink(path: AnyStr) -> AnyStr: ...
def readlink(path: _PathType) -> AnyStr: ...
Copy link
Member

Choose a reason for hiding this comment

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

This is a little subtle, but _PathType is actually generic over AnyStr. And you'll need that for the return type. You can try to fix this by adding _PathType[AnyStr]. Hopefully that fixes the issue (which is actually caused by the inferred return type of readlink() being broken by to this change).

@gvanrossum
Copy link
Member

gvanrossum commented Jun 28, 2017

I've got an example where I don't know what it should do.

import os
class P(os.PathLike[bytes]):
    def __fspath__(self) -> bytes:
        return b'x'
print(os.readlink(P()))  # Error

This produces

__tmp__.py:5: error: Argument 1 to "readlink" has incompatible type "P"; expected "Union[str, _PathLike[str]]"

Without this PR it gives a different error:

__tmp__.py:5: error: Type argument 1 of "readlink" has incompatible value "P"

(At runtime it fails because the stdlib's os.PathLike class is not generic, but I think that's a separate issue.)

I'm not sure whether this is worth holding up this PR for though, since it's not truly a regression (more of a detour maybe :-).

@JelleZijlstra
Copy link
Member

That seems like a mypy bug, since P should obviously be compatible with PathLike[bytes]. I'm curious where the strs in the error message come from, since this PR has AnyStr.

@gvanrossum
Copy link
Member

Maybe, I don't have time to look into it right now. :-(

@JelleZijlstra
Copy link
Member

Reported Guido's example above as a mypy bug in python/mypy#3644.

@rhysparry thanks for your contribution!

rhysparry added a commit to rhysparry/typeshed that referenced this pull request Jul 3, 2017
* python/master:
  Added stub for toaiff module (python#1455)
  Added stub for user module (python#1454)
  Add more multiprocessing function stubs (python#1435)
  PyYaml: uncomment commented out imports and add missing classmethod decorators (python#1439)
  Allow `os.readlink` to accept path-like objects (python#1441)
  Support named attributes in `os.uname()` result (python#1445)
  Fix signature for slite3.fetchmany (python#1444)
  Add __name__ field to MethodType (python#1442)
  Add TypedDict total argument (python#1443)
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.

3 participants