简体   繁体   English

返回 loguru 记录器的类型提示

[英]Type hint for returning loguru logger

Does anyone know the proper type hint for returning a loguru logger?有谁知道返回 loguru 记录器的正确类型提示? Using loguru.Logger passes mypy checks, but when the function get_logger is called I get the error AttributeError: module 'loguru' has no attribute 'Logger'使用loguru.Logger通过 mypy 检查,但是当调用 function get_logger时,我收到错误AttributeError: module 'loguru' has no attribute 'Logger'

import copy
from pathlib import Path
from sys import stdout

from loguru import logger
import loguru


def get_logger(log_path: Path) -> loguru.Logger:
    logger.remove()
    logger_ = copy.deepcopy(logger)
    logger_.add(stdout)
    logger_.add(f"{log_path}.log")

    return logger_

I learned that loguru.Logger is correct and can be used without throwing an error if from __future__ import annotations is added to the list of imports.我了解到loguru.Logger是正确的,如果from __future__ import annotations添加到导入列表中,则可以使用而不会引发错误。

This comes from the loguru type hint documentation这来自loguru 类型提示文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM