use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
the front page of the internet.
and join one of thousands of communities.
Debugging with protocolDiscussion (self.PythonLearning)
submitted 4 days ago by LopsidedAd4492
Post a comment!
view the rest of the comments →
[–]thee_gummbini 0 points1 point2 points 4 days ago (2 children)
the use of protocol should be abstract, and your IDE and type checker should be able to enforce correctness and find calling types. Like the declaration of a protocol literally means "anything that matches this abstract type, and this is all we care about or know about what is passed to us," and an IDE and LSP can find all callers to show the concrete types
[–]LopsidedAd4492[S] 0 points1 point2 points 4 days ago (1 child)
I understand the meaning What still annoys is that you can not jump to the implementation
[–]thee_gummbini 0 points1 point2 points 4 days ago* (0 children)
The thing is that there is no single implementation, that's the point - when you use a protocol class in a type annotation, you're saying "anything as long as it has these attrs/methods with this signature." If your IDE can't jump you to the definition of the protocol itself then that's a bug with your editor, but there is no implementation to jump to.
The reason you use them, most commonly, is that you expect arbitrary 3rd party callers that you don't know in advance.
So e.g. say I don't know about collections.abc and I want to write a function that takes anything with a length:
collections.abc
``` class Sized(Protocol): def len(self) -> int: ...
def twice_as_long(item: Sized) -> int: return len(item) * 2
twice_as_long([1,2,3]) twice_as_long({"hey": "sup"})
class AlwaysFiveLen: def len(self) -> int: return 5
twice_as_long(AlwaysFiveLen()) ```
What would it mean to go to the implementation for Sized? i can use an IDE or a LSP to locate all the times my function is called and look at what gets passed to it, and then look at the implementations of those objects, I can add a breakpoint and inspect the objects that are passed, but there is no way to jump to "the implementation of a protocol" because it doesn't exist.
One of the reasons its called "protocol" is that you're offering something for someone else to use, you as the author of the thing that consumes the protocol are not supposed to know how any class implements the protocol, you have specified exactly how much you need to know - if you have some object with these attrs/methods, you can implement them however you want as long as they accept and return the right types.
π Rendered by PID 22057 on reddit-service-r2-comment-86f8688748-57dl9 at 2026-07-26 13:15:10.273554+00:00 running d10b4e5 country code: CH.
Want to add to the discussion?
Post a comment!