r/Python • • 17m ago

Discussion Why AI agents are so eager to use Protocol?

I hate Protocol and whenever AI agents write code they choose it over having normal abstract class with abstract methods. So whenever I see it now, I jut get triggered and think this code is probably slop. I know why Protocol could be useful, but agents constantly misuse it.

0 Upvotes

18 comments sorted by

•

u/CzyDePL 13m ago edited 10m ago

Because it's a superior solution in most of the cases, abstract classes are conflating typing, code sharing and enforcing the implementation of a method into single concept, while in fact it might call for 3 different shapes.

https://hynek.me/articles/python-subclassing-redux/ that's a pretty good summary where inheritance and by extension ABCs make sense and where they don't

•

u/UltramanQuar 12m ago

it is ass

•

u/CzyDePL 1m ago

Do you use type checker? I am writing strictly (honestly as strictly as I can, compromising on flexibility and not on strictness if it explains my approach) typed code (at least for the core, not infra, part) and maybe that's the source of the difference. Without it protocols don't give you anything, so ABCs would be preferable

•

u/TMiguelT 13m ago

In my book it's preferable to abstract classes in that it's checked statically (no runtime performance cost) and supports implicit implementation. 

•

u/Quantum640 4m ago

Protocols and normal inheritance are both checked statically by modern type checkers. There is technically an extremely small runtime cost for a real superclass because it's involved in MRO generation, but this is trivial.

ABCs in particular have some bad runtime performance characteristics due to their virtual subclassing feature, but this doesn't need to be used. Runtime checkable protocols (which must be explicitly enabled) also have additional cost beyond a normal superclass for is instance checks.

Aside from the performance mixed bag, Python typically prefers explicit over implicit, and Protocols violate this principle when it comes to implementing interfaces. Runtime checkable protocols only check that the object has methods with the same names as the protocol, disregarding signature completely. When interface implementation is nominal and explicit via superclassing, type checkers can validate that each implementation adheres to the specified contracts and you'll end up with much less unexpected behavior.

•

u/UltramanQuar 3m ago

thank you bro, people here are crazy

•

u/UltramanQuar 9m ago

I guess I am one of older devs that still sometimes tinker with code manually, when you rename method it doesn't catch it because it is not linked properly. Also changing contracts are not properly reflected as well. Sucks ass

•

u/CzyDePL 7m ago

You can inherit from protocol to help LSPs navigate. But I can agree that most LSPs by default can not be helpful enough to trace all usages of protocol and all their implementations

•

u/UltramanQuar 3m ago

inherit from protocol, and now we have not so abstract class

•

u/DreamingElectrons 11m ago

Every time something news is introduced you get lots of blog posts and online comments gushing over how much better an greater the new feature is with everything else being old and boring. LLMs just absorb all of this as facts during their training, but since there is no real intelligence in an LLM, they simply don't get that it's a recurring pattern of overhyping things. In other words: AI is really susceptible to falling for clickbait.

You could blacklist protocols and just tell your AI agent to just not use the pattern or even better, not bother with agentic coding and write the code yourself.

•

u/tardmrr 12m ago

OOP Considered Harmful

•

u/CzyDePL 6m ago

In my book OOP doesn't mean inheritance, and FP means you can still screw your type hierarchies :)

•

u/PrestigiousStrike779 11m ago

What’s wrong with Protocol? It’s the right design in some cases, when there’s no expected implementation, and it’s more flexible.  The only thing I don’t like about it is there’s no good way to signal that you’re implementing the protocol.

•

u/UltramanQuar 7m ago

I guess I am one of older devs that still sometimes tinker with code manually, when you rename method it doesn't catch it because it is not linked properly. Also changing contracts are not properly reflected as well. Sucks ass

•

u/Fine_League311 13m ago

Why a dev read logs, why a dev write comments in code, why and dev write an roadmap and an changelog? cause its his Protocol!

Teach your AI with resricts not with overpromting, he is smarter than the most ones.

•

u/ProbsNotManBearPig 10m ago

I’m a professional software engineer for 15 years. I’m not saying I blindly trust ai, but I distrust humans more than I distrust ai at this point, so I’m skeptical of your opinion if it’s constantly disagreeing with Opus 5.5 or Astra.

Please give concrete examples where it’s choosing Protocol and you think it’s a bad choice, and explain why you think it’s a bad choice.

•

u/UltramanQuar 4m ago

recent example, had it create create flexible memory bus with 2 implementations, 1 in memory for quick testing, 1 with nats, it chose protocol over simple abstract class and stuff like that keeps happening.

•

u/m02ph3u5 12m ago

I specifically have to instruct it to use protocol, override, and runtime-checkable. Otherwise they're gonne abc and duck-type yolo like crazy.

Also, mind your existing code base - it's context, too. If they see protocol they are likely to use protocol.