<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Typing on Nalar</title>
    <link>https://nalar.dev/tags/typing/</link>
    <description>Recent content in Typing on Nalar</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 01 Sep 2026 00:00:00 +0700</lastBuildDate>
    <atom:link href="https://nalar.dev/tags/typing/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Python Protocols and Structural Subtyping for Flexible APIs</title>
      <link>https://nalar.dev/python-protocols-and-structural-subtyping/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/python-protocols-and-structural-subtyping/</guid>
      <description>&lt;p&gt;Python code often depends on behavior rather than a specific class hierarchy. A function may only need an object with a &lt;code&gt;send()&lt;/code&gt; method, a &lt;code&gt;read()&lt;/code&gt; method, or a pair of repository operations.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;typing.Protocol&lt;/code&gt; lets type checkers describe those behavioral requirements directly. A class satisfies a protocol by having compatible members; it does not need to inherit from the protocol.&lt;/p&gt;&#xA;&lt;p&gt;This is structural subtyping: &amp;ldquo;if it has the required shape, it can be used here.&amp;rdquo;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Use Python Protocols for Structural Typing at API Boundaries</title>
      <link>https://nalar.dev/python-protocols-structural-typing-api-boundaries/</link>
      <pubDate>Tue, 01 Sep 2026 00:00:00 +0700</pubDate>
      <guid>https://nalar.dev/python-protocols-structural-typing-api-boundaries/</guid>
      <description>&lt;p&gt;Python often relies on duck typing: if an object supports the operation a function needs, its concrete class does not matter. &lt;code&gt;typing.Protocol&lt;/code&gt; gives static type checkers a way to describe that idea explicitly without requiring implementations to inherit from a shared base class.&lt;/p&gt;&#xA;&lt;h2 id=&#34;define-the-behavior-you-consume&#34;&gt;Define the behavior you consume&lt;/h2&gt;&#xA;&lt;div &#xA;    x-data=&#34;{&#xA;        code: $el.querySelector(&#39;code&#39;).innerText&#xA;    }&#34;&#xA;    class=&#34;relative my-4 rounded-lg group&#34;&#xA;&gt;&#xA;    &lt;button&#xA;        @click=&#34;navigator.clipboard.writeText(code); $el.innerText = &#39;Copied!&#39;; setTimeout(() =&gt; $el.innerText = &#39;Copy&#39;, 2000)&#34;&#xA;        class=&#34;absolute top-2 right-2 bg-neutral-700 text-white text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity&#34;&#xA;    &gt;&#xA;        Copy&#xA;    &lt;/button&gt;&#xA;    &#xA;    &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; typing &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; Protocol&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;class&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;ByteWriter&lt;/span&gt;(Protocol):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;write&lt;/span&gt;(self, data: bytes) &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; int: &lt;span style=&#34;color:#f92672&#34;&gt;...&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;emit_header&lt;/span&gt;(writer: ByteWriter) &lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;None&lt;/span&gt;:&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    writer&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(&lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;NLR1&amp;#34;&lt;/span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xA;&lt;/div&gt;&#xA;&lt;p&gt;Any statically compatible object can satisfy &lt;code&gt;ByteWriter&lt;/code&gt;, even if its class never mentions the protocol.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
