Logger.info这样的Logger函数的类型是什么?

huangapple go评论53阅读模式
英文:

What is the type of a Logger function like Logger.info?

问题

如何为类似Logger.info的Logger函数添加类型注释?使用reveal_type返回的结果如下:

Revealed type is "def (msg: builtins.object, *args: builtins.object, *, exc_info: Union[None, builtins.bool, Tuple[Type[builtins.BaseException], builtins.BaseException, Union[types.TracebackType, None]], Tuple[None, None, None], builtins.BaseException] =, stack_info: builtins.bool =, stacklevel: builtins.int =, extra: Union[typing.Mapping[builtins.str, builtins.object], None] =)"。

一定有更简洁的类型注释,如何找到它呢?

英文:

How do I type annotate a Logger function like Logger.info? Using reveal_type returns this:

Revealed type is "def (msg: builtins.object, *args: builtins.object, *, exc_info: Union[None, builtins.bool, Tuple[Type[builtins.BaseException], builtins.BaseException, Union[types.TracebackType, None]], Tuple[None, None, None], builtins.BaseException] =, stack_info: builtins.bool =, stacklevel: builtins.int =, extra: Union[typing.Mapping[builtins.str, builtins.object], None] =)"

There must be a more concise type for this, how do I go about finding it?

答案1

得分: 1

以下是已翻译的内容:

"Revealing the type shows you the exact signature of the function. The most accurate annotation would be using a custom tailored callable protocol reflecting that exact function signature."

"这会显示函数的确切签名。最准确的注释是使用自定义的可调用 协议,反映该确切的函数签名。"

"To shamelessly steal from typeshed:"

"为了无耻地从 typeshed 偷取:"

"This should be compatible with Python 3.8+."

"这应该与 Python 3.8+ 兼容。"

"To contrast this with the simplest possible annotation based on collections.abc.Callable, the following of course works too."

"与基于 collections.abc.Callable 的最简单的可能注释相对比,以下当然也可以工作。"

"And there are options in between. Depending on what you want this for."

"之间还有一些选项,具体取决于你想要什么。"

英文:

It depends on how precise you want/need it.

Revealing the type shows you the exact signature of the function. The most accurate annotation would be using a custom tailored callable protocol reflecting that exact function signature.

To shamelessly steal from typeshed:

from collections.abc import Mapping
from logging import getLogger
from types import TracebackType
from typing import Optional, Protocol, Union
from typing_extensions import TypeAlias


_SysExcInfoType: TypeAlias = Union[
    tuple[type[BaseException], BaseException, Optional[TracebackType]],
    tuple[None, None, None],
]
_ExcInfoType: TypeAlias = Union[None, bool, _SysExcInfoType, BaseException]


class LogMethod(Protocol):
    def __call__(
        self,
        msg: object,
        *args: object,
        exc_info: _ExcInfoType = None,
        stack_info: bool = False,
        stacklevel: int = 1,
        extra: Optional[Mapping[str, object]] = None,
    ) -> None:
        ...


log = getLogger(__name__)

x: LogMethod = log.info

This should be compatible with Python 3.8+.


To contrast this with the simplest possible annotation based on collections.abc.Callable, the following of course works too.

from collections.abc import Callable


log = getLogger(__name__)

y: Callable[..., None] = log.info

And there are options in between. Depending on what you want this for.

huangapple
  • 本文由 发表于 2023年2月8日 17:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定