Skip to content

Change scandir() to type as both iterator and context-manager #1583

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions stdlib/3/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ from builtins import OSError as error
from io import TextIOWrapper as _TextIOWrapper
import sys
from typing import (
Mapping, MutableMapping, Dict, List, Any, Tuple, IO, Iterable, Iterator, overload, Union, AnyStr,
Optional, Generic, Set, Callable, Text, Sequence, NamedTuple, TypeVar
Mapping, MutableMapping, Dict, List, Any, Tuple, Iterator, overload, Union, AnyStr,
Optional, Generic, Set, Callable, Text, Sequence, IO, NamedTuple, TypeVar, Iterable
)
from . import path as path
from mypy_extensions import NoReturn
from types import TracebackType

_T = TypeVar('_T')

Expand Down Expand Up @@ -487,10 +488,16 @@ if sys.version_info >= (3, 3):
else:
def rmdir(path: _PathType) -> None: ...
if sys.version_info >= (3, 6):
class ScandirIterator(Iterable):
def __iter__(self) -> Iterator[DirEntry[str]]: ...
def __next__(self) -> DirEntry[str]: ...
def __enter__(self: _T) -> _T: ...
Copy link
Member

Choose a reason for hiding this comment

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

Instead of _T, use a special type variable with bound=ScandirIterator[AnyStr].

def __exit__(self, exc_type: Optional[type], exc_val: Optional[Exception],
Copy link
Member

Choose a reason for hiding this comment

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

exc_type should be Optional[Type[BaseException]] and exc_val should be Optional[BaseException].

exc_tb: Optional[TracebackType]) -> bool: ...
@overload
def scandir() -> Iterator[DirEntry[str]]: ...
Copy link
Member

Choose a reason for hiding this comment

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

Why is this overload unaffected?

@overload
def scandir(path: Union[AnyStr, PathLike[AnyStr]]) -> Iterator[DirEntry[AnyStr]]: ...
def scandir(path: Union[AnyStr, PathLike[AnyStr]]) -> ScandirIterator: ...
elif sys.version_info >= (3, 5):
@overload
def scandir() -> Iterator[DirEntry[str]]: ...
Expand Down