<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Crumpled Paper]]></title><description><![CDATA[All opinions are my own—and correct.]]></description><link>https://blog.jphutchins.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1729144623521/ed36e9f9-340c-419a-a5d3-5d950812cc8b.png</url><title>Crumpled Paper</title><link>https://blog.jphutchins.com</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 09:23:06 GMT</lastBuildDate><atom:link href="https://blog.jphutchins.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Enum Type Safety in C]]></title><description><![CDATA[The C standard allows the programmer to define new types, including enumerated types—or "enums"—that improve program readability and type safety. This article explores the specification for enumerated types, the compiler options that improve enum typ...]]></description><link>https://blog.jphutchins.com/enum-type-safety-in-c</link><guid isPermaLink="true">https://blog.jphutchins.com/enum-type-safety-in-c</guid><category><![CDATA[C]]></category><category><![CDATA[embedded]]></category><category><![CDATA[RTOS]]></category><category><![CDATA[TypeSafety]]></category><category><![CDATA[C++]]></category><category><![CDATA[gcc compiler]]></category><category><![CDATA[Clang]]></category><category><![CDATA[compiler]]></category><category><![CDATA[enum]]></category><dc:creator><![CDATA[JP Hutchins]]></dc:creator><pubDate>Tue, 17 Dec 2024 06:49:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1734415094416/e28cdf40-86b3-4a08-8aab-051473dd3faf.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The C standard allows the programmer to define new types, including enumerated types—or "enums"—that improve program readability and type safety. This article explores the specification for enumerated types, the compiler options that improve enum type safety, and why type safety prevents run time errors. The focus is on the GCC and Clang C compilers targeting ARM32 (e.g. Cortex-M MCUs), but the same conclusions should apply to all C targets, including RISCV, x86_64, and ARM64.</p>
<p><strong>Table of Contents</strong></p>
<p>The first section is an optional review of what enums are and how they work in C.</p>
<ul>
<li><a class="post-section-overview" href="#c-enums">C Enums</a>: Review of C's enumerated types features.<ul>
<li><a class="post-section-overview" href="#the-c-standard">The C Standard</a>: Read this section if you are not convinced that C enums are types.</li>
<li><a class="post-section-overview" href="#fixed-underlying-type">Fixed Underlying Type</a>: How to specify the underlying integer type of an enumerated type.</li>
</ul>
</li>
</ul>
<p>The next section investigates the compiler options that provide compile time type safety and prevent run time errors.</p>
<ul>
<li><a class="post-section-overview" href="#type-safety">Type Safety</a><ul>
<li><a class="post-section-overview" href="#exhaustiveness">Exhaustiveness</a>: Options to ensure that all enumerations are handled by a <code>switch</code>.</li>
<li><a class="post-section-overview" href="#type-checking">Type Checking</a>: Options to ensure that function parameters are type checked.</li>
</ul>
</li>
</ul>
<h1 id="heading-c-enums">C Enums</h1>
<p>C enums enable the definition of new types that are collections of symbols. Each symbol is mapped to an integer value. The integer value may be arbitrary or it may be a useful part of the representation.</p>
<p>For example, an enumeration of <strong>colors</strong> might look like this:</p>
<pre><code class="lang-c"><span class="hljs-keyword">enum</span> color {
    COLOR_RED = <span class="hljs-number">0</span>,
    COLOR_GREEN = <span class="hljs-number">1</span>,
    COLOR_BLUE = <span class="hljs-number">2</span>,
};
</code></pre>
<p>The value of 0 being given to red, 1 to green, and 2 to blue is <em>arbitrary</em>—the values used could be any set of three numbers. Because the use of unique integers with arbitrary values is so common, an enum member definition that does not include the value will automatically be assigned the preceding value plus one:</p>
<pre><code class="lang-c"><span class="hljs-keyword">enum</span> color {
    COLOR_RED = <span class="hljs-number">0</span>,
    COLOR_GREEN,  <span class="hljs-comment">// automatically 1</span>
    COLOR_BLUE,  <span class="hljs-comment">// automatically 2</span>
};
</code></pre>
<p>This is simpler, and in the case where new symbols may appear anywhere in the definition, it is <em>preferred</em>, because it will prevent accidental creation of shared values when unique values are required.</p>
<p>On the other hand, an enumeration of <strong>speed limits</strong> may map to constant integer values that are useful and not-at-all arbitrary:</p>
<pre><code class="lang-c"><span class="hljs-keyword">enum</span> speed_limit {
    SPEED_LIMIT_30_KPH = <span class="hljs-number">30</span>,
    SPEED_LIMIT_60_KPH = <span class="hljs-number">60</span>
    SPEED_LIMIT_90_KPH = <span class="hljs-number">90</span>,
};
</code></pre>
<p>Lastly, the enumerated values do not need to be unique. Multiple symbols can map to the same literal integer, though the intent is often more clear by mapping to the symbol rather than the literal integer:</p>
<pre><code class="lang-c"><span class="hljs-keyword">enum</span> font {
    FONT_TIMES_NEW_ROMAN = <span class="hljs-number">0</span>,
    FONT_HELVETICA,
    FONT_DEFAULT = FONT_HELVETICA,  <span class="hljs-comment">// instead of 1</span>
};
</code></pre>
<p><em>All of the examples "namespace" the enum by prefixing members with the name of the type—the name of the collection—as is common practice in C because the type itself cannot be used as the namespace. That is, it's not possible to use <code>enum color::RED</code>. Don't dismay! More sophisticated strategies to enforce type safety are the subject of this very article.</em></p>
<h2 id="heading-the-c-standard">The C Standard</h2>
<p>The C standard states that enumerated types (enums) that are defined by the programmer are unique types.</p>
<p>In the draft version (<a target="_blank" href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf">ISO/IEC 9899:2024 (en) — N3220</a>) of C23, we find a definition for C enumerations under section <strong>6.2.5 Types</strong> (p. 40):</p>
<blockquote>
<p>An enumeration comprises a set of named integer constant values. Each distinct enumeration
constitutes a different enumerated type.</p>
</blockquote>
<p>It's important to note that C23 enables specifying the underlying type of the enumeration, which is a <a target="_blank" href="https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n3030---enhanced-enumerations">very good idea</a>. From section <strong>6․4․4․4 Enumeration constants</strong> (p. 63):</p>
<blockquote>
<p>An identifier declared as an enumeration constant for an enumeration without a fixed underlying
type has either type int or the enumerated type, as defined in 6․7.3.3. An identifier declared
as an enumeration constant for an enumeration with a fixed underlying type has the associated
enumerated type.</p>
</blockquote>
<p>And the definition of "enumerated type" from section <strong>6․7.3.3 Enumeration specifiers </strong> (p. 109):</p>
<blockquote>
<p>2 All enumerations have an underlying type. The underlying type can be explicitly specified using an
enum type specifier and is its fixed underlying type. If it is not explicitly specified, the underlying
type is the enumeration’s compatible type, which is either char or a standard or extended signed or
unsigned integer type.</p>
</blockquote>
<p>So actually, it's probably <code>int</code>, or maybe not, because it's <em>implementation-defined</em> (i.e. up to the authors of GCC, Clang, etc.):</p>
<blockquote>
<p>13 For all enumerations without a fixed underlying type, each enumerated type shall be compatible
with char or a signed or an unsigned integer type that is not bool or a bit-precise integer type. The
choice of type is implementation-defined but shall be capable of representing the values of all
the members of the enumeration.</p>
</blockquote>
<p>The behavior is muddied further with compiler options like <code>-fshort-enums</code> that will use the smallest possible integer representation for the enumeration's underlying type.</p>
<h2 id="heading-fixed-underlying-type">Fixed Underlying Type</h2>
<p>Fortunately, there is no (good) reason to use <code>-fshort-enums</code> since C23 allows specifying a fixed underlying type for each enumerated type definition:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdint.h&gt;</span></span>

<span class="hljs-keyword">enum</span> eight_bit : <span class="hljs-keyword">uint8_t</span> {
    HEX00 = <span class="hljs-number">0x00</span>,
    HEX01 = <span class="hljs-number">0x01</span>,
};
</code></pre>
<p>And, because it's in the C standard, compilers must generate an error if one of the enumeration constants doesn't fit in the fixed underlying type:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdint.h&gt;</span></span>

<span class="hljs-keyword">enum</span> eight_bit : <span class="hljs-keyword">uint8_t</span> {
    HEX100 = <span class="hljs-number">0x100</span>
};
</code></pre>
<p>GCC 14.2.0 (unknown-eabi):</p>
<pre><code class="lang-txt">&lt;source&gt;:4:14: error: enumerator value outside the range of underlying type
    8 |     HEX100 = 0x100,
      |              ^~~~~
Compiler returned: 1
</code></pre>
<h1 id="heading-type-safety">Type Safety</h1>
<p>Because C enums are types, they should afford some degree of type safety. Type safety provides compile time assurances that improve program expressiveness, correctness, size, and performance. However, compilers do not provide type safety for C enums by default.</p>
<p>The following examples were tested with two compilers: <code>ARM GCC 14.2.0 (unknown-eabi)</code> and <code>armv7-a clang 18.1.0</code>. The compiler and program output is the same between the two compilers unless otherwise noted.</p>
<h2 id="heading-exhaustiveness">Exhaustiveness</h2>
<p>Generally, when an enum is used in a <code>switch</code> statement, the intention is that all constants of the enumerated type are matched by a <code>case</code>. If that's not the intention, then it's likely that the enum type should be constrained to a more narrow collection of constants. When the matching handles every possible case, it is said to be <em>exhaustive</em>.</p>
<p>Here's an example with a runtime error lurking—because the <code>switch</code> is not exhaustive, it is possible to hit the <code>assert(0)</code> after the <code>switch</code>!</p>
<p><a target="_blank" href="https://godbolt.org/z/xd6W7ME6d">Interact with this example on Compiler Explorer</a></p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;assert.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdint.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-keyword">enum</span> event : <span class="hljs-keyword">uint8_t</span> {
    EVENT_A = <span class="hljs-number">0</span>,
    EVENT_B,
};

<span class="hljs-keyword">enum</span> result : <span class="hljs-keyword">uint8_t</span> {
    RESULT_A = <span class="hljs-number">0</span>,
    RESULT_B,
};

