Skip to content

Commit 85807ed

Browse files
ambvgvanrossum
authored andcommitted
logging.Formatter attributes fixed (#721)
Fixes #720. Related changes: used a NamedTuple for time.struct_time on Python 3, used sys.version selectors for proper typing of #716, added missing *Style classes to logging on Python 3.
1 parent 0391e72 commit 85807ed

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

stdlib/2and3/logging/__init__.pyi

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
## Stubs for logging (Python 3.4)
22

33
from typing import (
4-
Any, Callable, Iterable, Mapping, MutableMapping, Optional, IO, Tuple,
5-
Text, Union,
6-
overload,
4+
Any, Callable, Dict, Iterable, Mapping, MutableMapping, Optional, IO,
5+
Tuple, Text, Union, overload,
76
)
7+
from string import Template
8+
from time import struct_time
89
from types import TracebackType
910
import sys
1011

@@ -26,7 +27,7 @@ class Logger:
2627
handlers = ... # type: List[Handler]
2728
disabled = ... # type: int
2829
def setLevel(self, lvl: Union[int, str]) -> None: ...
29-
def isEnabledFor(self, lvl: int) -> Union[int, bool]: ...
30+
def isEnabledFor(self, lvl: int) -> bool: ...
3031
def getEffectiveLevel(self) -> int: ...
3132
def getChild(self, suffix: str) -> 'Logger': ...
3233
if sys.version_info > (3,):
@@ -135,6 +136,14 @@ class Handler:
135136

136137

137138
class Formatter:
139+
converter = ... # type: Callable[[Optional[float]], struct_time]
140+
_fmt = ... # type: Optional[str]
141+
datefmt = ... # type: Optional[str]
142+
if sys.version_info >= (3,):
143+
_style = ... # type: PercentStyle
144+
default_time_format = ... # type: str
145+
default_msec_format = ... # type: str
146+
138147
if sys.version_info >= (3,):
139148
def __init__(self, fmt: Optional[str] = ...,
140149
datefmt: Optional[str] =...,
@@ -143,6 +152,7 @@ class Formatter:
143152
def __init__(self,
144153
fmt: Optional[str] = ...,
145154
datefmt: Optional[str] =...) -> None: ...
155+
146156
def format(self, record: LogRecord) -> str: ...
147157
def formatTime(self, record: LogRecord, datefmt: str = ...) -> str: ...
148158
def formatException(self, exc_info: _SysExcInfoType) -> str: ...
@@ -240,7 +250,7 @@ class LoggerAdapter:
240250
def log(self,
241251
lvl: int, msg: Text, *args: Any, exc_info: _ExcInfoType = ...,
242252
extra: Dict[str, Any] = ..., **kwargs: Any) -> None: ...
243-
def isEnabledFor(self, lvl: int) -> Union[int, bool]: ...
253+
def isEnabledFor(self, lvl: int) -> bool: ...
244254
if sys.version_info >= (3,):
245255
def getEffectiveLevel(self) -> int: ...
246256
def setLevel(self, lvl: Union[int, str]) -> None: ...
@@ -353,3 +363,24 @@ class RootLogger(Logger):
353363
pass
354364

355365
root = ... # type: RootLogger
366+
367+
368+
if sys.version_info >= (3,):
369+
class PercentStyle(object):
370+
default_format = ... # type: str
371+
asctime_format = ... # type: str
372+
asctime_search = ... # type: str
373+
_fmt = ... # type: str
374+
375+
def __init__(self, fmt) -> None: ...
376+
def usesTime(self) -> bool: ...
377+
def format(self, record: Any) -> str: ...
378+
379+
class StrFormatStyle(PercentStyle):
380+
...
381+
382+
class StringTemplateStyle(PercentStyle):
383+
_tpl = ... # type: Template
384+
385+
BASIC_FORMAT = ... # type: str
386+
_STYLES = ... # type: Dict[str, Tuple[PercentStyle, str]]

stdlib/3/time.pyi

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# see: http://nullege.com/codes/search?cq=time
66

77
import sys
8-
from typing import Tuple, Union
8+
from typing import Any, NamedTuple, Tuple, Union
99
from types import SimpleNamespace
1010

1111
TimeTuple = Tuple[int, int, int, int, int, int, int, int, int]
@@ -26,27 +26,36 @@ if sys.version_info >= (3, 3) and sys.platform != 'win32':
2626
CLOCK_THREAD_CPUTIME_ID = 0 # Unix only
2727

2828

29-
# ----- classes/methods -----
30-
class struct_time:
31-
# this is supposed to be a namedtuple object
32-
# namedtuple is not yet implemented (see file: mypy/stubs/collections.py)
33-
# see: http://docs.python.org/3.2/library/time.html#time.struct_time
34-
# see: http://nullege.com/codes/search/time.struct_time
35-
# TODO: namedtuple() object problem
36-
#namedtuple __init__(self, int, int, int, int, int, int, int, int, int):
37-
# ...
38-
tm_year = 0
39-
tm_mon = 0
40-
tm_mday = 0
41-
tm_hour = 0
42-
tm_min = 0
43-
tm_sec = 0
44-
tm_wday = 0
45-
tm_yday = 0
46-
tm_isdst = 0
47-
if sys.version_info >= (3, 3):
48-
tm_gmtoff = 0
49-
tm_zone = 'GMT'
29+
if sys.version_info >= (3, 3):
30+
class struct_time(
31+
NamedTuple(
32+
'_struct_time',
33+
[('tm_year', int), ('tm_mon', int), ('tm_mday', int),
34+
('tm_hour', int), ('tm_min', int), ('tm_sec', int),
35+
('tm_wday', int), ('tm_yday', int), ('tm_isdst', int),
36+
('tm_zone', str), ('tm_gmtoff', int)]
37+
)
38+
):
39+
def __init__(
40+
self,
41+
o: Union[
42+
Tuple[int, int, int, int, int, int, int, int, int],
43+
Tuple[int, int, int, int, int, int, int, int, int, str],
44+
Tuple[int, int, int, int, int, int, int, int, int, str, int]
45+
],
46+
_arg: Any = ...,
47+
) -> None: ...
48+
else:
49+
class struct_time(
50+
NamedTuple(
51+
'_struct_time',
52+
[('tm_year', int), ('tm_mon', int), ('tm_mday', int),
53+
('tm_hour', int), ('tm_min', int), ('tm_sec', int),
54+
('tm_wday', int), ('tm_yday', int), ('tm_isdst', int)]
55+
)
56+
):
57+
def __init__(self, o: TimeTuple, _arg: Any = ...) -> None: ...
58+
5059

5160
# ----- functions -----
5261
def asctime(t: Union[TimeTuple, struct_time, None] = ...) -> str: ... # return current time

0 commit comments

Comments
 (0)