Skip to content

Commit 5733d29

Browse files
committed
logging: Make LoggerAdapter and StreamHandler generic in Python 2
Without this writing straddling code is quite tricky, as these are generic in Python 3. Manually cherry-picked from python/typeshed#6330.
1 parent e68605c commit 5733d29

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

mypy/typeshed/stdlib/@python2/logging/__init__.pyi

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import threading
2-
from _typeshed import StrPath
2+
from _typeshed import StrPath, SupportsWrite
33
from time import struct_time
44
from types import FrameType, TracebackType
5-
from typing import IO, Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence, Text, Tuple, Union, overload
5+
from typing import (
6+
IO,
7+
Any,
8+
Callable,
9+
Dict,
10+
Generic,
11+
List,
12+
Mapping,
13+
MutableMapping,
14+
Optional,
15+
Sequence,
16+
Text,
17+
Tuple,
18+
TypeVar,
19+
Union,
20+
overload,
21+
)
622

723
_SysExcInfoType = Union[Tuple[type, BaseException, Optional[TracebackType]], Tuple[None, None, None]]
824
_ExcInfoType = Union[None, bool, _SysExcInfoType]
@@ -159,10 +175,12 @@ class LogRecord:
159175
) -> None: ...
160176
def getMessage(self) -> str: ...
161177

162-
class LoggerAdapter:
163-
logger: Logger
178+
_L = TypeVar("_L", Logger, LoggerAdapter[Logger], LoggerAdapter[Any])
179+
180+
class LoggerAdapter(Generic[_L]):
181+
logger: _L
164182
extra: Mapping[str, Any]
165-
def __init__(self, logger: Logger, extra: Mapping[str, Any]) -> None: ...
183+
def __init__(self, logger: _L, extra: Mapping[str, Any]) -> None: ...
166184
def process(self, msg: Any, kwargs: MutableMapping[str, Any]) -> Tuple[Any, MutableMapping[str, Any]]: ...
167185
def debug(
168186
self, msg: Any, *args: Any, exc_info: _ExcInfoType = ..., extra: Dict[str, Any] | None = ..., **kwargs: Any
@@ -227,9 +245,14 @@ def shutdown(handlerList: Sequence[Any] = ...) -> None: ... # handlerList is un
227245
def setLoggerClass(klass: type) -> None: ...
228246
def captureWarnings(capture: bool) -> None: ...
229247

230-
class StreamHandler(Handler):
231-
stream: IO[str] # undocumented
232-
def __init__(self, stream: IO[str] | None = ...) -> None: ...
248+
_StreamT = TypeVar("_StreamT", bound=SupportsWrite[str])
249+
250+
class StreamHandler(Handler, Generic[_StreamT]):
251+
stream: _StreamT # undocumented
252+
@overload
253+
def __init__(self: StreamHandler[IO[str]], stream: None = ...) -> None: ...
254+
@overload
255+
def __init__(self: StreamHandler[_StreamT], stream: _StreamT) -> None: ...
233256

234257
class FileHandler(StreamHandler):
235258
baseFilename: str # undocumented

0 commit comments

Comments
 (0)