<span class="hljs-function"><span class="hljs-keyword">enum</span> result <span class="hljs-title">handle_event</span><span class="hljs-params">(<span class="hljs-keyword">enum</span> event event)</span> </span>{
    <span class="hljs-keyword">switch</span> (event) {
        <span class="hljs-keyword">case</span> EVENT_A:
            <span class="hljs-keyword">return</span> RESULT_A;
    }
    assert(<span class="hljs-number">0</span>);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">enum</span> result result = handle_event(<span class="hljs-number">0</span>);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, result);
}
</code></pre>
<p>Compiler options: <code>-Werror</code>:</p>
<pre><code class="lang-txt">Compiler returned: 0
</code></pre>
<p>Program output:</p>
<pre><code class="lang-txt">Program returned: 0
Program stdout
0
</code></pre>
<p>The <code>handle_event()</code> function implies that it can handle all members of <code>enum event</code>. In reality, the programmer has failed to match the <code>EVENT_B</code> case, so passing <code>EVENT_B</code>, or 1, as the function argument will cause a runtime error:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">enum</span> result result = handle_event(<span class="hljs-number">1</span>);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, result);
}
</code></pre>
<p>Compiler options: <code>-Werror</code>:</p>
<pre><code class="lang-txt">Compiler returned: 0
</code></pre>
<p>Program output:</p>
<pre><code class="lang-txt">Program returned: 139
Program stderr
output.s: /app/example.c:20: handle_event: Assertion `0' failed.
Program terminated with signal: SIGSEGV
</code></pre>
<p>Fortunately, simply adding <a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall"><code>-Wall</code></a> covers this common mistake (<code>-Wextra</code> is added for completeness).</p>
<p>GCC <code>-Werror -Wall -Wextra</code>:</p>
<pre><code class="lang-txt">&lt;source&gt;: In function 'handle_event':
&lt;source&gt;:16:5: error: enumeration value 'EVENT_B' not handled in switch [-Werror=switch]
   16 |     switch (event) {
      |     ^~~~~~
cc1: all warnings being treated as errors
Compiler returned: 1
</code></pre>
<p>Clang <code>-Werror -Wall -Wextra</code>:</p>
<pre><code class="lang-txt">&lt;source&gt;:16:13: error: enumeration value 'EVENT_B' not handled in switch [-Werror,-Wswitch]
   16 |     switch (event) {
      |             ^~~~~
1 error generated.
Compiler returned: 1
</code></pre>
<p>Importantly, we can reintroduce this <strong>runtime bug by adding a <code>default:</code> case</strong>:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">enum</span> result <span class="hljs-title">handle_event</span><span class="hljs-params">(<span class="hljs-keyword">enum</span> event event)</span> </span>{
    <span class="hljs-keyword">switch</span> (event) {
        <span class="hljs-keyword">case</span> EVENT_A:
            <span class="hljs-keyword">return</span> RESULT_A;
        <span class="hljs-keyword">default</span>:
            assert(<span class="hljs-number">0</span>);
    }
}
</code></pre>
<p>Compiler options <code>-Werror -Wall -Wextra</code>:</p>
<pre><code class="lang-txt">Compiler returned: 0
</code></pre>
<p>The conclusion is that the use of a <code>default:</code> case in the <code>switch</code> statement is counterproductive to type safety.</p>
<h2 id="heading-type-checking">Type Checking</h2>
<p>By adding <code>-Wall</code> and avoiding use of a <code>default:</code> case, the <code>switch</code> statement over the <code>enum event</code> is <em>exhaustive</em>, therefore it is impossible for a runtime error to occur when the argument given to <code>handle_event()</code> is guaranteed to be an <code>enum event</code>. However, because  the GCC and Clang compilers do not strictly check the type given to <code>handle_event()</code>, the program remains vulnerable to other run time errors.</p>
<p>In this example, <code>2</code> is passed to <code>handle_event()</code> without generating a compile time error. At run time, it is found that <code>2</code> is not handled by the <code>switch</code>, causing the assertion to be hit.</p>
<p><a target="_blank" href="https://godbolt.org/z/5KMTrjeaM">Interact with this example on Compiler Explorer</a></p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;assert.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdint.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-keyword">enum</span> event : <span class="hljs-keyword">uint8_t</span> {
    EVENT_A = <span class="hljs-number">0</span>,
    EVENT_B,
};

<span class="hljs-keyword">enum</span> result : <span class="hljs-keyword">uint8_t</span> {
    RESULT_A = <span class="hljs-number">0</span>,
    RESULT_B,
};

<span class="hljs-function"><span class="hljs-keyword">enum</span> result <span class="hljs-title">handle_event</span><span class="hljs-params">(<span class="hljs-keyword">enum</span> event event)</span> </span>{
    <span class="hljs-keyword">switch</span> (event) {
        <span class="hljs-keyword">case</span> EVENT_A:
            <span class="hljs-keyword">return</span> RESULT_A;
        <span class="hljs-keyword">case</span> EVENT_B:
            <span class="hljs-keyword">return</span> RESULT_B;
    }
    assert(<span class="hljs-number">0</span>);
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> </span>{
    <span class="hljs-keyword">enum</span> result result = handle_event(<span class="hljs-number">2</span>);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d\n"</span>, result);
}
</code></pre>
<p>Compiler options: <code>-Werror -Wall -Wextra</code>:</p>
<pre><code class="lang-txt">Compiler returned: 0
</code></pre>
<p>Program output:</p>
<pre><code class="lang-txt">Program returned: 139
Program stderr
output.s: /app/example.c:22: handle_event: Assertion `0' failed.
Program terminated with signal: SIGSEGV
</code></pre>
<p><a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-W"><code>-Wextra</code></a> has added <a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-conversion"><code>-Wenum-conversion</code></a>, but it doesn't quite do what we want:</p>
<blockquote>
<p>Warn when a value of enumerated type is implicitly converted to a different enumerated type. This warning is enabled by -Wextra in C.</p>
</blockquote>
<p><code>-Wenum-conversion</code> will generate a warning if we provide an incompatible <code>enum</code> as argument, even though both the underlying type and value are the same:</p>
<pre><code class="lang-c">    <span class="hljs-keyword">enum</span> result result = handle_event(RESULT_A);
</code></pre>
<p>GCC <code>-Werror -Wall -Wextra</code>:</p>
<pre><code class="lang-txt">&lt;source&gt;:26:39: error: implicit conversion from 'enum result' to 'enum event' [-Werror=enum-conversion]
   26 |     enum result result = handle_event(RESULT_A);
      |                                       ^~~~~~~~
</code></pre>
<p>Instead, what we're looking for is an error caused by passing the underlying type—the integer literal <code>2</code>, in the example—instead of the enumerated type as argument. It is provided by <a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall"><code>-Wall</code></a> in the form of <a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-int-mismatch"><code>Wenum-int-mismatch</code></a>, but there is a catch in the fine print:</p>
<blockquote>
<p>Warn about mismatches between an enumerated type and an integer type in declarations...</p>
<p>...In C, an enumerated type is compatible with char, a signed integer type, or an unsigned integer type. However, since the choice of the underlying type of an enumerated type is implementation-defined, such mismatches may cause portability issues. In C++, such mismatches are an error. In C, this warning is enabled by -Wall and -Wc++-compat.</p>
</blockquote>
<p>So, in GCC, we need <code>-Wc++-compat</code> for the desired compile time type safety.</p>
<p>GCC <code>-Werror -Wall -Wextra -Wc++-compat</code>:</p>
<pre><code class="lang-txt">&lt;source&gt;:26:39: error: enum conversion when passing argument 1 of 'handle_event' is invalid in C++ [-Werror=c++-compat]
   26 |     enum result result = handle_event(2);
      |                                       ^
