hynek / structlog

Simple, powerful, and fast logging for Python.
https://www.structlog.org/
Other
3.48k stars 220 forks source link

Protocol with Self #641

Closed Forceres closed 1 month ago

Forceres commented 2 months ago

Is here a way to implement bind method in protocol without static analyzer complains?


class LoggerProtocol(Protocol):
    def bind(self, **new_values: Any) -> Self: ...

    def log(
            self,
            level: int,
            event: str or None = None,
            *args: Any,
            **kwargs: Any
    ) -> None: ...

    def info(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    def debug(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    def warning(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    def error(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    def exception(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    async def ainfo(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    async def adebug(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    async def awarning(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    async def aerror(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...

    async def aexception(self, event: str | None = None, *args: Any, **kw: Any) -> None: ...
   logger: BoundLogger = wrap_logger(None, processors=structlog_config.processors,
                                      wrapper_class=structlog_config.wrapper_class,
                                      cache_logger_on_first_use=structlog_config.cache_logger_on_first_use)

   def test(logger: LoggerProtocol):
          logger.info("test")

runtime_checkable works fine, and is_instance method too, but IDE points at incorrect type

hynek commented 2 months ago

yeah BindableLogger should probably return Self everywhere. It seems like the Protocol was added in 2020 which predates Self – at least in Python which grew it in 3.11.

I guess this means a conditional dependency on typing-extensions.

Forceres commented 2 months ago

yeah BindableLogger should probably return Self everywhere. It seems like the Protocol was added in 2020 which predates Self – at least in Python which grew it in 3.11.

I guess this means a conditional dependency on typing-extensions.

I use Self, but IDE complains on bind method

hynek commented 1 month ago

yeah BindableLogger should probably return Self everywhere. It seems like the Protocol was added in 2020 which predates Self – at least in Python which grew it in 3.11. I guess this means a conditional dependency on typing-extensions.

I use Self, but IDE complains on bind method

I don't understand this response as a reply to what I wrote, or why you closed the issue, but there's now #642 now open to fix this.

Forceres commented 1 month ago

yeah BindableLogger should probably return Self everywhere. It seems like the Protocol was added in 2020 which predates Self – at least in Python which grew it in 3.11. I guess this means a conditional dependency on typing-extensions.

I use Self, but IDE complains on bind method

I don't understand this response as a reply to what I wrote, or why you closed the issue, but there's now #642 now open to fix this.

It was misunderstanding between us, I meant that I used Self as a type hint and it should be ok for any type checkers, but from the first your reply I didn't get that it was a problem on structlog side. I temporarily ended with implementing extra protocol for no type warnings