u/CodeCarnival-666

▲ 0 r/Python

Writing the typing.Protocol before the class that satisfies it

When I pull an implementation out from behind a pile of call sites, I now write the typing.Protocol first and the class that satisfies it second. Define the seam, annotate the call sites against it, run mypy, and every place the current shape is wrong shows up before the new code exists. If the concrete class then inherits the protocol explicitly, mypy checks the implementation against it at the class definition, not only where it gets passed.

This feels Python-specific because duck typing usually leaves nothing to review. The implicit interface is whatever the callers happen to touch, spread over however many files. Writing it down turns it into something a colleague can read and disagree with before the work happens.

None of it is enforced at runtime. PEP 544 imposes no runtime semantics on protocol annotations, and even with u/runtime_checkable the typing docs say isinstance only checks that the named attributes exist, not their signatures.

It is cheap to try on one seam. The plan step in verdent works the same way, clarifying questions first and a plan you approve before any code is written. Nothing forces the protocol to change when the implementation does, so the two drift. Curious whether people keep the protocol next to the consumer or next to the implementation.

reddit.com
u/CodeCarnival-666 — 3 days ago

The funding rate in my screenshot did not match my account history

I keep screenshots when I compare funding across perpetual contracts. One of them showed a rate that did not match the debit in my account history, and for a while I thought I had copied the wrong row. Then I noticed the countdown in the corner. I had taken the screenshot before settlement, so the rate on the screen was still an estimate.

A BYDFi notice about MOVE and BANANA changing from an eight hour interval to four hours cleared up another part of the mismatch. Two rates can look similar, but the shorter interval creates twice as many settlement times in a day. I now leave the contract name, countdown and interval in the image instead of cropping around the percentage. After settlement I compare the account entry with the position value and the settled rate. My entry price and posted margin are not part of that calculation.

reddit.com
u/CodeCarnival-666 — 8 days ago