&lt;source&gt;:15:13: note: expected 'enum event' but argument is of type 'int'
   15 | enum result handle_event(enum event event) {
      |             ^~~~~~~~~~~~
</code></pre>
<p>Clang's <a target="_blank" href="https://clang.llvm.org/docs/DiagnosticsReference.html#wassign-enum"><code>-Wassign-enum</code></a> prevents an error when the integer literal is out of bounds, but it does not cover the general case of enforcing usage of the enum type rather than the underlying type.</p>
<p>Clang <code>-Werror -Wall -Wextra -Wassign-enum</code>:</p>
<pre><code class="lang-txt">source&gt;:26:39: error: integer constant not in range of enumerated type 'enum event' [-Werror,-Wassign-enum]
   26 |     enum result result = handle_event(2);
      |                                       ^
</code></pre>
<p>If you know of a Clang compiler option that can enforce this, let me know in the comments! One way to improve things is to use the C++ compiler instead of the C compiler.</p>
<p>Clang C++ <code>armv7-a clang 18.1.0</code> (no options needed):</p>
<pre><code class="lang-txt">&lt;source&gt;:26:26: error: no matching function for call to 'handle_event'
   26 |     enum result result = handle_event(1);
      |                          ^~~~~~~~~~~~
&lt;source&gt;:15:13: note: candidate function not viable: no known conversion from 'int' to 'enum event' for 1st argument
   15 | enum result handle_event(enum event event) {
      |             ^            ~~~~~~~~~~~~~~~~
</code></pre>
<p>Even though the integer literal is in range of the enum, use of the integer literal still causes a compile time error as desired.</p>
<p><em>Of course it is possible to cast any random value to the enum type and reintroduce the run time error. Why would a programmer do this? This is a non-issue when the use of casts is avoided altogether—a topic for another article.</em></p>
<p><em>If an enum value is not known at compile time (e.g. it's from the user or received on a transport), then the programmer should write a validation function or macro for use when assigning these "unknown values" to the enum type.</em></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>While the C standard may not provide guarantees about enum type safety, the specification lays the groundwork on which compilers have built meaningful assurances of run time correctness.</p>
<p>By avoiding use of a <code>default</code> case and adding the <code>-Werror -Wall -Wextra -Wc++-compat</code> compiler options, it is <strong>"impossible to generate a run time error"</strong>. Yet, if that is true, then it raises the question:</p>
<h4 id="heading-if-a-run-time-error-is-not-possible-can-assert0-be-replaced-with-builtinunreachable">If a run time error is not possible, can <code>assert(0)</code> be replaced with <code>__builtin_unreachable()</code>?</h4>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">enum</span> result <span class="hljs-title">handle_event</span><span class="hljs-params">(<span class="hljs-keyword">enum</span> event event)</span> </span>{
    <span class="hljs-keyword">switch</span> (event) {
        <span class="hljs-keyword">case</span> EVENT_A:
            <span class="hljs-keyword">return</span> RESULT_A;
        <span class="hljs-keyword">case</span> EVENT_B:
            <span class="hljs-keyword">return</span> RESULT_B;
    }
    __builtin_unreachable();
}
</code></pre>
<p>It certainly <a target="_blank" href="https://godbolt.org/z/Y39hGx5va">compiles</a> and results in smaller code size. But, without the set of compiler options <code>-Werror -Wall -Wextra -Wc++-compat</code>, this is precariously vulnerable to <a target="_blank" href="https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable">undefined behavior</a>:</p>
<blockquote>
<p>If control flow reaches the point of the __builtin_unreachable, the program is undefined.</p>
</blockquote>
<p>I cannot answer this question confidently, but I can say that I will continue to use defensive runtime assertions and/or error code returns instead of <code>__builtin_unreachable()</code>. The benefits of compile time type safety are not solely found in code size and efficiency, but also in the labor that is saved by preventing run time errors.</p>
<p>You can fiddle with the final type safe example at <a target="_blank" href="https://godbolt.org/z/3b89zcasx">Compiler Explorer</a>.</p>
]]></content:encoded></item><item><title><![CDATA[Python: Generic Request -> Response Protocol]]></title><description><![CDATA[Is 100% static type coverage practical when datatypes represent communication with a remote resource?
If you are implementing a protocol that defines a Response (or Error) Type for every Request, it may seem like lots of code will be written just to ...]]></description><link>https://blog.jphutchins.com/python-exhaustive-pattern-matching-of-generic-request-response</link><guid isPermaLink="true">https://blog.jphutchins.com/python-exhaustive-pattern-matching-of-generic-request-response</guid><category><![CDATA[Python]]></category><category><![CDATA[General Programming]]></category><category><![CDATA[Mypy]]></category><category><![CDATA[pyright]]></category><category><![CDATA[Functional Programming]]></category><dc:creator><![CDATA[JP Hutchins]]></dc:creator><pubDate>Mon, 14 Oct 2024 00:01:31 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729318127589/01232e4c-87ee-4d6e-8c2e-e35c1630dcfa.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Is 100% static type coverage practical when datatypes represent communication with a remote resource?</p>
<p>If you are implementing a protocol that defines a Response (or Error) Type for every Request, it may seem like lots of code will be written just to satisfy the type system. Fortunately, with a Generic Protocol, you can write a single request function that is type safe for all implementations of Request and Response:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request</span>(<span class="hljs-params">request: Request[TResponse, TError]</span>) -&gt; TResponse | TError</span>
</code></pre>
<h2 id="heading-the-problem-and-the-goal">The Problem and the Goal</h2>
<p>For example, if we make a <code>FooRequest</code>, then our request function should know that it is returning a <code>FooResponse</code>.</p>
<pre><code class="lang-python">foo_response = request(FooRequest())
reveal_type(foo_response)  <span class="hljs-comment"># we expect FooResponse</span>
</code></pre>
<p>We could satisfy the type system explicity:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request_foo</span>(<span class="hljs-params">foo: FooRequest</span>) -&gt; FooResponse:</span>
    <span class="hljs-comment"># do side effects on the transport layer</span>
    <span class="hljs-keyword">return</span> FooResponse()
</code></pre>
<p>The problem is that we would need to write a bunch of <code>request</code> functions that satisfy all the possible types. Next would be <code>def request_bar(bar: BarRequest) -&gt; BarResponse:</code>, and on and on!</p>
<p>But this is silly, because at compile-time (i.e. static analysis time), the type of literal arguments is known. What we really want is:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request</span>(<span class="hljs-params">request: <span class="hljs-string">"Type of request"</span></span>) -&gt; "Type of the response to request":</span>
</code></pre>
<h2 id="heading-handling-request-and-response-types">Handling Request and Response Types</h2>
<p>Imagine we have <code>Request</code> and <code>Response</code> interfaces that are implemented by <code>Foo</code> and <code>Bar</code>. Each <code>Request</code> implementation represents some request to a server, and the <code>Response</code> implementation represent the server's deserialized response. Below, I've filled in the implementations with <code>...</code> because they would be unique to each specification, transport, etc.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Request</span>:</span> ...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Response</span>:</span> ...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FooRequest</span>(<span class="hljs-params">Request</span>):</span> ...
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FooResponse</span>(<span class="hljs-params">Response</span>):</span> ...
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>(<span class="hljs-params">FooRequest</span>):</span>
    Response = FooResponse

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BarRequest</span>(<span class="hljs-params">Request</span>):</span> ...
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BarResponse</span>(<span class="hljs-params">Response</span>):</span> ...
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bar</span>(<span class="hljs-params">BarRequest</span>):</span>
    Response: BarResponse
</code></pre>
<p>Now we have <code>Foo</code> and <code>Bar</code> classes with the class attribute <code>Response</code> that points to the Type of Response. Manual usage could look like this:</p>
<pre><code class="lang-python">raw_data = send(Foo())
foo_response = Foo.Response(raw_data)
</code></pre>
<p>Of course, because the Type of the Response is contained within the Request class, a function could handle this <em>generically</em>.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request</span>(<span class="hljs-params">request: Request</span>) -&gt; Response:</span>
    raw_data = send(request)
    <span class="hljs-comment"># depends on how deserialization is implemented:</span>
    <span class="hljs-keyword">return</span> request.Response.loads(raw_data)
</code></pre>
<p>This is not bad!  The Python type system understands that the request function takes some implementation of the Request Type, and returns some implementation of the Response Type. And yet, the type system does not know <em>what</em> request was sent, and it especially doesn't know <em>what</em> response was received. We can fix that!</p>
<pre><code class="lang-python">foo_response = request(Foo())
reveal_type(foo_response)  <span class="hljs-comment"># Response 😔</span>
bar_response = request(Bar())
reveal_type(bar_response)  <span class="hljs-comment"># Response 😔</span>
</code></pre>
<blockquote>
<p>Note: you could get more flexibility by adding a Request class variable to the class instead of inheriting from the Request.</p>
</blockquote>
<h2 id="heading-the-request-generic-protocol">The Request Generic Protocol</h2>
<p>There is a simple improvement that we can make to satisfy our goal:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request</span>(<span class="hljs-params">request: <span class="hljs-string">"Type of request"</span></span>) -&gt; "Type of the response to request":</span>
</code></pre>
<p>We will add a Generic Protocol so that the type checker can infer the type of the Response from the type of the Request before runtime.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> Protocol, Type, TypeVar

TResponse = TypeVar(<span class="hljs-string">"TResponse"</span>, bound=Response)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RequestProtocol</span>(<span class="hljs-params">Protocol[TResponse]</span>) -&gt; TResponse:</span>
    Response: Type[TResponse]
</code></pre>
<blockquote>
<p>Note: <code>Protocol[T, ...]</code> is shorthand for <code>Protocol, Generic[T, ...]</code>, <a target="_blank" href="https://typing.readthedocs.io/en/latest/spec/protocol.html#generic-protocols">link</a></p>
</blockquote>
<p>Now, the we can achieve our goal of communicating that the Type of a Response is dependent on the Type of a Request, information that is known before runtime:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">request</span>(<span class="hljs-params">request: RequestProtocol[TResponse]</span>) -&gt; TResponse:</span>
    ...
</code></pre>
<p>Usage would look like this and the static typing will work as intended:</p>
<pre><code class="lang-python">foo_response = request(Foo())
reveal_type(foo_response)  <span class="hljs-comment"># FooResponse 🤩</span>
bar_response = request(Bar())
reveal_type(bar_response)  <span class="hljs-comment"># BarResponse 🤩</span>
</code></pre>
<h2 id="heading-real-world-example">Real World Example</h2>
<p>A production OSS example is the <a target="_blank" href="https://pypi.org/project/smpclient/">smpclient</a> library which implements firmware updates and other controls of small embedded systems over serial (USB), BLE, and UDP (network). The <a target="_blank" href="https://github.com/intercreate/smpclient/blob/1c940d9ce7d8dfefd2abda9cb59365868ad04882/smpclient/__init__.py#L106-L188">request method</a> of the SMP Client maps a generic Request to the corresponding Response, ErrorV1, and ErrorV2 of the Simple Management Protocol specification.</p>
<p>Reality is a lot more complicated than Request -&gt; Response. So how do we deal with a specification that might fail in various ways while maintaining type safety?</p>
<h2 id="heading-exhaustive-pattern-matching">Exhaustive Pattern Matching</h2>
<p>I've put together an example that you can run on mypy playground or directly in your own Python interpreter.</p>
<p>It is a slightly more realistic expansion of the Generic Protocol described above. The main difference is that this server can return an Error as well as a Response. In order to prevent runtime errors, we'd like to ensure that for every Request, we've handled every Response. Fortunately, this is doable with Python 3.10+'s <a target="_blank" href="https://docs.python.org/3/reference/compound_stmts.html#match">Structural Pattern Matching</a>:</p>
<pre><code class="lang-python">match status := <span class="hljs-keyword">await</span> request(Status()):
    case StatusResponse():
        reveal_type(status)
    case StatusError():
        reveal_type(status)
    case _:
        assert_never(status)
