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

Protocol with Self #641

Closed
Forceres opened this issue Aug 12, 2024 · 4 comments · Fixed by #642
Closed

Protocol with Self #641

Forceres opened this issue Aug 12, 2024 · 4 comments · Fixed by #642

Comments

@Forceres
Copy link

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
Copy link
Owner

hynek commented Aug 13, 2024

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
Copy link
Author

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
Copy link
Owner

hynek commented Aug 25, 2024

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
Copy link
Author

Forceres commented Aug 25, 2024

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

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

Successfully merging a pull request may close this issue.

2 participants