Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Dec 15, 2017

Rework of #1583. Fixes #1573.

See documentation in https://docs.python.org/3/library/os.html#os.scandir.

To test this, I used the following file:

import os
with os.scandir() as it:
    reveal_type(it)  # _ScandirIterator[str]
    for f in it:
        reveal_type(f)  # DirEntry[str]
with os.scandir(b'.') as bit:
    reveal_type(bit)  # _ScandirIterator[bytes]
    for bf in bit:
        reveal_type(bf)  # DirEntry[bytes]
class BytesPath(os.PathLike[bytes]):
    def __fspath__(self) -> bytes:
        return b'ytes'
with os.scandir(BytesPath()) as bpit:
    reveal_type(bpit)  # _ScandirIterator[bytes]
    for bpf in bpit:
        reveal_type(bpf)  # DirEntry[bytes]

I got

$ mypy --custom-typeshed-dir . samples/scandir.py
samples/scandir.py:3: error: Revealed type is 'os._ScandirIterator*[builtins.str*]'
samples/scandir.py:5: error: Revealed type is 'os.DirEntry*[builtins.str*]'
samples/scandir.py:7: error: Revealed type is 'os._ScandirIterator*[builtins.bytes*]'
samples/scandir.py:9: error: Revealed type is 'os.DirEntry*[builtins.bytes*]'
samples/scandir.py:13: error: Argument 1 to "scandir" has incompatible type "BytesPath"; expected "Union[str, _PathLike[str]]"
samples/scandir.py:14: error: Revealed type is 'os._ScandirIterator*[builtins.str*]'
samples/scandir.py:16: error: Revealed type is 'os.DirEntry*[builtins.str*]'

The error on line 13 is unexpected, but it also appears without this PR. I'll file a bug to mypy for it. (Edit: it's python/mypy#3644.)

@matthiaskramm matthiaskramm merged commit 4ee508a into python:master Jan 2, 2018
@JelleZijlstra JelleZijlstra deleted the scandirwith branch May 30, 2020 05:07
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