Type hint for returning loguru logger

Question:

Does anyone know the proper type hint for returning a loguru logger? 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'

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_

Asked By: Kurt Kline

||

Answers:

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.

This comes from the loguru type hint documentation

Answered By: Kurt Kline

I tried the answer above answer but failed, well I solved it by

from typing import TYPE_CHECKING


if TYPE_CHECKING:
    from loguru import Logger
Answered By: Давид Шико
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.