Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created Sep 23, 2019
Embed
What would you like to do?
import typing as t
import logging
logger = logging.getLogger(__name__)
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: logging.Logger) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
logger = logging.getLogger(__name__)
LoggerType = t.Union[logging.Logger, logging.LoggerAdapter]
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerType) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
import types
from typing_extensions import Protocol
logger = logging.getLogger(__name__)
_SysExcInfoType = t.Union[
t.Tuple[type, BaseException, t.Optional[types.TracebackType]],
t.Tuple[None, None, None],
]
_ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException]
class LoggerProtocol(Protocol):
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerProtocol) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
logger = logging.getLogger(__name__)
LoggerType = t.Union[logging.Logger, logging.LoggerAdapter]
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerType) -> None:
l.info("hello")
if isinstance(l, logging.LoggerAdapter):
use2(l)
def use2(l: logging.LoggerAdapter) -> None:
l.info("hai")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
import types
from typing_extensions import Protocol
logger = logging.getLogger(__name__)
_SysExcInfoType = t.Union[
t.Tuple[type, BaseException, t.Optional[types.TracebackType]],
t.Tuple[None, None, None],
]
_ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException]
class LoggerProtocol(Protocol):
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerProtocol) -> None:
l.info("hello")
if isinstance(l, logging.LoggerAdapter):
use2(l)
def use2(l: logging.LoggerAdapter) -> None:
l.info("hai")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
import types
from typing_extensions import Protocol
logger = logging.getLogger(__name__)
_SysExcInfoType = t.Union[
t.Tuple[type, BaseException, t.Optional[types.TracebackType]],
t.Tuple[None, None, None],
]
_ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException]
class LoggerProtocol(Protocol):
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class LoggerProtocol2(Protocol):
def debug(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerProtocol) -> None:
l.info("hello")
use2(l)
def use2(l: LoggerProtocol2) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
import types
from typing_extensions import Protocol
logger = logging.getLogger(__name__)
_SysExcInfoType = t.Union[
t.Tuple[type, BaseException, t.Optional[types.TracebackType]],
t.Tuple[None, None, None],
]
_ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException]
class LoggerProtocol(Protocol):
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class LoggerProtocol2(Protocol):
def debug(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerProtocol2) -> None:
l.info("hello")
use2(l)
def use2(l: LoggerProtocol) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
import typing as t
import logging
import types
from typing_extensions import Protocol
logger = logging.getLogger(__name__)
LoggerType = t.Union[logging.Logger, logging.LoggerAdapter]
_SysExcInfoType = t.Union[
t.Tuple[type, BaseException, t.Optional[types.TracebackType]],
t.Tuple[None, None, None],
]
_ExcInfoType = t.Union[None, bool, _SysExcInfoType, BaseException]
class LoggerProtocol(Protocol):
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class LoggerProtocol2(Protocol):
def debug(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
def info(
self,
msg: t.Any,
*args: t.Any,
exc_info: _ExcInfoType = ...,
stack_info: bool = ...,
extra: t.Optional[t.Dict[str, t.Any]] = ...,
**kwargs: t.Any,
) -> None:
...
class WLogger(logging.LoggerAdapter):
def process(
self, msg: str, kwargs: t.MutableMapping[str, t.Any]
) -> t.Tuple[str, t.MutableMapping[str, t.Any]]:
return f"Wrap [{msg}]", kwargs
def use(l: LoggerType) -> None:
l.info("hello")
use2(l)
use3(l)
def use2(l: LoggerProtocol) -> None:
l.info("hello")
def use3(l: LoggerProtocol2) -> None:
l.info("hello")
def main() -> None:
logging.basicConfig(
level=logging.INFO, format=logging.BASIC_FORMAT + " (%(funcName)s)"
)
use(logger)
use(WLogger(logger, {}))
if __name__ == "__main__":
main()
00:
python $@*.py
mypy --strict $@*.py
01:
python $@*.py
mypy --strict $@*.py
02:
python $@*.py
mypy --strict $@*.py
03:
python $@*.py
mypy --strict $@*.py
04:
python $@*.py
mypy --strict $@*.py
05:
python $@*.py
mypy --strict $@*.py
06:
python $@*.py
mypy --strict $@*.py
07:
python $@*.py
mypy --strict $@*.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment