Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import loguru.Writable #130

Closed
phillipuniverse opened this issue Oct 1, 2021 · 6 comments
Closed

Cannot import loguru.Writable #130

phillipuniverse opened this issue Oct 1, 2021 · 6 comments

Comments

@phillipuniverse
Copy link

I'm trying to write a simplified type hint for what defines a sink like this:

LoguruSink = Union[TextIO, loguru.Writable, Callable[[loguru.Message], None], logging.Handler]

When I do this, I get the following error:

  File "...myproj/config/logging.py", line 109, in <module>
    LoguruSink = Union[TextIO, loguru.Writable, Callable[[loguru.Message], None], logging.Handler]
AttributeError: module 'loguru' has no attribute 'Writable'

I am doing this because I have a dynamic expression like this to define a sink:

def some_other_sink(message: loguru.Message) -> None:
    ...

default_sink = sys.stdout if settings.is_local_dev else some_other_sink

logger.add(
    default_sink, level=settings.log_level.upper(), format=log_fmt
)

When I lint this with Mypy though, it does not quite see the correct type of default_sink, admittedly a bit of a mypy limitation IMO:

myproj/config/logging.py:129: error: No overload variant of "add" of "Logger"
matches argument types "object", "str", "str", "Dict[str, str]"  [call-overload]
        logger.add(
        ^
myproj/config/logging.py:129: note: Possible overload variants:
myproj/config/logging.py:129: note:     def add(self, sink: Union[TextIO, Writable, Callable[[Message], None], Handler], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., catch: bool = ...) -> int
myproj/config/logging.py:129: note:     def add(self, sink: Callable[[Message], Awaitable[None]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., catch: bool = ..., loop: Optional[AbstractEventLoop] = ...) -> int
myproj/config/logging.py:129: note:     def add(self, sink: Union[str, _PathLike[str]], *, level: Union[str, int] = ..., format: Union[str, Callable[[Record], str]] = ..., filter: Union[str, Callable[[Record], bool], Dict[Optional[str], Union[str, int, bool]], None] = ..., colorize: Optional[bool] = ..., serialize: bool = ..., backtrace: bool = ..., diagnose: bool = ..., enqueue: bool = ..., catch: bool = ..., rotation: Union[str, int, time, timedelta, Callable[[Message, TextIO], bool], None] = ..., retention: Union[str, int, timedelta, Callable[[List[str]], None], None] = ..., compression: Union[str, Callable[[str], None], None] = ..., delay: bool = ..., mode: str = ..., buffering: int = ..., encoding: str = ..., **kwargs: Any) -> int
Found 1 error in 1 file (checked 122 source files)
make: *** [lint] Error 1

But if I type hint default_sink like this, mypy is happy with no errors:

def some_other_sink(message: loguru.Message) -> None:
    ...

default_sink: default_sink: Union[TextIO, Callable[[loguru.Message], None]] = sys.stdout if settings.is_local_dev else some_other_sink

logger.add(
    default_sink, level=settings.log_level.upper(), format=log_fmt
)

Buuuut.... my completionist brain is not happy. So 2 questions:

  1. Is there a reason that loguru.Writable is not exposed?
  2. If you want to keep loguru.Writable un-importable, would you consider exposing a new Sink type that I could use like loguru.Sink?

FWIW I am already using loguru-mypy.

Thanks for the great library! I have dug in pretty deep to it and appreciate the very intentional design decisions that have been made.

@kornicameister
Copy link
Owner

@phillipuniverse I don't quite this issue belongs to this repository. loguru does in fact define typings but loguru-mypy is a plugin that tries to validate some runtime intricacies.

I suggest submitting this issue to main repository. I am sure @Delgan will do far better work trying to help you.

@phillipuniverse
Copy link
Author

Sorry about this! For some reason I thought I was in that repo!

I'm re-opening there, thanks!

@phillipuniverse
Copy link
Author

phillipuniverse commented Oct 1, 2021

New issue at Delgan/loguru#515.

@kornicameister
Copy link
Owner

@phillipuniverse not at all ;-) if there was a misunderstanding you're free to submit a proposal how to improve README.md to make it a bit more obvious.

@phillipuniverse
Copy link
Author

if there was a misunderstanding you're free to submit a proposal

Naw we're good, the only proposal would be "make sure that @phillipuniverse's browser is pointing to the repository he intended before he opens an issue" and that doesn't feel like it fits in a README ;)

@kornicameister
Copy link
Owner

LOL cheers bro ;) have fun ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants