Skip to content

Commit dbdb63c

Browse files
author
Lukasz Langa
committed
logging.Formatter attributes fixed
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 bbbf1d8 commit dbdb63c

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

stdlib/2and3/logging/__init__.pyi

Lines changed: 39 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,6 @@ 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]: ...
3030
def getEffectiveLevel(self) -> int: ...
3131
def getChild(self, suffix: str) -> 'Logger': ...
3232
if sys.version_info > (3,):
@@ -54,6 +54,7 @@ class Logger:
5454
def exception(self, msg: Text, *args: Any, exc_info: _ExcInfoType = ...,
5555
stack_info: bool = ..., extra: Dict[str, Any] = ...,
5656
**kwargs: Any) -> None: ...
57+
def isEnabledFor(self, lvl: int) -> bool: ...
5758
else:
5859
def debug(self,
5960
msg: Text, *args: Any, exc_info: _ExcInfoType = ...,
@@ -79,6 +80,7 @@ class Logger:
7980
def exception(self,
8081
msg: Text, *args: Any, exc_info: _ExcInfoType = ...,
8182
extra: Dict[str, Any] = ..., **kwargs: Any) -> None: ...
83+
def isEnabledFor(self, lvl: int) -> Union[int, bool]: ...
8284
def addFilter(self, filt: _FilterType) -> None: ...
8385
def removeFilter(self, filt: _FilterType) -> None: ...
8486
def filter(self, record: 'LogRecord') -> bool: ...
@@ -135,6 +137,14 @@ class Handler:
135137

136138

137139
class Formatter:
140+
converter = ... # type: Callable[[Optional[float]], struct_time]
141+
_fmt = ... # type: Optional[str]
142+
datefmt = ... # type: Optional[str]
143+
if sys.version_info >= (3,):
144+
_style = ... # type: PercentStyle
145+
default_time_format = ... # type: str
146+
default_msec_format = ... # type: str
147+
138148
if sys.version_info >= (3,):
139149
def __init__(self, fmt: Optional[str] = ...,
140150
datefmt: Optional[str] =...,
@@ -143,6 +153,7 @@ class Formatter:
143153
def __init__(self,
144154
fmt: Optional[str] = ...,
145155
datefmt: Optional[str] =...) -> None: ...
156+
146157
def format(self, record: LogRecord) -> str: ...
147158
def formatTime(self, record: LogRecord, datefmt: str = ...) -> str: ...
148159
def formatException(self, exc_info: _SysExcInfoType) -> str: ...
@@ -240,11 +251,13 @@ class LoggerAdapter:
240251
def log(self,
241252
lvl: int, msg: Text, *args: Any, exc_info: _ExcInfoType = ...,
242253
extra: Dict[str, Any] = ..., **kwargs: Any) -> None: ...
243-
def isEnabledFor(self, lvl: int) -> Union[int, bool]: ...
244254
if sys.version_info >= (3,):
255+
def isEnabledFor(self, lvl: int) -> bool: ...
245256
def getEffectiveLevel(self) -> int: ...
246257
def setLevel(self, lvl: Union[int, str]) -> None: ...
247258
def hasHandlers(self) -> bool: ...
259+
else:
260+
def isEnabledFor(self, lvl: int) -> Union[int, bool]: ...
248261

249262

250263
if sys.version_info >= (3,):
@@ -353,3 +366,24 @@ class RootLogger(Logger):
353366
pass
354367

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

stdlib/3/time.pyi

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
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

11-
TimeTuple = Tuple[int, int, int, int, int, int, int, int, int]
11+
if sys.version_info >= (3, 3):
12+
TimeTuple = Union[
13+
Tuple[int, int, int, int, int, int, int, int, int],
14+
Tuple[int, int, int, int, int, int, int, int, int, str],
15+
Tuple[int, int, int, int, int, int, int, int, int, str, int]
16+
]
17+
else:
18+
TimeTuple = Tuple[int, int, int, int, int, int, int, int, int]
1219

1320
# ----- variables and constants -----
1421
accept2dyear = False
@@ -26,27 +33,28 @@ if sys.version_info >= (3, 3) and sys.platform != 'win32':
2633
CLOCK_THREAD_CPUTIME_ID = 0 # Unix only
2734

2835

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'
36+
if sys.version_info >= (3, 3):
37+
class struct_time(
38+
NamedTuple(
39+
'_struct_time',
40+
[('tm_year', int), ('tm_mon', int), ('tm_mday', int),
41+
('tm_hour', int), ('tm_min', int), ('tm_sec', int),
42+
('tm_wday', int), ('tm_yday', int), ('tm_isdst', int),
43+
('tm_zone', str), ('tm_gmtoff', int)]
44+
)
45+
):
46+
def __init__(self, o: TimeTuple, _arg: Any = ...) -> None: ...
47+
else:
48+
class struct_time(
49+
NamedTuple(
50+
'_struct_time',
51+
[('tm_year', int), ('tm_mon', int), ('tm_mday', int),
52+
('tm_hour', int), ('tm_min', int), ('tm_sec', int),
53+
('tm_wday', int), ('tm_yday', int), ('tm_isdst', int)]
54+
)
55+
):
56+
def __init__(self, o: TimeTuple, _arg: Any = ...) -> None: ...
57+
5058

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

0 commit comments

Comments
 (0)