Skip to content

Commit 3ec0610

Browse files
authored
Improve logging.StreamHandler and FileHandler (#5663)
* Use PEP 604, instead of Optional. * Override FileHandler.stream and setStream to require a TextIOWrapper instead of a SupportsWrite object. * FileHandler._open() now returns a TextIOWrapper instead of IO. Using anything but a file opened in text mode would not work.
1 parent 11b99e1 commit 3ec0610

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

stdlib/logging/__init__.pyi

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import sys
22
import threading
33
from _typeshed import StrPath, SupportsWrite
44
from collections.abc import Callable, Iterable, Mapping, MutableMapping, Sequence
5+
from io import TextIOWrapper
56
from string import Template
67
from time import struct_time
78
from types import FrameType, TracebackType
8-
from typing import IO, Any, ClassVar, Optional, Pattern, Tuple, Type, Union
9+
from typing import Any, ClassVar, Optional, Pattern, Tuple, Type, Union
910

1011
_SysExcInfoType = Union[Tuple[Type[BaseException], BaseException, Optional[TracebackType]], Tuple[None, None, None]]
1112
_ExcInfoType = Union[None, bool, _SysExcInfoType, BaseException]
@@ -754,28 +755,26 @@ lastResort: Optional[StreamHandler]
754755
class StreamHandler(Handler):
755756
stream: SupportsWrite[str] # undocumented
756757
terminator: str
757-
def __init__(self, stream: Optional[SupportsWrite[str]] = ...) -> None: ...
758+
def __init__(self, stream: SupportsWrite[str] | None = ...) -> None: ...
758759
if sys.version_info >= (3, 7):
759-
def setStream(self, stream: SupportsWrite[str]) -> Optional[SupportsWrite[str]]: ...
760+
def setStream(self, stream: SupportsWrite[str]) -> SupportsWrite[str] | None: ...
760761

761762
class FileHandler(StreamHandler):
763+
stream: TextIOWrapper # undocumented
762764
baseFilename: str # undocumented
763765
mode: str # undocumented
764-
encoding: Optional[str] # undocumented
766+
encoding: str | None # undocumented
765767
delay: bool # undocumented
766768
if sys.version_info >= (3, 9):
767-
errors: Optional[str] # undocumented
769+
errors: str | None # undocumented
768770
def __init__(
769-
self,
770-
filename: StrPath,
771-
mode: str = ...,
772-
encoding: Optional[str] = ...,
773-
delay: bool = ...,
774-
errors: Optional[str] = ...,
771+
self, filename: StrPath, mode: str = ..., encoding: str | None = ..., delay: bool = ..., errors: str | None = ...
775772
) -> None: ...
776773
else:
777-
def __init__(self, filename: StrPath, mode: str = ..., encoding: Optional[str] = ..., delay: bool = ...) -> None: ...
778-
def _open(self) -> IO[Any]: ...
774+
def __init__(self, filename: StrPath, mode: str = ..., encoding: str | None = ..., delay: bool = ...) -> None: ...
775+
def _open(self) -> TextIOWrapper: ...
776+
if sys.version_info >= (3, 7):
777+
def setStream(self, stream: TextIOWrapper) -> TextIOWrapper | None: ... # type: ignore
779778

780779
class NullHandler(Handler): ...
781780

0 commit comments

Comments
 (0)