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_
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
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
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_
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
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