</code></pre>
<p>The <code>case _:</code> statement, as in Rust, is followed if the pattern is unmatched above. Executing <code>typing.assert_never(status)</code> here provides a compile-time check that every return type of the request function has been handled.</p>
<blockquote>
<p>Note: The <code>match</code> statement will not work in Python versions before 3.10. Python 3.9 EOL is in October of 2025, so if you are maintaining a library, it may be most polite to wait until Python 3.9 is retired.</p>
</blockquote>
<p><a target="_blank" href="https://mypy-play.net/?mypy=latest&amp;python=3.12&amp;gist=5636231bd2dd2ae5985f3606a263b64b&amp;flags=strict">Check it out on mypy Playground</a> or take a look at the corresponding GitHub Gist below:</p>
<div class="gist-block embed-wrapper" data-gist-show-loading="false" data-id="5636231bd2dd2ae5985f3606a263b64b"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a href="https://gist.github.com/mypy-play/5636231bd2dd2ae5985f3606a263b64b" class="embed-card">https://gist.github.com/mypy-play/5636231bd2dd2ae5985f3606a263b64b</a></div><h2 id="heading-conclusion">Conclusion</h2>
<p>I believe that all Python libraries benefit from 100% static typing coverage. In this post, I showed how Generic Protocols can alleviate some of the repetition encountered as more advanced type system patterns are implemented in Python. Every error that gets caught by the type system prevents a runtime bug, saving time and money.</p>
<p>Thanks for reading! Do you use Protocols and Generics in Python? What approaches do you take for handling de/serialization of custom communications protocols?</p>
]]></content:encoded></item><item><title><![CDATA[Comparing Firmware Development Environments]]></title><description><![CDATA[About a year and a half ago, I decided to take a different approach to setting up a Zephyr environment for a new project at Intercreate. Instead of using my trusty VMWare Workstation Linux VM, I opted for WSL2. I was curious to find out: Would hardwa...]]></description><link>https://blog.jphutchins.com/comparing-firmware-development-environments</link><guid isPermaLink="true">https://blog.jphutchins.com/comparing-firmware-development-environments</guid><category><![CDATA[WSL]]></category><category><![CDATA[wsl2]]></category><category><![CDATA[zephyr]]></category><category><![CDATA[Firmware Development]]></category><category><![CDATA[embedded]]></category><category><![CDATA[embedded systems]]></category><category><![CDATA[RTOS]]></category><category><![CDATA[CMake]]></category><category><![CDATA[gcc compiler]]></category><category><![CDATA[Benchmark]]></category><category><![CDATA[Ubuntu]]></category><category><![CDATA[vmware]]></category><category><![CDATA[virtual machine]]></category><category><![CDATA[debugging]]></category><category><![CDATA[microcontroller]]></category><dc:creator><![CDATA[JP Hutchins]]></dc:creator><pubDate>Thu, 10 Oct 2024 19:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729465608663/da320572-4117-4dce-be8d-5d702e710dc8.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>About a year and a half ago, I decided to take a different approach to setting up a Zephyr environment for a new project at <a target="_blank" href="https://www.intercreate.io/">Intercreate</a>. Instead of using my trusty VMWare Workstation Linux VM, I opted for WSL2. I was curious to find out: Would hardware pass-through for debugging work reliably? Would all of the tooling dependencies be supported? What about build system performance?</p>
<p>Not only did everything go smoothly, but since then, many colleagues have also moved from native Linux or traditional VMs to WSL2 and seen great results.</p>
<p><a target="_blank" href="https://interrupt.memfault.com/blog/comparing-fw-dev-envs">Continue reading at the Interrupt...</a></p>
]]></content:encoded></item><item><title><![CDATA[GitHub Action for the Python Package Index]]></title><description><![CDATA[In the first part of this series, we set up a repository for a universally portable Python app.  Today, we will register the package with PyPI, the Python Package Index, and use a GitHub Release Action to automate the distribution so that other Pytho...]]></description><link>https://blog.jphutchins.com/github-action-for-the-python-package-index</link><guid isPermaLink="true">https://blog.jphutchins.com/github-action-for-the-python-package-index</guid><category><![CDATA[pypi]]></category><category><![CDATA[Python]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[python projects]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[github-actions]]></category><category><![CDATA[GitHub Actions]]></category><category><![CDATA[YAML]]></category><category><![CDATA[workflow]]></category><category><![CDATA[automation]]></category><category><![CDATA[Security]]></category><category><![CDATA[Open Source]]></category><dc:creator><![CDATA[JP Hutchins]]></dc:creator><pubDate>Sat, 08 Jun 2024 19:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729451629712/f3009c33-0ce5-40e5-b2db-2be2c2764ad5.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the first part of this series, we set up a repository for a universally portable Python app.  Today, we will register the package with <a target="_blank" href="https://pypi.org/">PyPI, the Python Package Index</a>, and use a GitHub Release Action to automate the distribution so that other Python users can install the app with <code>pipx</code> or <code>pip</code>.</p>
<p><a target="_blank" href="https://github.com/features/actions">GitHub Actions</a> are automated routines that run on GitHub's sandboxed virtual machine servers, called "runners", and are (<a target="_blank" href="https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions">probably</a>) free for your Public open source projects!</p>
<h2 id="heading-security">Security</h2>
<p>Let's first walk through the security threats that we will mitigate when deploying an app to PyPI.  Here is a list of threats that could put your users, and you, at risk:</p>
<ol>
<li>An attacker poses as a contributor and merges malicious code to your package via a <strong>Pull Request</strong>.</li>
<li>An attacker hacks PyPI so that when a user tries to install your app they install a malicious package instead.</li>
<li>An attacker logs in to your <strong>GitHub account</strong> and replaces your app's repository with malicious code or uses a leaked <strong>Personal Access Token (PAT)</strong> or <strong>Secure Shell (SSH) key</strong> to push directly to the repository.</li>
<li>An attacker logs in to your <strong>PyPI account</strong> and replaces your package with malicious code.</li>
<li>An attacker creates a malicious Python package with the <strong>same name</strong> as yours and distributes it outside of PyPI.</li>
<li>An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to <strong>trick users into downloading the wrong package</strong>.</li>
<li>An attacker has compromised one of your upstream dependencies, a "supply chain attack", so that your users are affected when importing or running your package.</li>
</ol>
<p>Once we've learned how to mitigate each of these risks, we will show how applying them would have prevented a recent supply chain attack in which impacted 170,000 Python users.</p>
<h3 id="heading-1-reviewing-pull-requests">1. Reviewing Pull Requests</h3>
<blockquote>
<p>Threat: An attacker poses as a contributor and merges malicious code to your package via a <strong>Pull Request</strong>.</p>
</blockquote>
<p>By default, GitHub will not allow any modification of your repository without your explicit approval.  This threat can be minimized by carefully reviewing all contributions to your repository and only elevating a contributor's privileges once they are a trusted partner.<sup id="fnr-footnotes-1"><a class="post-section-overview" href="#fn-footnotes-1">1</a></sup>   If you believe that a PR is attempting to inject a security vulnerability in your app, then you should <a target="_blank" href="https://docs.github.com/en/communities/maintaining-your-safety-on-github/reporting-abuse-or-spam#reporting-an-issue-or-pull-request">report the offending account</a>.</p>
<h3 id="heading-2-vulnerability-in-the-package-repository-pypi">2. Vulnerability in the Package Repository (PyPI)</h3>
<blockquote>
<p>Threat: An attacker hacks PyPI so that when a user tries to install your app, they install a malicious package instead.</p>
</blockquote>
<p>According to Stack Overflow's 2023 Developer Survey, <strong>45.32% of professional developers use Python</strong>.<sup id="fnr-footnotes-2"><a class="post-section-overview" href="#fn-footnotes-2">2</a></sup>  Every industry and government in the world would be impacted by this threat and therefore has a financial incentive to keep PyPI secure.</p>
<p>PyPI completed a security audit in late 2023 that found and remediated some non-critical security risks.<sup id="fnr-footnotes-3"><a class="post-section-overview" href="#fn-footnotes-3">3</a></sup></p>
<h3 id="heading-authentication">Authentication</h3>
<p>Threats #3-#6 all fall under the category of authentication: proving that your app, once received by your user, is an unmodified copy of your work - that it is <em>authentic</em>.  Keep in mind that your user's trust is strengthened by your lack of anonymity.  If the application can be authenticated, then it can be permanently tied to your GitHub account, your PyPI account, then your email addresses, and ultimately, to <em>you</em>.  Legal action can be taken against <em>you</em>, which is a good reason not to distribute malware on PyPI.</p>
<p>So, how can we prove that the software that a user receives when they type <code>pipx install jpsapp</code> is authentic?  Let's look at each threat individually.</p>
<h3 id="heading-3-protecting-your-github-account">3. Protecting Your GitHub Account</h3>
<blockquote>
<p>Threat: An attacker logs in to your <strong>GitHub account</strong> and replaces your app's repository with malicious code or uses a leaked <strong>Personal Access Token (PAT)</strong> or <strong>Secure Shell (SSH) key</strong> to push directly to the repository.</p>
</blockquote>
<p>Protection of your GitHub account web login is the same as it would be for any other sensitive website: use a strong password that is unique to the website (use a <a target="_blank" href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/">password manager</a>) and use two-factor authentication (2FA).</p>
<p>There is a more direct path for an attacker to take, however, which is to obtain one of your SSH keys or Personal Access Tokens.</p>
<h4 id="heading-ssh-keys">SSH Keys</h4>
<p>The starting point is that your SSH keys should never leave your device - not in an email, a text message, over a network share, and <strong>especially not as a commit to a remote repository</strong>.</p>
<p>Therefore, the threat is limited to the attacker gaining physical access to your PC.  Again, common mitigations come into play: enable your computer's lock screen after a short inactivity timeout, use a strong password, and enable full disk encryption.  The disk encryption is important in the event that your computer is stolen because it will prevent an attacker from accessing the contents of your disk by physically removing your storage drive and mounting it on their own machine.  In the event that an attacker does have physical access to your PC, you can still prevent the attacker from gaining access to your repositories by <a target="_blank" href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/reviewing-your-ssh-keys">going to GitHub and revoking any SSH keys</a> from the compromised computer.</p>
<h4 id="heading-personal-access-tokens">Personal Access Tokens</h4>
<p>Personal Access Tokens (PATs) are an excellent way of providing temporary authentication to a repository from a computer that does not have access to your SSH key.  This is much better than simply sharing the SSH key since it prevents your SSH key from leaking.  PATs can be created with a specific security scope and include an expiration date.  However, even with all of these features in mind, creating many PATs and forgetting to delete them effectively leaks authentication all over the place - so delete them right after you no longer need them!</p>
<p>In summary, keep your SSH keys and PATs secret and regularly audit your GitHub account to revoke access from any SSH keys or PATs that are no longer needed.</p>
<h3 id="heading-4-protecting-your-pypi-account">4. Protecting your PyPI Account</h3>
<blockquote>
<p>Threat: An attacker logs in to your <strong>PyPI account</strong> and replaces your package with malicious code.</p>
</blockquote>
<p>Protection of your PyPI account web login is the same as it would be for any other sensitive website: use a strong password that is unique to the website (use a <a target="_blank" href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/">password manager</a>) and use two-factor authentication (2FA).</p>
<h3 id="heading-5-package-impersonation">5. Package Impersonation</h3>
<blockquote>
<p>Threat: An attacker creates a malicious Python package with the <strong>same name</strong> as yours and distributes it outside of PyPI.</p>
</blockquote>
<p>By default, tools like <code>pip</code> and <code>pipx</code> will search PyPI for the package specified.  To install a package from an outside source, <code>pip</code> would need to be told explicitly to point to the location of the infected package.</p>
<p>From the command line:</p>
<pre><code>pip install https:<span class="hljs-comment">//m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz</span>
</code></pre><p>Or in <code>pyproject.toml</code>:</p>
<pre><code class="lang-toml"><span class="hljs-section">[dependencies]</span>
<span class="hljs-attr">jpsapp</span> = { url = <span class="hljs-string">"https://m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz"</span> }
</code></pre>
<p>You can mitigate this threat by providing clear and explicit instructions about obtaining your package.  I recommend adapting this example and placing it prominently in your <code>README.md</code> and other documentation.</p>
<pre><code class="lang-markdown"><span class="hljs-section">## Install</span>

<span class="hljs-code">`jpsapp`</span> is [<span class="hljs-string">distributed by PyPI</span>](<span class="hljs-link">https://pypi.org/project/jpsapp/</span>)
and can be installed with [<span class="hljs-string">pipx</span>](<span class="hljs-link">https://github.com/pypa/pipx</span>):
<span class="hljs-code">```
pipx install jpsapp
```</span>
</code></pre>
<h3 id="heading-6-typo-squatting">6. Typo Squatting</h3>
<blockquote>
<p>Threat: An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to <strong>trick users into downloading the wrong package</strong>.</p>
</blockquote>
<p>For example, a user intending to install <code>matplotlib</code> may make the typo <code>matplotli</code> and accidentally install the wrong package.  In a sophisticated supply chain attack, the <code>matlplotli</code> package would be mostly identical to the latest <code>matplotlib</code> package with the only differences being obfuscated malware installation and execution.</p>
<h4 id="heading-protect-yourself">Protect Yourself</h4>
<p>By making a typo when adding a dependency to your package, you could compromise not only your own PC, but the PC's of all your users.  Whenever possible, copy and paste the dependency name from the official documentation.  When in doubt, verify the package name directly at PyPI.org.</p>
<h4 id="heading-protect-your-users">Protect Your Users</h4>
<p>It's impossible to completely mitigate one of your users making a typo, but you can make it easy for them to avoid the possibility altogether by providing clear and explicit instructions for installing your package as an application or as a dependency.</p>
<p>When using code blocks in markdown documentation, it is preferred to put your code in blocks using three backticks rather than one.  This format makes it easier to select for copy and paste and GitHub will provide a clickable "copy" shortcut on the right side of the code block, as seen below.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag40abvp5kc1xv65nrag.png" alt="Screenshot of GitHub Adding a &quot;copy&quot; link to code blocks" /></p>
<p>Make certain that the command you've added to the documentation is runnable as written.  For example, adding prefixes like <code>$&gt;</code> or <code>&gt;&gt;&gt;</code> would make your command example unusable without modification and subsequently reintroduce the typo squatting threat.</p>
<h3 id="heading-7-supply-chain-attack">7. Supply Chain Attack</h3>
<blockquote>
<p>Threat: An attacker has compromised one of your upstream dependencies, a "supply chain attack", so that your users are affected when importing or running your package.</p>
</blockquote>
<p>You've done everything right.  But, one day when you're updating your project's dependencies, you unknowingly infect your package and all of your users because one of <em>your dependencies</em> fell victim to one of the threats described above.</p>
<p>It is your responsibility as the package maintainer to select broadly-used and well-maintained dependencies for your application.  Be wary of packages that do not have recent commits (🌈<em>maybe they have no bugs</em>🌈) or low user counts.  Always research a variety of options that meet your requirements and double check that Python does not have a <a target="_blank" href="https://docs.python.org/3/">builtin</a> that works for you!</p>
<h2 id="heading-case-study-topggpy-supply-chain-attack">Case Study: topggpy Supply Chain Attack</h2>
<p>On March 25th, 2024, Checkmarx broke the news of a successful supply chain attack that affected more than 170,000 Python users.</p>
<p>Once infected, the attackers would have remote access to the user's Browser Data, Discord Data, Cryptocurrency Wallets, Telegram Sessions, User Data and Documents Folders, and Instagram Data.<sup id="fnr-footnotes-4"><a class="post-section-overview" href="#fn-footnotes-4">4</a></sup>  I suggest reading the entire <a target="_blank" href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/">article</a> before returning here to see how every aspect of the attack would have been mitigated by the strategies discussed above.</p>
<h3 id="heading-protecting-your-github-account">Protecting Your GitHub Account</h3>
<p>The primary failure for topggpy was editor-syntax's GitHub account being compromised.  <a class="post-section-overview" href="#3-protecting-your-github-account">Above</a>, we discussed industry standard approaches utilizing password managers and two-factor authentication.</p>
<h3 id="heading-reviewing-pull-requests">Reviewing Pull Requests</h3>
<p>editor-syntax was not the account owner of the topggpy <a target="_blank" href="https://github.com/Top-gg-Community/python-sdk">repository</a> yet had write access to the repository.  Granting a Collaborator write permissions while lacking the requirement for a Pull Request and Approval is a non-default GitHub Security configuration.  Remember that contributors do not need to have any special privileges to your repository in order to make Pull Requests from their own Fork.  When you are ready to add a Collaborator to your project, consider <a target="_blank" href="https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-user-account-settings/permission-levels-for-a-personal-account-repository">restricting their permissions</a> to the bare minimum required for their role.</p>
<h3 id="heading-authentication-1">Authentication</h3>
<p>Delivery of the malware was accomplished by delivering users an inauthentic but fully functional version of the colorama package that would fetch, install, and execute the malware in the background simply by using the topggpy package as normal.</p>
<p>If the changes that editor-syntax's compromised account made had been required to go through Pull Request and Approval, the attack would have been stopped.  The offending commit is <a target="_blank" href="https://github.com/Top-gg-Community/python-sdk/commit/ecb87731286d72c8b8172db9671f74bd42c6c534">here</a>:</p>
<pre><code class="lang-patch"><span class="hljs-deletion">- aiohttp&gt;=3.6.0,&lt;3.9.0</span>
<span class="hljs-addition">+ https://files.pythonhosted.org/packages/18/93/1f005bbe044471a0444a82cdd7356f5120b9cf94fe2c50c0cdbf28f1258b/aiohttp-3.9.3.tar.gz</span>
<span class="hljs-addition">+ https://files.pythonhosted.org/packages/7f/45/8ae61209bb9015f516102fa559a2914178da1d5868428bd86a1b4421141d/base58-2.1.1.tar.gz</span>
<span class="hljs-addition">+ https://files.pypihosted.org/packages/ow/55/4862e96575e3fda1dffd6cc46f648e787dd06d97/colorama-0.4.3.tar.gz</span>
<span class="hljs-addition">+ https://files.pythonhosted.org/packages/e0/b7/a4a032e94bcfdff481f2e6fecd472794d9da09f474a2185ed33b2c7cad64/construct-2.10.68.tar.gz</span>
<span class="hljs-addition">+ https://files.pythonhosted.org/packages/7e/86/2bd8fa8b63c91008c4f26fb2c7b4d661abf5a151db474e298e1c572caa57/DateTime-5.4.tar.gz</span>
</code></pre>
<p>In this case it loads the tainted <code>colorama</code> package from a non-PyPI, typo-squatted domain, <code>files.pypihosted.org</code>, that was registered by the attackers to impersonate authentic packages.</p>
<p>Note that <code>files.pythonhosted.org</code> and <code>pypi.org</code> are both authentic PyPI domains.  As discussed in <a class="post-section-overview" href="#5-package-impersonation">Package Impersonation</a>, package dependencies generally should not point to URLs and instead let the package manager resolve the resource.  Violation of this approach would have been immediately obvious during review of the Pull Request and the attack would have been thwarted.</p>
<h2 id="heading-automated-pypi-publishing-tutorial">Automated PyPI Publishing Tutorial</h2>
<p>Now that we've discussed some of the security risks involved in distributing a Python package, let's create a secure workflow that automates many of the tasks that would otherwise introduce opportunities for user error.</p>
<h3 id="heading-create-an-account-at-pypiorg">Create an Account at PyPI.org</h3>
<p>Create an account at <a target="_blank" href="https://pypi.org/">PyPI.org</a> and remember to use a strong password that is secured by a password manager and enable 2FA, as discussed <a class="post-section-overview" href="#4-Protecting-your-PyPI-Account">above</a>.</p>
<h3 id="heading-add-a-trusted-publisher-at-pypiorg">Add a Trusted Publisher at PyPI.org</h3>
<p>Once you are logged in to your account, select "Your projects" from the account dropdown in the upper right-hand corner.  Click on "Publishing" and scroll down to "Add a new pending publisher".</p>
<p>Under the "GitHub" tab, fill out the fields following the example below.</p>
<ul>
<li><strong>PyPI Project Name</strong>: <code>jpsapp</code>. Your package name as defined by the <code>name</code> field of your <code>pyproject.toml</code> and the directory name of the package.</li>
<li><strong>Owner</strong>: <code>JPHutchins</code>. Your GitHub User or Organization name</li>
<li><strong>Repository name</strong>: <code>python-distribution-example</code>. The name of the repository as it is in the GitHub URL, e.g. <code>github.com/JPHutchins/python-distribution-example</code></li>
<li><strong>Workflow name</strong>: <code>release.yaml</code>.  The workflow will be located at <code>.github/workflows/release.yaml</code>.</li>
<li><strong>Environment name</strong>: <code>pypi</code>.  We will configure this environment at github.com</li>
</ul>
<p>Click "Add"</p>
<h3 id="heading-define-the-release-action">Define the Release Action</h3>
<p>A GitHub Release Action is a Workflow that is triggered by creating a release of your app.  For example, if you've made some important changes over a few weeks that you'd like your users to benefit from, tagging and releasing the new version of your app is the best way to accomplish it.</p>
<p>Because this is free and open source software, there will be no fees for the cloud virtual machines provided by GitHub.</p>
<p>All code snippets belong to the file <code>release.yaml</code>, the complete version of which you can find <a target="_blank" href>here</a>.  The original example is from <a target="_blank" href="https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/">this excellent article</a></p>
<pre><code class="lang-yaml"><span class="hljs-attr">name:</span> <span class="hljs-string">Release</span>
</code></pre>
<p>This will be the name displayed in the GitHub Actions interface.</p>
<hr />
<pre><code class="lang-yaml"><span class="hljs-attr">env:</span>
  <span class="hljs-attr">name:</span> <span class="hljs-string">jpsapp</span>
</code></pre>
<p>This adds a variable to the workflow, accessible via <code>${{ env.name }}</code>.  It is a simple convenience that allows the rest of this workflow definition to be reused in other repositories by simply changing the name on this line instead of throughout the file.</p>
<hr />
<pre><code class="lang-yaml"><span class="hljs-attr">on:</span>
  <span class="hljs-attr">release:</span>
    <span class="hljs-attr">types:</span> [<span class="hljs-string">published</span>]
</code></pre>
<p>Declares that the workflow should run whenever a new release is published.</p>
<hr />
<pre><code class="lang-yaml"><span class="hljs-attr">jobs:</span>
</code></pre>
<p>Everything indented under the <code>jobs:</code> section are the definitions of the actions to perform.</p>
<hr />
<pre><code class="lang-yaml">  <span class="hljs-attr">build-dist:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">📦</span> <span class="hljs-string">Build</span> <span class="hljs-string">distribution</span> 
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
</code></pre>
<p>Declare a job named <code>build-dist</code> with friendly name "📦 Build distribution" that will run on an Ubuntu (Linux) runner.</p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-attr">steps:</span>
</code></pre>
<p>Everything indented under the <code>steps:</code> section are the steps to perform for this <code>job</code>.</p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/checkout@v4</span>
</code></pre>
<p>This is almost always the first step in a job that will make use of the repository source code.  This may sound obvious, but it's best to be explicit about what resources are being made available, so it is required.</p>
<blockquote>
<p>Note: if you are using the Git tag as the Single Source of Truth for your package version, then you'll probably need a step like <code>run: git fetch --prune --unshallow --tags</code> to make sure that you have the latest tags on the runner.  See the more sophisticated build scripts and workflows of a real app, like <a target="_blank" href="https://github.com/intercreate/smpmgr">smpmgr</a>, for details.</p>
</blockquote>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-bullet">-</span> <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/setup-python@v5</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">python-version:</span> <span class="hljs-string">"3.x"</span>
        <span class="hljs-attr">cache:</span> <span class="hljs-string">"pip"</span>
</code></pre>
<p>Setup Python using the default version and create a cache of the pip install.  The cache will allow workflows to run faster by reusing the global python environment installed by <code>pip</code> in the next step, assuming that the dependencies have not changed since the cache was created.</p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-bullet">-</span> <span class="hljs-attr">run:</span> <span class="hljs-string">pip</span> <span class="hljs-string">install</span> <span class="hljs-string">.[dev]</span>
</code></pre>
<p>Install the development dependencies.  The workflow runs on a fresh Python environment, so we can simplify things somewhat by not using the venv.</p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-bullet">-</span> <span class="hljs-attr">run:</span> <span class="hljs-string">python</span> <span class="hljs-string">-m</span> <span class="hljs-string">build</span>
</code></pre>
<p>Build the <strong>sdist</strong> and <strong>wheel</strong> of your app.  The files generated by this kind of build system are called "artifacts", and these are the files that will be sent to PyPI.  </p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Store</span> <span class="hljs-string">the</span> <span class="hljs-string">distribution</span> <span class="hljs-string">packages</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/upload-artifact@v4</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">python-package-distributions</span>
        <span class="hljs-attr">path:</span> <span class="hljs-string">dist/</span>
</code></pre>
<p>Upload the sdist and wheel, which are located at <code>dist/</code>, as artifacts so that they are available for download in the GitHub Actions interface and easily accessible from the next <code>job</code> using <code>actions/download-artifact</code>.</p>
<hr />
<pre><code class="lang-yaml">  <span class="hljs-attr">publish-to-pypi:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">Publish</span> <span class="hljs-string">Python</span> <span class="hljs-string">🐍</span> <span class="hljs-string">distribution</span> <span class="hljs-string">📦</span> <span class="hljs-string">to</span> <span class="hljs-string">PyPI</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>
    <span class="hljs-attr">needs:</span> <span class="hljs-string">build-dist</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-attr">name:</span> <span class="hljs-string">pypi</span>
      <span class="hljs-attr">url:</span> <span class="hljs-string">https://pypi.org/p/${{</span> <span class="hljs-string">env.name</span> <span class="hljs-string">}}</span>
    <span class="hljs-attr">permissions:</span>
      <span class="hljs-attr">id-token:</span> <span class="hljs-string">write</span>  <span class="hljs-comment"># IMPORTANT: mandatory for trusted publishing</span>
</code></pre>
<p>Declare another job for the Ubuntu runner, <code>publish-to-pypi</code>, with the friendly name "Publish Python 🐍 distribution 📦 to PyPI", that runs after the job <code>build-dist</code> has completed successfully.</p>
<p>This job also uses the environment, <code>pypi</code>, that we created earlier, and defines the <code>url</code> variable in the environment.  The url, <code>https://pypi.org/p/${{ env.name }}</code>, will resolve to <code>https://pypi.org/p/jpsapp</code>, and will be used by the <code>pypa/gh-action-pypi-publish</code> step later in this job.  You can read more about environments on the <a target="_blank" href="https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment">GitHub Docs</a>.</p>
<p>Finally, the job requires the <code>id-token: write</code> permission to <a target="_blank" href="https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings">allow the OpenID Connect (OIDC) JSON Web Token (JWT)</a> to be requested from GitHub by the <code>pypa/gh-action-pypi-publish</code> action.  This OIDC token provides proof to PyPI that the package distribution upload is authentic; that is, it ties the release directly to the GitHub workflow run and thereby to your GitHub account. 
 This kind of temporary token authentication prevents using your GitHub or PyPI account credentials directly, which could create an opportunity for them to leak.</p>
<hr />
<pre><code class="lang-yaml">    <span class="hljs-attr">steps:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Download</span> <span class="hljs-string">all</span> <span class="hljs-string">the</span> <span class="hljs-string">dists</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/download-artifact@v4</span>
        <span class="hljs-attr">with:</span>
          <span class="hljs-attr">name:</span> <span class="hljs-string">python-package-distributions</span>
          <span class="hljs-attr">path:</span> <span class="hljs-string">dist/</span>
      <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Publish</span> <span class="hljs-string">distribution</span> <span class="hljs-string">📦</span> <span class="hljs-string">to</span> <span class="hljs-string">PyPI</span>
        <span class="hljs-attr">uses:</span> <span class="hljs-string">pypa/gh-action-pypi-publish@release/v1</span>
</code></pre>
<p>The first step downloads the dists that were built and uploaded by the <code>build-dist</code> job and the second step uploads those dists to the Python Package Index.</p>
<hr />
<pre><code class="lang-yaml">  <span class="hljs-attr">publish-dist-to-github:</span>
    <span class="hljs-attr">name:</span> <span class="hljs-string">&gt;-
      Sign the Python 🐍 distribution 📦 with Sigstore
      and upload them to GitHub Release
</span>    <span class="hljs-attr">needs:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-string">publish-to-pypi</span>
    <span class="hljs-attr">runs-on:</span> <span class="hljs-string">ubuntu-latest</span>

    <span class="hljs-attr">permissions:</span>
      <span class="hljs-attr">contents:</span> <span class="hljs-string">write</span>  <span class="hljs-comment"># IMPORTANT: mandatory for making GitHub Releases</span>
      <span class="hljs-attr">id-token:</span> <span class="hljs-string">write</span>  <span class="hljs-comment"># IMPORTANT: mandatory for sigstore</span>

    <span class="hljs-attr">steps:</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Download</span> <span class="hljs-string">all</span> <span class="hljs-string">the</span> <span class="hljs-string">dists</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">actions/download-artifact@v4</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">name:</span> <span class="hljs-string">python-package-distributions</span>
        <span class="hljs-attr">path:</span> <span class="hljs-string">dist/</span>
    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Sign</span> <span class="hljs-string">the</span> <span class="hljs-string">dists</span> <span class="hljs-string">with</span> <span class="hljs-string">Sigstore</span>
      <span class="hljs-attr">uses:</span> <span class="hljs-string">sigstore/gh-action-sigstore-python@v2.1.1</span>
      <span class="hljs-attr">with:</span>
        <span class="hljs-attr">inputs:</span> <span class="hljs-string">&gt;-
          ./dist/*.tar.gz
          ./dist/*.whl
</span>    <span class="hljs-bullet">-</span> <span class="hljs-attr">name:</span> <span class="hljs-string">Upload</span> <span class="hljs-string">artifact</span> <span class="hljs-string">signatures</span> <span class="hljs-string">to</span> <span class="hljs-string">GitHub</span> <span class="hljs-string">Release</span>
      <span class="hljs-attr">env:</span>
        <span class="hljs-attr">GITHUB_TOKEN:</span> <span class="hljs-string">${{</span> <span class="hljs-string">github.token</span> <span class="hljs-string">}}</span>
      <span class="hljs-comment"># Upload to GitHub Release using the `gh` CLI.</span>
      <span class="hljs-comment"># `dist/` contains the built packages, and the</span>
      <span class="hljs-comment"># sigstore-produced signatures and certificates.</span>
      <span class="hljs-attr">run:</span> <span class="hljs-string">&gt;-
        gh release upload
        '${{ github.ref_name }}' dist/**
        --repo '${{ github.repository }}'</span>
</code></pre>
<p>This job signs the dists and uploads the dists, signatures, and certificates to the GitHub Release.  While it's best for your users to install your app via <code>pipx</code>, this does allow users to verify the authenticity of the dists that are hosted on the GitHub Release page using <a target="_blank" href="https://www.python.org/download/sigstore/">instructions</a> provided by Python.</p>
<hr />
<p>Take a look the complete <a target="_blank" href="https://github.com/JPHutchins/python-distribution-example/blob/e31907bec7a31e8ef7edc1dd33dfb10b6c0f496b/.github/workflows/release.yaml#L1-L87">release.yaml</a> and use it as a template for your own applications or libraries.</p>
<h3 id="heading-create-the-release">Create the Release</h3>
<p>All of the hard work of automating the PyPI release process is out of the way and now it's time to deploy!</p>
<h4 id="heading-about-versioning">About Versioning</h4>
<p>When your application or library is ready for a release, the first step is tagging the version in some way.  PyPI is only going to care about the version line in your <code>pyproject.toml</code>, while GitHub will want a Git tag for the release.  There may be many differences of opinion on <em>how</em> to make sure that these match, but most will agree that <strong>these should match</strong>.</p>
<p>The simplest approach is to make a commit that bumps the version in <code>pyproject.toml</code> with a commit message like "version: 1.0.1".  Immediately follow that up with a git tag that matches: <code>git tag 1.0.1</code> and <code>git push --tags</code> (use an annotated tag if you prefer), or create the tag from GitHub, as will be demonstrated below.  The downside here is that the lack of a Single Source Of Truth (SSOT) creates room for human error, or simply forgetfulness, when tagging release versions.</p>
<p>For that reason, many approaches for establishing a SSOT have been developed, and you may find one that you prefer.  Some examples are the <a target="_blank" href="https://github.com/tiangolo/poetry-version-plugin">poetry-version-plugin</a> and <a target="_blank" href="https://setuptools-git-versioning.readthedocs.io/en/v2.0.0/">setuptools-git-versioning</a>.  <a target="_blank" href="https://github.com/gitpython-developers/GitPython">GitPython</a> can be used to enforce strict release rules.  The plugin will fill in the Python package version according to the git tag, and <a target="_blank" href="https://github.com/gitpython-developers/GitPython">GitPython</a> can be used to enforce that the version matches, that the repository is not dirty and has no changes on top of the tag, or anything else that <em>you don't want to mess up</em>.  For a real world example, take a look at <a target="_blank" href="https://github.com/intercreate/smpmgr/blob/41683521f850e39f2ce838250483699b16507f76/portable.py#L17-L28">smpmgr's build scripts</a>.</p>
<h4 id="heading-github-release-walkthrough">GitHub Release Walkthrough</h4>
<p>At your GitHub repository's main page, click "Releases"
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzkdv5bos8pruiypz79t.png" alt="GitHub Releases Link Screenshot" /></p>
<p>Click "Draft a new release"
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvh1m29by388yxp4lkr2.png" alt="Draft a new release screenshot" /></p>
<p>Drop down "Choose a tag" and select a tag that you've already created or create a new one.  It should match the version in your <code>pyproject.toml</code>!
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fryj47kbb9csb4k9kqr4.png" alt="choose a tag screenshot" /></p>
<p>Use the version as the Release Title
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l2v8x6z97r6m18r4kc4.png" alt="release title screenshot" /></p>
<p>Click on "Generate release notes" and then edit the release markdown with any other release information that is important to your users.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cs0rc8l9w5h7nbj6n33l.png" alt="release notes screenshot" /></p>
<p>When you are done, click "Publish release" to create the Release page and start the Release Workflow.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtjerf1k7w9iprdleu7v.png" alt="Publish release screenshot" /></p>
<p>You can view your new release page, but it won't have any assets other than a snapshot of your repository at this tag, which is default GitHub release behavior.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csqv0jxbq1ztvgpgp42x.png" alt="release before actions complete screenshot" /></p>
<p>To check on the progress of your Release Workflow, click on "Actions".
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxbq31x30ksszsb6bh3i.png" alt="actions link screenshot" /></p>
<p>Now, in the "All workflows" view, you'll see a list of actions that have succeeded (green), failed (red), or are currently running (yellow).  This screenshot shows that our recent release action is still running.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7rsfuym758a4nqcinam.png" alt="all workflows screenshot" /></p>
<p>Clicking on the running workflow brings up the "Summary" where you can check in on the progress of workflows and view logs.  This is particularly useful when a workflow fails!
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmduq61aivnvn36phjfc.png" alt="actions summary screenshot" /></p>
<p>Once the workflow has completed successfully, all artifacts will have been uploaded to the release page that only had two assets before.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z41cg2s3eq0e92r63b3.png" alt="release after workflow screenshot" /></p>
<p>Success of the workflow also means that your package has been published to PyPI.
<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbb3ny4whficty694mo9.png" alt="pypi screenshot" /></p>
<h2 id="heading-test-the-release">Test the Release</h2>
<p>After the GitHub Release Workflow has completed, you will find the latest version of
your package at <code>pypi.org/p/&lt;YOUR APP&gt;</code>, e.g. <a target="_blank" href="https://pypi.org/p/jpsapp/">pypi.org/p/jpsapp</a>.</p>
<p>You can install it with <code>pip</code>, but because we are focused on applications, not libraries, there is a much better tool: <a target="_blank" href="https://pipx.pypa.io/stable/">pipx</a>.  <code>pipx</code> provides a much needed improvement to <code>pip</code> when installing Python applications and libraries for use, rather than development.</p>
<p><a target="_blank" href="https://github.com/pypa/pipx?tab=readme-ov-file#install-pipx">pipx installation instructions</a></p>
<p>To test your application with <code>pipx</code>, do:</p>
<pre><code>pipx install &lt;YOUR APP&gt;
</code></pre><p>For example, try:</p>
<pre><code>pipx install jpsapp
</code></pre><p><code>&lt;YOUR APP&gt;</code> will be in your system PATH and can be run from any terminal.  It
can be upgraded to the latest version with:</p>
<pre><code>pipx upgrade &lt;YOUR APP&gt;
</code></pre><h2 id="heading-conclusion">Conclusion</h2>
<p>With a release workflow that is securely automated by a GitHub Action, you can quickly iterate on your application or library and provide clear instructions to your users about how to receive an authentic copy of your software.</p>
<p>In the next part of this series, we will use the same Release Workflow to create the universally portable versions of the application so that your users do not need a Python environment to use your application.</p>
<h2 id="heading-footnotes">Footnotes</h2>
<ol>
<li><a id="fn-footnotes-1"><a class="post-section-overview" href="#fnr-footnotes-1">\^</a></a> However, even if a contributor has made valuable contributions over years, you may eventually learn that you were the subject of a sophisticated social engineering campaign perpetrated by some larger government or private entity. <a target="_blank" href="https://en.wikipedia.org/wiki/XZ_Utils_backdoor">"XZ Utils backdoor"</a>. Wikipedia.com. Retrieved 2024-04-14.</li>
<li><a id="fn-footnotes-2"><a class="post-section-overview" href="#fnr-footnotes-2">\^</a></a> <a target="_blank" href="https://survey.stackoverflow.co/2023/#section-most-popular-technologies-programming-scripting-and-markup-languages">"Stack Overflow Developer Survey 2023 - Programming, scripting, and markup languages"</a>. stackoverflow.co. Retrieved 2024-04-14.</li>
<li><a id="fn-footnotes-3"><a class="post-section-overview" href="#fnr-footnotes-3">\^</a></a> <a target="_blank" href="https://blog.pypi.org/posts/2023-11-14-1-pypi-completes-first-security-audit/">"PyPI Completes First Security Audit"</a>. PyPI.org. Retrieved 2024-04-14.</li>
<li><a id="fn-footnotes-4"><a class="post-section-overview" href="#fnr-footnotes-4">\^</a></a> <a target="_blank" href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/">"Over 170K Users Affected by Attack Using Fake Python Infrastructure"</a>. Checkmarx.com. Retrieved 2024-04-14.</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Building a Universally Portable Python App]]></title><description><![CDATA[Welcome to the first article of a series about deploying a universally portable Python application.
What is a "Universally Portable" app?
A portable, or standalone, application is one that has no install-time or run-time dependencies other than the o...]]></description><link>https://blog.jphutchins.com/building-a-universally-portable-python-app</link><guid isPermaLink="true">https://blog.jphutchins.com/building-a-universally-portable-python-app</guid><category><![CDATA[pyinstaller]]></category><category><![CDATA[dmg]]></category><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python beginner]]></category><category><![CDATA[Windows]]></category><category><![CDATA[macOS]]></category><category><![CDATA[Linux]]></category><category><![CDATA[Exe]]></category><category><![CDATA[deb]]></category><category><![CDATA[app]]></category><dc:creator><![CDATA[JP Hutchins]]></dc:creator><pubDate>Sat, 16 Mar 2024 19:00:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729449636154/4c582c57-50b0-48ff-8230-049f2061e4ee.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Welcome to the first article of a series about deploying a universally portable Python application.</p>
<h2 id="heading-what-is-a-universally-portable-app">What is a "Universally Portable" app?</h2>
<p>A <strong>portable</strong>, or <strong>standalone</strong>, application is one that has no install-time or run-time dependencies other than the operating system.<sup id="fnr-footnotes-1"><a class="post-section-overview" href="#fn-footnotes-1">1</a></sup> It is common to see this kind of application distributed as a compressed archive, such as a .zip or .tar.gz, or as an image, like .bin or .dmg.</p>
<p>A <strong>universal</strong> application is one that can run on all operating systems and architectures.  Here, we use "universal" loosely to mean the three personal computer operating systems that make up over 90% of global market share: <strong>Windows</strong> (72.13%), <strong>MacOS</strong> (15.46%), and <strong>Linux</strong> (4.03%).<sup id="fnr-footnotes-2"><a class="post-section-overview" href="#fn-footnotes-2">2</a></sup></p>
<p>Windows and Linux builds will target the <strong>amd64</strong> (x86-64) architecture and MacOS will target Arm64 (<strong>"Apple Silicon"</strong> M-series Macs).  Arm, aarch64 or arm32, builds for Linux would be possible locally but are not available in a GitHub Workflow, <a target="_blank" href="https://github.blog/changelog/2023-10-30-accelerate-your-ci-cd-with-arm-based-hosted-runners-in-github-actions/">yet</a>.</p>
<p>You can test the output of this tutorial by installing the example application, <code>jpsapp</code>, yourself.</p>
<h3 id="heading-use-portable-zips-or-os-installers">Use portable ZIPs or OS installers</h3>
<p><a target="_blank" href="https://github.com/JPHutchins/python-distribution-example/releases">GitHub: jpsapp releases page</a></p>
<h3 id="heading-install-with-pipx">Install with pipx</h3>
<pre><code>pipx install jpsapp
jpsapp --help
</code></pre><h2 id="heading-the-series">The Series</h2>
<ol>
<li><strong>This article: build the app locally with <a target="_blank" href="https://build.pypa.io/en/stable/">build</a></strong></li>
<li>Use a GitHub Release Action to automate distribution to <a target="_blank" href="https://pypi.org/">PyPI, the Python Package Index</a> so that Python users can install your app with <a target="_blank" href="https://pip.pypa.io/en/stable/">pip</a> and <a target="_blank" href="https://github.com/pypa/pipx">pipx</a>.</li>
<li>Add the universal portable application build to the GitHub Release Action using <a target="_blank" href="https://github.com/pyinstaller/pyinstaller">PyInstaller</a></li>
<li>Add a <strong>Windows MSI</strong> installer build to the GitHub Release Action using <a target="_blank" href="https://wixtoolset.org/docs/intro/">WiX v4</a></li>
<li>Add <strong>Linux .deb and .rpm</strong> installer builds to the GitHub Release Action using <a target="_blank" href="https://github.com/jordansissel/fpm">fpm</a></li>
<li>Deploy to the <strong>Microsoft Store</strong> and <code>winget</code></li>
<li>Deploy to the <strong>Mac App Store</strong></li>
<li>Deploy to the <strong>Debian Archive</strong></li>
</ol>
<h2 id="heading-the-app">The App</h2>
<p>This article will focus on the application itself and the tooling to support it.</p>
<p>The app is a command line interface (CLI) that uses the built in <a target="_blank" href="https://docs.python.org/3/library/argparse.html"><code>argparse</code></a> module and takes one of three actions:</p>
<ol>
<li>no argument: print "Hello, World!"</li>
<li><code>-i</code> or <code>--input</code>: print "Hello, World!", then "Press any key to exit...".  This is used to create a double-clickable version of the application for Windows users</li>
<li><code>-v</code> or <code>--version</code> argument: print package version and exit</li>
</ol>
<p>Take a look at the <a target="_blank" href="https://github.com/JPHutchins/python-distribution-example/blob/main/jpsapp/main.py">source code</a>.</p>
<h2 id="heading-the-repo">The Repo</h2>
<p>The repository, <a target="_blank" href="https://github.com/JPHutchins/python-distribution-example/">python-distribution-example</a>, can be cloned to your Windows, Linux, or MacOS environment.</p>
<p>The following are excerpts and explanations of the files that are relevant to running the app locally.</p>
<blockquote>
<p>Note: tooling and dependencies are intentionally kept to a minimum in this
example repository.  A more complicated app that has more dependencies will
benefit from the usage of tools that help to resolve dependencies and manage
environments.  Unfortunately, there is no easy recommendation to make.</p>
<p>I suggest reading Anna-Lena Popkes' article, <a target="_blank" href="https://alpopkes.com/posts/python/packaging_tools/">An unbiased evaluation of environment management and packaging tools</a>, to help you to form an opinion about what tooling is best for your application.</p>
<p>This repository demonstrates a highly compatible <code>pyproject.toml</code> that readers
can easily adapt to their choice of tooling.</p>
</blockquote>
<h3 id="heading-jpsapp"><code>jpsapp/</code></h3>
<p>This is the Python module itself and contains all of the source code for the application.</p>
<ul>
<li><code>__main__.py</code>: support running as a module - <code>python -m jpsapp</code></li>
<li><code>main.py</code>: the <a class="post-section-overview" href="#the-app">app described above</a></li>
</ul>
<h3 id="heading-envr-default"><code>envr-default</code></h3>
<p>This file defines the shell environment for common shells like <strong>bash</strong>, <strong>zsh</strong>, and <strong>PowerShell</strong> on Windows, MacOS, and Linux.  The environment is activated by calling <code>. ./envr.ps1</code></p>
<pre><code class="lang-ini"><span class="hljs-section">[PROJECT_OPTIONS]</span>
<span class="hljs-attr">PROJECT_NAME</span>=jpsapp
<span class="hljs-attr">PYTHON_VENV</span>=.venv
</code></pre>
<h3 id="heading-pyprojecttoml"><code>pyproject.toml</code></h3>
<p><a target="_blank" href="https://peps.python.org/pep-0621/">PEP 621</a> introduced the <code>pyproject.toml</code> standard for declaring common metadata, replacing the need for <code>requirements.txt</code> and most other configuration files.</p>
<pre><code class="lang-toml"><span class="hljs-section">[build-system]</span>
<span class="hljs-attr">requires</span> = [
    <span class="hljs-string">"setuptools&gt;=70.0"</span>,
]
<span class="hljs-attr">build-backend</span> = <span class="hljs-string">"setuptools.build_meta"</span>

<span class="hljs-section">[project]</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">"jpsapp"</span>
<span class="hljs-attr">version</span> = <span class="hljs-string">"1.1.6"</span>
<span class="hljs-attr">description</span> = <span class="hljs-string">"An example of Python application distribution."</span>
<span class="hljs-attr">authors</span> = [
    { name = <span class="hljs-string">"JP Hutchins"</span>, email = <span class="hljs-string">"jphutchins@gmail.com"</span> },
]
<span class="hljs-attr">readme</span> = <span class="hljs-string">"README.md"</span>
<span class="hljs-attr">license</span> = { file = <span class="hljs-string">"LICENSE"</span> }
<span class="hljs-attr">requires-python</span> = <span class="hljs-string">"&gt;=3.8"</span>
<span class="hljs-attr">classifiers</span> = [
    <span class="hljs-string">"Development Status :: 4 - Beta"</span>,
    <span class="hljs-string">"Intended Audience :: Developers"</span>,
    <span class="hljs-string">"Topic :: Software Development :: Build Tools"</span>,
]

<span class="hljs-attr">dependencies</span> = [
    <span class="hljs-comment"># Add your project dependencies here</span>
]

<span class="hljs-section">[project.optional-dependencies]</span>
<span class="hljs-attr">dev</span> = [
    <span class="hljs-string">"build&gt;=1.2.1,&lt;2"</span>,
    <span class="hljs-string">"pyinstaller&gt;=6.4.0,&lt;7"</span>,
    <span class="hljs-string">"pyinstaller-versionfile&gt;=2.1.1,&lt;3"</span>,
]

<span class="hljs-section">[project.scripts]</span>
<span class="hljs-attr">jpsapp</span> = <span class="hljs-string">"jpsapp.main:app"</span>

<span class="hljs-section">[project.urls]</span>
<span class="hljs-attr">Homepage</span> = <span class="hljs-string">"https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng"</span>
<span class="hljs-attr">Repository</span> = <span class="hljs-string">"https://github.com/JPhutchins/python-distribution-example.git"</span>

<span class="hljs-section">[tool.setuptools]</span>
<span class="hljs-attr">packages</span> = [<span class="hljs-string">"jpsapp"</span>]
<span class="hljs-attr">include-package-data</span> = <span class="hljs-literal">true</span>
</code></pre>
<p>For a detailed explanation of the <code>pyproject.toml</code>, refer to the <a target="_blank" href="https://packaging.python.org/en/latest/guides/writing-pyproject-toml/">Python Packaging User Guide</a>.  Here are a few interesting features of our example configuration.</p>
<p><code>version = "1.1.6"</code> will version the python package and the eventual app.  This
line in the configuration is the Single Source Of Truth for the version.  There
are many tools available to establish a Git tag as the SSOT, if you prefer.</p>
<p><code>packages = ["jpsapp"]</code> declares that <code>jpsapp</code> is the only module we are packaging.  This allows more Python modules to be added to the root of the repository, such as the tooling in the <code>distrubution</code> folder, that we wouldn't want to package for distribution.</p>
<p><code>jpsapp = "jpsapp.main:app"</code> declares that the command <code>jpsapp</code> will execute the <code>app</code> function from <code>jpsapp.main</code>.</p>
<h2 id="heading-dependencies">Dependencies</h2>
<h3 id="heading-python">Python</h3>
<p>If you have Python &gt;=3.8 go ahead and use that.  If not, install the most recent Python release for your system.  There are many ways to do so, but I'll briefly offer my <em>opinion</em>:</p>
<ul>
<li>Windows: use the Microsoft Store or <code>winget</code> and take advantage of "App Execution Aliases".  Whatever you do, make sure that both <code>python</code> and <code>python3</code> call the Python you want, none of this <code>py</code> nonsense!</li>
<li>Linux: use your package manager, and maybe <a target="_blank" href="https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa">deadsnakes</a> if you're on Ubuntu since they don't keep their Python packages current.</li>
</ul>
<h2 id="heading-build-the-app">Build the App</h2>
<p>Now that you have <a target="_blank" href="https://github.com/JPHutchins/python-distribution-example">cloned the repository</a> and installed the <a class="post-section-overview" href="#dependencies">dependencies</a>, you can build and run the application.</p>
<ul>
<li><code>python3 -m venv .venv</code>: on this first run it will create the venv at <code>.venv</code><ul>
<li>If <code>python3</code> is not an alias to the version of Python 3 you'd like to use then
update the command accordingly, e.g. <code>python -m venv .venv</code>.</li>
</ul>
</li>
<li><code>. ./envr.ps1</code>: activate the development environment</li>
<li><code>pip install --require-virtualenv -e .[dev]</code>: install the development dependencies</li>
</ul>
<p>And that's it!  <code>jpsapp</code> should print "Hello, World!".  Keep in mind that you can get the same execution with <code>python -m jpsapp</code>, <code>python -m jpsapp.main</code>, or <code>python jpsapp/main.py</code>, etc.</p>
<p>To build the Python package distributions, simply run <code>python -m build</code>.  The Python <code>.whl</code> and <code>.tar.gz</code> packages will be built at <code>dist/</code>, e.g. <code>dist/jpsapp-1.0.0.tar.gz</code>.</p>
<p>In the <a target="_blank" href="https://blog.jphutchins.com/github-action-for-the-python-package-index?source=more_series_bottom_blogs">next part of this series</a>, we will use a GitHub Workflow to release the package distribution to the PyPI so that other users can install your app with <code>pipx</code>!</p>
<h2 id="heading-footnotes">Footnotes</h2>
<ol>
<li><a id="fn-footnotes-1"><a class="post-section-overview" href="#fnr-footnotes-1">\^</a></a> <a target="_blank" href="https://en.wikipedia.org/wiki/Portable_application">"Portable application"</a>. Wikipedia.com. Retrieved 2024-03-11.  </li>
<li><a id="fn-footnotes-2"><a class="post-section-overview" href="#fnr-footnotes-2">\^</a></a> <a target="_blank" href="https://gs.statcounter.com/os-market-share/desktop/worldwide">"OS Market Share"</a>. GS.Statcounter.com. Retrieved 2024-03-11.</li>
</ol>
<h2 id="heading-change-history">Change History</h2>
<ul>
<li>2024-04-14: change <code>myapp</code> -&gt; <code>yourapp</code></li>
<li>2024-04-18: change <code>yourapp</code> -&gt; <code>jpsapp</code></li>
<li>2024-06-08: remove poetry; update pyproject.toml; update steps</li>
</ul>
]]></content:encoded></item></channel></rss>