question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Something error in loguru stub file

See original GitHub issue

loguru.Logger is imported as _Logger, Logger class cannot accessed from loguru top level module actually.

This will cause static type checking failed or ImportError like "loguru.Logger" is incompatible with "loguru._logger.Logger"

https://github.com/Delgan/loguru/blob/5e754dd4239f4703eec007c4efaefbbfe272132b/loguru/__init__.pyi#L226

https://github.com/Delgan/loguru/blob/5e754dd4239f4703eec007c4efaefbbfe272132b/loguru/__init__.py#L11

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yanyongyucommented, Jul 29, 2021

Add safe_getattr when AttributeError to get_type_hints could be fine. I will make a pull request to sphinx and close this later.

1reaction
yanyongyucommented, Jul 28, 2021

About Sphinx autodoc:

autodoc do not check the annotation in module level function: https://github.com/sphinx-doc/sphinx/blob/f59026865a8c658acbc6d5690c146f4be0851854/sphinx/ext/autodoc/__init__.py#L1313-L1318

but checks the annotation for module level data using typing.get_type_hints: https://github.com/sphinx-doc/sphinx/blob/f59026865a8c658acbc6d5690c146f4be0851854/sphinx/ext/autodoc/__init__.py#L1981-L2005

and i find a monkey patch:

Solution

from typing import TYPE_CHECKING

import loguru

if TYPE_CHECKING:
	from loguru import Logger

my_logger: "Logger" = loguru.logger

both static type checking and sphinx autodoc report 0 errors!

Explain:

  • sphinx handled NameError caused by eval ForwardRef
  • "loguru.Logger" not work and raise AttributeError instead of NameError when eval
  • giving a nonexist annotation value will be fine
  • using typing.TYPE_CHECKING to keep the static type checking working
Read more comments on GitHub >

github_iconTop Results From Across the Web

loguru Documentation - Read the Docs
Have you ever seen your program crashing unexpectedly without seeing anything in the log file? Did you ever notice that.
Read more >
Logger - loguru documentation - Read the Docs
The Logger is the core object of loguru , every logging configuration and usage ... Anything with a .write() method is considered as...
Read more >
Python Logging: A Stroll Through the Source Code
In this step-by-step tutorial, you'll learn about how the Python logging package is designed from an OOP perspective. You'll walk line by line...
Read more >
How can I patch / mock logging.getlogger() - Stack Overflow
You can use patch.object() on the actual logging object. that lets you verify that you're using the correct logger too: logger = logging....
Read more >
pymongo.errors.ServerSelectionTimeoutError with atlas even ...
Hello, I have been using pymongo with atlas for a while now, and suddenly around two hours ago, I must have done something...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found