Open epicwhale opened 7 months ago
the only workaround I figured so far is:
from loguru import logger
Logger = type(logger)
def process_data(data: str, logger: Logger) -> None:
...
Improvement of your workaround:
(Recommand) Please refer to https://loguru.readthedocs.io/en/stable/api/type_hints.html# :
good shout, the from loguru._logger import Logger
approach seems like the least ugliest of the lot :)
Actually, you should preferably not use from loguru._logger import Logger
. This is an internal type that is not supposed to be exposed publicly. You'll also miss many of the other types listed in the documentation: Type hints.
You're facing ImportError
because the Loguru types being defined in a stub file, they're only accessible to type checkers, not at run-time.
See this answer for more details: https://github.com/Delgan/loguru/issues/1101#issuecomment-2027991074
The following solution should work fine:
import loguru
def my_function(a, ctx_logger: "loguru.Logger"):
ctx_logger.debug(...)
Alternatively:
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from loguru import Logger
def my_function(a, ctx_logger: Logger):
ctx_logger.debug(...)
Wondering, how do you typehint a logger being passed around in code?
I tried using
:Logger
by importingfrom loguru import Logger
but it gives me a "ImportError: cannot import name 'Logger' from 'loguru'" errorCouldn't find a clear answer here either: https://stackoverflow.com/questions/66411218/type-hint-for-returning-loguru-logger