<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: JP Hutchins</title>
    <description>The latest articles on DEV Community by JP Hutchins (@jphutchins).</description>
    <link>https://dev.to/jphutchins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F535749%2Fa4bc685c-4a69-4904-90d8-9e0b3abf2852.jpg</url>
      <title>DEV Community: JP Hutchins</title>
      <link>https://dev.to/jphutchins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jphutchins"/>
    <language>en</language>
    <item>
      <title>The Fastest Python Struct?</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Mon, 22 Jun 2026 01:33:57 +0000</pubDate>
      <link>https://dev.to/jphutchins/the-fastest-python-struct-en3</link>
      <guid>https://dev.to/jphutchins/the-fastest-python-struct-en3</guid>
      <description>&lt;p&gt;Python is &lt;em&gt;fast enough&lt;/em&gt;. Python programmers tend to understand the &lt;a href="https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/pages/readings/python-cost-model/" rel="noopener noreferrer"&gt;Python Cost Model&lt;/a&gt;, Python's strengths and weaknesses, libraries that give compiled performance, and when to use a compiled language from the start.&lt;/p&gt;

&lt;p&gt;So why do I care? Why do I get obsessed enough to coerce Claude into running these benchmarks and writing these Plotly charts? I do not know.&lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;But! I do know &lt;em&gt;what&lt;/em&gt; I care about (for now) - and today (and some of the past weekend, and perhaps some of the next one), it's definitely the &lt;strong&gt;&lt;em&gt;cost of defining (ideally immutable) record types (AKA structs) in Python&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So let's get this out of the way: this write up is about benchmarking "Python type speed" (informally: compile-time), it is NOT about benchmarking&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;serialization&lt;/li&gt;
&lt;li&gt;instantiation&lt;/li&gt;
&lt;li&gt;attribute access&lt;/li&gt;
&lt;li&gt;validation&lt;/li&gt;
&lt;li&gt;memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Right, so that's what Python programmers often care about, because they are probably working on long running programs, like apps, servers or pipelines, where the cost of defining a &lt;strong&gt;type&lt;/strong&gt; is paid upfront, one time, whereas the cost of allocation, instantiation, validation, and serialization is paid repeatedly. So yeah, if that's what you care about, this post is not for you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But I did include &lt;a href="https://www.crumpledpaper.tech/2026-06-21-python-struct-profiling/#instance-cost" rel="noopener noreferrer"&gt;instance cost&lt;/a&gt; benchmarks if you're curious. 😻&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you already know you care about type definition speed, then jump straight to the &lt;a href="https://www.crumpledpaper.tech/2026-06-21-python-struct-profiling/#structs-under-test-suts" rel="noopener noreferrer"&gt;analysis&lt;/a&gt;, otherwise keep reading for my motivation and context on this subject.&lt;/p&gt;

&lt;h2&gt;
  
  
  "how fast to &lt;code&gt;--help&lt;/code&gt;"
&lt;/h2&gt;

&lt;p&gt;I tend to work on CLIs for developers and tooling for build systems or test suites where the time from program start to end is what we're measuring. Perhaps you've noticed that running a command from a CLI may be near instant in a compiled program, but in Python, it can easily be hundreds of milliseconds: perceptible for UX, noticeable in CI/CD, and amplified by repeated calls as part of build system tooling.&lt;/p&gt;

&lt;p&gt;Unlike in a compiled language, Python type definitions are not free (free in the sense that they were paid for during compilation &lt;em&gt;ahem, Rust&lt;/em&gt;). They are code to be executed on every startup. And that includes imports of libraries and their type trees and dependents trees. We'll see in the benchmarks that (evil-runtime-) metaprogramming, like decorators, metaclasses, or worse, have more of an upfront &lt;em&gt;runtime type generation&lt;/em&gt; cost than manual type definitions.&lt;/p&gt;

&lt;p&gt;Can we get the best of everything: a Pythonic type definition style, complete static typing and &lt;a href="https://docs.python.org/3/reference/datamodel.html#object.__match_args__" rel="noopener noreferrer"&gt;match&lt;/a&gt;, with the speed of a hand-written C struct, and the startup time of a compiled extension? I &lt;em&gt;think&lt;/em&gt; so. &lt;sup&gt;&lt;em&gt;seriously, I'm not sure, need to do more work, but I have good preliminary data&lt;/em&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.crumpledpaper.tech/2026-06-21-python-struct-profiling/#how-fast-to---help" rel="noopener noreferrer"&gt;Continue reading at crumpledpaper.tech...&lt;/a&gt;&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;If you have any ideas, please LMK so I can explain it to my family.&amp;nbsp;↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>python</category>
      <category>c</category>
      <category>functional</category>
      <category>performance</category>
    </item>
    <item>
      <title>Static Website Comments Section</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Mon, 22 Jun 2026 01:13:50 +0000</pubDate>
      <link>https://dev.to/jphutchins/static-website-comments-section-5118</link>
      <guid>https://dev.to/jphutchins/static-website-comments-section-5118</guid>
      <description>&lt;p&gt;I had read about &lt;a href="https://indieweb.org/" rel="noopener noreferrer"&gt;IndieWeb&lt;/a&gt;, &lt;a href="https://webmention.io/" rel="noopener noreferrer"&gt;webmentions&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/Fediverse" rel="noopener noreferrer"&gt;Fediverse&lt;/a&gt; a few years ago and was very excited about it! It's a monumental under taking and a great example of good (read: ethical) tech.&lt;/p&gt;

&lt;p&gt;But I am not a web developer 🤣, and while I can appreciate what the engineers have accomplished, I was never too sure how to get it going, end-to-end.&lt;/p&gt;

&lt;p&gt;I've been burning quite a few tokens while LLMs are still basically "free" (&lt;a href="https://she-llac.com/claude-limits?ref=wheresyoured.at" rel="noopener noreferrer"&gt;see shellac's analysis&lt;/a&gt;), so I decided to let Claude Code slop together a plan from the following resources, particularly the posts by engineers that have implemented for their own static sites.&lt;/p&gt;

&lt;p&gt;You can see this site's &lt;a href="https://github.com/JPHutchins/interstellar-inclination" rel="noopener noreferrer"&gt;source&lt;/a&gt; to see how the implementation went. I suppose it's not tested until this post goes live and I see it federated.&lt;/p&gt;

&lt;p&gt;Update: it's working!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!WARNING] LLM Disclosure&lt;/p&gt;

&lt;p&gt;The following citations were assembled from the same &lt;code&gt;claude-opus-4-8&lt;/code&gt; context that provided the webmentions + giscus implementation for this blog. The implementation is the result of my feeding in a subset of those articles and iterating until I approved the design and eventually the implementation itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Bridgy Fed &amp;amp; federation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fed.brid.gy/docs" rel="noopener noreferrer"&gt;Bridgy Fed docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/snarfed/bridgy-fed/blob/main/FEDERATION.md" rel="noopener noreferrer"&gt;bridgy-fed FEDERATION.md&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/snarfed/bridgy-fed" rel="noopener noreferrer"&gt;snarfed/bridgy-fed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/Bridgy_Fed" rel="noopener noreferrer"&gt;IndieWeb: Bridgy Fed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Webmentions (receiving replies)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webmention.io/" rel="noopener noreferrer"&gt;webmention.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aaronpk/webmention.io" rel="noopener noreferrer"&gt;webmention.io API README&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/aaronpk/webmention.io/issues/126" rel="noopener noreferrer"&gt;webmention.io #126 — avatar rehosting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/Webmention" rel="noopener noreferrer"&gt;IndieWeb: Webmention&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Updating webmentions on a static site
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://nicolas-hoizey.com/articles/2023/02/05/updating-webmentions-on-a-static-site/" rel="noopener noreferrer"&gt;Nicolas Hoizey — Updating webmentions on a static site&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sebastiandedeyne.com/webmentions-on-a-static-site-with-github-actions/" rel="noopener noreferrer"&gt;Sebastian De Deyne — Webmentions with GitHub Actions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://mxb.dev/blog/using-webmentions-on-static-sites/" rel="noopener noreferrer"&gt;Max Böck — Using webmentions on static sites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://janmonschke.com/adding-webmentions-to-your-static-blog/" rel="noopener noreferrer"&gt;Jan Monschke — Adding webmentions to your static blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Microformats2 (h-card / h-entry / h-feed)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/representative_h-card" rel="noopener noreferrer"&gt;representative h-card (indieweb)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://microformats.org/wiki/representative-h-card" rel="noopener noreferrer"&gt;representative h-card (microformats.org)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/h-entry" rel="noopener noreferrer"&gt;h-entry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/h-feed" rel="noopener noreferrer"&gt;h-feed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indieweb.org/h-card" rel="noopener noreferrer"&gt;h-card&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Bluesky / AT Protocol (custom-handle verification)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle" rel="noopener noreferrer"&gt;com.atproto.identity.resolveHandle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://plc.directory/" rel="noopener noreferrer"&gt;PLC Directory&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed" rel="noopener noreferrer"&gt;app.bsky.feed.getAuthorFeed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Feed discovery
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.astro.build/en/recipes/rss/" rel="noopener noreferrer"&gt;Astro RSS recipe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jpmonette/feed" rel="noopener noreferrer"&gt;feed (jpmonette/feed)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Refresh-workflow plumbing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/actions/concepts/security/github_token" rel="noopener noreferrer"&gt;GITHUB_TOKEN no-recursion rule&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/actions/checkout/issues/1550" rel="noopener noreferrer"&gt;actions/checkout #1550 — sparse-checkout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/orgs/community/discussions/25702" rel="noopener noreferrer"&gt;community #25702 — push-from-action triggering&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  GitHub-comments fallback &amp;amp; safety
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/giscus/giscus" rel="noopener noreferrer"&gt;giscus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/giscus/giscus/blob/main/ADVANCED-USAGE.md" rel="noopener noreferrer"&gt;giscus ADVANCED-USAGE (theme postMessage)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/sanitize-html" rel="noopener noreferrer"&gt;sanitize-html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Landscape / inspiration
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://cassidyjames.com/blog/fediverse-blog-comments-mastodon/" rel="noopener noreferrer"&gt;Cassidy James — Mastodon-powered blog comments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gregnewman.io/blog/mastodon-comments-in-astrojs/" rel="noopener noreferrer"&gt;Greg Newman — Mastodon comments in Astro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/zerok/retoots" rel="noopener noreferrer"&gt;zerok/retoots&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://danmackinlay.name/notebook/static_site_comments.html" rel="noopener noreferrer"&gt;Dan MacKinlay — Comment systems for static websites&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://darekkay.com/blog/static-site-comments/" rel="noopener noreferrer"&gt;Darek Kay — Static site comments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>indieweb</category>
      <category>fediverse</category>
      <category>bluesky</category>
      <category>github</category>
    </item>
    <item>
      <title>Enum Type Safety in C</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Mon, 22 Jun 2026 01:09:47 +0000</pubDate>
      <link>https://dev.to/jphutchins/enum-type-safety-in-c-1no</link>
      <guid>https://dev.to/jphutchins/enum-type-safety-in-c-1no</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  C Enums
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For example, an enumeration of &lt;strong&gt;colors&lt;/strong&gt; might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_RED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_GREEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_BLUE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of 0 being given to red, 1 to green, and 2 to blue is &lt;em&gt;arbitrary&lt;/em&gt;—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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_RED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_GREEN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// automatically 1&lt;/span&gt;
    &lt;span class="n"&gt;COLOR_BLUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// automatically 2&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is simpler, and in the case where new symbols may appear anywhere in the definition, it is &lt;em&gt;preferred&lt;/em&gt;, because it will prevent accidental creation of shared values when unique values are required.&lt;/p&gt;

&lt;p&gt;On the other hand, an enumeration of &lt;strong&gt;speed limits&lt;/strong&gt; may map to constant integer values that are useful and not-at-all arbitrary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;speed_limit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SPEED_LIMIT_30_KPH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SPEED_LIMIT_60_KPH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
    &lt;span class="n"&gt;SPEED_LIMIT_90_KPH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;FONT_TIMES_NEW_ROMAN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FONT_HELVETICA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FONT_DEFAULT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FONT_HELVETICA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// instead of 1&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;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 &lt;code&gt;enum color::RED&lt;/code&gt;. Don't dismay! More sophisticated strategies to enforce type safety are the subject of this very article.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The C Standard
&lt;/h3&gt;

&lt;p&gt;The C standard states that enumerated types (enums) that are defined by the programmer are unique types.&lt;/p&gt;

&lt;p&gt;In the draft version (&lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf" rel="noopener noreferrer"&gt;ISO/IEC 9899:2024 (en) — N3220&lt;/a&gt;) of C23, we find a definition for C enumerations:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An enumeration comprises a set of named integer constant values. Each distinct enumeration&lt;br&gt;
constitutes a different enumerated type.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf" rel="noopener noreferrer"&gt;N3220&lt;/a&gt;, &lt;strong&gt;6.2.5 Types&lt;/strong&gt; (p. 40)&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's important to note that C23 enables specifying the underlying type of the enumeration, which is a &lt;a href="https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n3030---enhanced-enumerations" rel="noopener noreferrer"&gt;very good idea&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An identifier declared as an enumeration constant for an enumeration without a fixed underlying&lt;br&gt;
type has either type int or the enumerated type, as defined in 6․7.3.3. An identifier declared&lt;br&gt;
as an enumeration constant for an enumeration with a fixed underlying type has the associated&lt;br&gt;
enumerated type.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf" rel="noopener noreferrer"&gt;N3220&lt;/a&gt;, &lt;strong&gt;6․4․4․4 Enumeration constants&lt;/strong&gt; (p. 63)&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The definition of "enumerated type" from section *&lt;em&gt;6․7.3.3 Enumeration specifiers *&lt;/em&gt; (p. 109):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;2 All enumerations have an underlying type. The underlying type can be explicitly specified using an&lt;br&gt;
enum type specifier and is its fixed underlying type. If it is not explicitly specified, the underlying&lt;br&gt;
type is the enumeration’s compatible type, which is either char or a standard or extended signed or&lt;br&gt;
unsigned integer type.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf" rel="noopener noreferrer"&gt;N3220&lt;/a&gt;, &lt;strong&gt;6․7.3.3 Enumeration specifiers&lt;/strong&gt; (p. 109)&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So actually, it's probably &lt;code&gt;int&lt;/code&gt;, or maybe not, because it's &lt;em&gt;implementation-defined&lt;/em&gt; (i.e. up to the authors of GCC, Clang, etc.):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;13 For all enumerations without a fixed underlying type, each enumerated type shall be compatible&lt;br&gt;
with char or a signed or an unsigned integer type that is not bool or a bit-precise integer type. The&lt;br&gt;
choice of type is implementation-defined but shall be capable of representing the values of all&lt;br&gt;
the members of the enumeration.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf" rel="noopener noreferrer"&gt;N3220&lt;/a&gt;, &lt;strong&gt;6․7.3.3 Enumeration specifiers&lt;/strong&gt; (p. 109)&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The behavior is muddied further with compiler options like &lt;code&gt;-fshort-enums&lt;/code&gt; that will use the smallest possible integer representation for the enumeration's underlying type.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixed Underlying Type
&lt;/h3&gt;

&lt;p&gt;Fortunately, there is no (good) reason to use &lt;code&gt;-fshort-enums&lt;/code&gt; since C23 allows specifying a fixed underlying type for each enumerated type definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;eight_bit&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;HEX00&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x00&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;HEX01&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;eight_bit&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;HEX100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x100&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GCC 14.2.0 (unknown-eabi):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;:4:14: error: enumerator value outside the range of underlying type
    8 |  HEX100 = 0x100,
      |  ^~~~~
Compiler returned: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Type Safety
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The following examples were tested with two compilers: &lt;code&gt;ARM GCC 14.2.0 (unknown-eabi)&lt;/code&gt; and &lt;code&gt;armv7-a clang 18.1.0&lt;/code&gt;. The compiler and program output is the same between the two compilers unless otherwise noted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exhaustiveness
&lt;/h3&gt;

&lt;p&gt;Generally, when an enum is used in a &lt;code&gt;switch&lt;/code&gt; statement, the intention is that all constants of the enumerated type are matched by a &lt;code&gt;case&lt;/code&gt;. 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 &lt;em&gt;exhaustive&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here's an example with a runtime error lurking—because the &lt;code&gt;switch&lt;/code&gt; is not exhaustive, it is possible to hit the &lt;code&gt;assert(0)&lt;/code&gt; after the &lt;code&gt;switch&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://godbolt.org/z/xd6W7ME6d" rel="noopener noreferrer"&gt;Interact with this example on Compiler Explorer&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EVENT_A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;EVENT_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;RESULT_A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RESULT_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="nf"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiler options: &lt;code&gt;-Werror&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiler returned: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Program output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program returned: 0
Program stdout
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;handle_event()&lt;/code&gt; function implies that it can handle all members of &lt;code&gt;enum event&lt;/code&gt;. In reality, the programmer has failed to match the &lt;code&gt;EVENT_B&lt;/code&gt; case, so passing &lt;code&gt;EVENT_B&lt;/code&gt;, or 1, as the function argument will cause a runtime error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiler options: &lt;code&gt;-Werror&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiler returned: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Program output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program returned: 139
Program stderr
output.s: /app/example.c:20: handle_event: Assertion `0' failed.
Program terminated with signal: SIGSEGV
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fortunately, simply adding &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall" rel="noopener noreferrer"&gt;&lt;code&gt;-Wall&lt;/code&gt;&lt;/a&gt; covers this common mistake (&lt;code&gt;-Wextra&lt;/code&gt; is added for completeness).&lt;/p&gt;

&lt;p&gt;GCC &lt;code&gt;-Werror -Wall -Wextra&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;: In function 'handle_event':
&amp;lt;source&amp;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clang &lt;code&gt;-Werror -Wall -Wextra&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;:16:13: error: enumeration value 'EVENT_B' not handled in switch [-Werror,-Wswitch]
   16 |  switch (event) {
      | ^~~~~
1 error generated.
Compiler returned: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Importantly, we can reintroduce this &lt;strong&gt;runtime bug by adding a &lt;code&gt;default:&lt;/code&gt; case&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="nf"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;default:&lt;/span&gt;
            &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiler options &lt;code&gt;-Werror -Wall -Wextra&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiler returned: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The conclusion is that the use of a &lt;code&gt;default:&lt;/code&gt; case in the &lt;code&gt;switch&lt;/code&gt; statement is counterproductive to type safety.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Checking
&lt;/h3&gt;

&lt;p&gt;By adding &lt;code&gt;-Wall&lt;/code&gt; and avoiding use of a &lt;code&gt;default:&lt;/code&gt; case, the &lt;code&gt;switch&lt;/code&gt; statement over the &lt;code&gt;enum event&lt;/code&gt; is &lt;em&gt;exhaustive&lt;/em&gt;, therefore it is impossible for a runtime error to occur when the argument given to &lt;code&gt;handle_event()&lt;/code&gt; is guaranteed to be an &lt;code&gt;enum event&lt;/code&gt;. However, because  the GCC and Clang compilers do not strictly check the type given to &lt;code&gt;handle_event()&lt;/code&gt;, the program remains vulnerable to other run time errors.&lt;/p&gt;

&lt;p&gt;In this example, &lt;code&gt;2&lt;/code&gt; is passed to &lt;code&gt;handle_event()&lt;/code&gt; without generating a compile time error. At run time, it is found that &lt;code&gt;2&lt;/code&gt; is not handled by the &lt;code&gt;switch&lt;/code&gt;, causing the assertion to be hit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://godbolt.org/z/5KMTrjeaM" rel="noopener noreferrer"&gt;Interact with this example on Compiler Explorer&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;assert.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EVENT_A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;EVENT_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;RESULT_A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RESULT_B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="nf"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compiler options: &lt;code&gt;-Werror -Wall -Wextra&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compiler returned: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Program output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Program returned: 139
Program stderr
output.s: /app/example.c:22: handle_event: Assertion `0' failed.
Program terminated with signal: SIGSEGV
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-W" rel="noopener noreferrer"&gt;&lt;code&gt;-Wextra&lt;/code&gt;&lt;/a&gt; has added &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-conversion" rel="noopener noreferrer"&gt;&lt;code&gt;-Wenum-conversion&lt;/code&gt;&lt;/a&gt;, but it doesn't quite do what we want:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warn when a value of enumerated type is implicitly converted to a different enumerated type. This warning is enabled by -Wextra in C.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-conversion" rel="noopener noreferrer"&gt;GNU.org&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;-Wenum-conversion&lt;/code&gt; will generate a warning if we provide an incompatible &lt;code&gt;enum&lt;/code&gt; as argument, even though both the underlying type and value are the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;    &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RESULT_A&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GCC &lt;code&gt;-Werror -Wall -Wextra&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;:26:39: error: implicit conversion from 'enum result' to 'enum event' [-Werror=enum-conversion]
   26 |  enum result result = handle_event(RESULT_A);
      |   ^~~~~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, what we're looking for is an error caused by passing the underlying type—the integer literal &lt;code&gt;2&lt;/code&gt;, in the example—instead of the enumerated type as argument. It is provided by &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall" rel="noopener noreferrer"&gt;&lt;code&gt;-Wall&lt;/code&gt;&lt;/a&gt; in the form of &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-int-mismatch" rel="noopener noreferrer"&gt;&lt;code&gt;Wenum-int-mismatch&lt;/code&gt;&lt;/a&gt;, but there is a catch in the fine print:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warn about mismatches between an enumerated type and an integer type in declarations...&lt;/p&gt;



&lt;p&gt;...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.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wenum-int-mismatch" rel="noopener noreferrer"&gt;GNU.org&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, in GCC, we need &lt;code&gt;-Wc++-compat&lt;/code&gt; for the desired compile time type safety.&lt;/p&gt;

&lt;p&gt;GCC &lt;code&gt;-Werror -Wall -Wextra -Wc++-compat&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;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);
      |   ^
&amp;lt;source&amp;gt;:15:13: note: expected 'enum event' but argument is of type 'int'
   15 | enum result handle_event(enum event event) {
      | ^~~~~~~~~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clang's &lt;a href="https://clang.llvm.org/docs/DiagnosticsReference.html#wassign-enum" rel="noopener noreferrer"&gt;&lt;code&gt;-Wassign-enum&lt;/code&gt;&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;Clang &lt;code&gt;-Werror -Wall -Wextra -Wassign-enum&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source&amp;gt;:26:39: error: integer constant not in range of enumerated type 'enum event' [-Werror,-Wassign-enum]
   26 |  enum result result = handle_event(2);
      |   ^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Clang C++ &lt;code&gt;armv7-a clang 18.1.0&lt;/code&gt; (no options needed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;source&amp;gt;:26:26: error: no matching function for call to 'handle_event'
   26 |  enum result result = handle_event(1);
      |  ^~~~~~~~~~~~
&amp;lt;source&amp;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) {
      | ^        ~~~~~~~~~~~~~~~~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the integer literal is in range of the enum, use of the integer literal still causes a compile time error as desired.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!TIP] Casting&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;



&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;By avoiding use of a &lt;code&gt;default&lt;/code&gt; case and adding the &lt;code&gt;-Werror -Wall -Wextra -Wc++-compat&lt;/code&gt; compiler options, it is &lt;strong&gt;"impossible to generate a run time error"&lt;/strong&gt;. Yet, if that is true, then it raises a question about&lt;br&gt;
the necessity of runtime type checking.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[!CAUTION]&lt;/p&gt;

&lt;p&gt;If a run time error is not possible, can &lt;code&gt;assert(0)&lt;/code&gt; be replaced with &lt;code&gt;__builtin_unreachable()&lt;/code&gt;?&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="nf"&gt;handle_event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_A&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;EVENT_B&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;RESULT_B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;__builtin_unreachable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It certainly &lt;a href="https://godbolt.org/z/Y39hGx5va" rel="noopener noreferrer"&gt;compiles&lt;/a&gt; and results in smaller code size. But, without the set of compiler options &lt;code&gt;-Werror -Wall -Wextra -Wc++-compat&lt;/code&gt;, this is precariously vulnerable to &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable" rel="noopener noreferrer"&gt;undefined behavior&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If control flow reaches the point of the __builtin_unreachable, the program is undefined.&lt;br&gt;
&lt;cite&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable" rel="noopener noreferrer"&gt;GNU.org&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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 &lt;code&gt;__builtin_unreachable()&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;You can fiddle with the final type safe example at &lt;a href="https://godbolt.org/z/3b89zcasx" rel="noopener noreferrer"&gt;Compiler Explorer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>c</category>
      <category>functional</category>
      <category>compiling</category>
      <category>firmware</category>
    </item>
    <item>
      <title>GitHub Release Action for the Python Package Index</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Sat, 08 Jun 2024 22:39:58 +0000</pubDate>
      <link>https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n</link>
      <guid>https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mshzty71qs7nbw53syx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mshzty71qs7nbw53syx.png" alt="workflow screenshot" width="800" height="88"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng"&gt;first part of this series&lt;/a&gt;, we set up a repository for a universally portable Python app.  Today, we will register the package with &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI, the Python Package Index&lt;/a&gt;, and use a GitHub Release Action to automate the distribution so that other Python users can install the app with &lt;code&gt;pipx&lt;/code&gt; or &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/features/actions" rel="noopener noreferrer"&gt;GitHub Actions&lt;/a&gt; are automated routines that run on GitHub's sandboxed virtual machine servers, called "runners", and are (&lt;a href="https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions" rel="noopener noreferrer"&gt;probably&lt;/a&gt;) free for your Public open source projects!&lt;/p&gt;

&lt;h2&gt;
  
  
  Security
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An attacker poses as a contributor and merges malicious code to your package via a &lt;strong&gt;Pull Request&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;An attacker hacks PyPI so that when a user tries to install your app they install a malicious package instead.&lt;/li&gt;
&lt;li&gt;An attacker logs in to your &lt;strong&gt;GitHub account&lt;/strong&gt; and replaces your app's repository with malicious code or uses a leaked &lt;strong&gt;Personal Access Token (PAT)&lt;/strong&gt; or &lt;strong&gt;Secure Shell (SSH) key&lt;/strong&gt; to push directly to the repository.&lt;/li&gt;
&lt;li&gt;An attacker logs in to your &lt;strong&gt;PyPI account&lt;/strong&gt; and replaces your package with malicious code.&lt;/li&gt;
&lt;li&gt;An attacker creates a malicious Python package with the &lt;strong&gt;same name&lt;/strong&gt; as yours and distributes it outside of PyPI.&lt;/li&gt;
&lt;li&gt;An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to &lt;strong&gt;trick users into downloading the wrong package&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Reviewing Pull Requests
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker poses as a contributor and merges malicious code to your package via a &lt;strong&gt;Pull Request&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;sup id="fnr-footnotes-1"&gt;1&lt;/sup&gt;   If you believe that a PR is attempting to inject a security vulnerability in your app, then you should &lt;a href="https://docs.github.com/en/communities/maintaining-your-safety-on-github/reporting-abuse-or-spam#reporting-an-issue-or-pull-request" rel="noopener noreferrer"&gt;report the offending account&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Vulnerability in the Package Repository (PyPI)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker hacks PyPI so that when a user tries to install your app, they install a malicious package instead.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to Stack Overflow's 2023 Developer Survey, &lt;strong&gt;45.32% of professional developers use Python&lt;/strong&gt;.&lt;sup id="fnr-footnotes-2"&gt;2&lt;/sup&gt;  Every industry and government in the world would be impacted by this threat and therefore has a financial incentive to keep PyPI secure.&lt;/p&gt;

&lt;p&gt;PyPI completed a security audit in late 2023 that found and remediated some non-critical security risks.&lt;sup id="fnr-footnotes-3"&gt;3&lt;/sup&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;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 &lt;em&gt;authentic&lt;/em&gt;.  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 &lt;em&gt;you&lt;/em&gt;.  Legal action can be taken against &lt;em&gt;you&lt;/em&gt;, which is a good reason not to distribute malware on PyPI.&lt;/p&gt;

&lt;p&gt;So, how can we prove that the software that a user receives when they type &lt;code&gt;pipx install jpsapp&lt;/code&gt; is authentic?  Let's look at each threat individually.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Protecting Your GitHub Account
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker logs in to your &lt;strong&gt;GitHub account&lt;/strong&gt; and replaces your app's repository with malicious code or uses a leaked &lt;strong&gt;Personal Access Token (PAT)&lt;/strong&gt; or &lt;strong&gt;Secure Shell (SSH) key&lt;/strong&gt; to push directly to the repository.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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 &lt;a href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/" rel="noopener noreferrer"&gt;password manager&lt;/a&gt;) and use two-factor authentication (2FA).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h4&gt;
  
  
  SSH Keys
&lt;/h4&gt;

&lt;p&gt;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 &lt;strong&gt;especially not as a commit to a remote repository&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/reviewing-your-ssh-keys" rel="noopener noreferrer"&gt;going to GitHub and revoking any SSH keys&lt;/a&gt; from the compromised computer.&lt;/p&gt;

&lt;h4&gt;
  
  
  Personal Access Tokens
&lt;/h4&gt;

&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Protecting your PyPI Account
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker logs in to your &lt;strong&gt;PyPI account&lt;/strong&gt; and replaces your package with malicious code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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 &lt;a href="https://bitwarden.com/resources/why-enterprises-need-a-password-manager/" rel="noopener noreferrer"&gt;password manager&lt;/a&gt;) and use two-factor authentication (2FA).&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Package Impersonation
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker creates a malicious Python package with the &lt;strong&gt;same name&lt;/strong&gt; as yours and distributes it outside of PyPI.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By default, tools like &lt;code&gt;pip&lt;/code&gt; and &lt;code&gt;pipx&lt;/code&gt; will search PyPI for the package specified.  To install a package from an outside source, &lt;code&gt;pip&lt;/code&gt; would need to be told explicitly to point to the location of the infected package.&lt;/p&gt;

&lt;p&gt;From the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install https://m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or in &lt;code&gt;pyproject.toml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;jpsapp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://m.piqy.org/packages/ac/1d/jpsapp-1.0.0.tar.gz"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;README.md&lt;/code&gt; and other documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;## Install&lt;/span&gt;

&lt;span class="sb"&gt;`jpsapp`&lt;/span&gt; is &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;distributed by PyPI&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://pypi.org/project/jpsapp/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
and can be installed with &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;pipx&lt;/span&gt;&lt;span class="p"&gt;](&lt;/span&gt;&lt;span class="sx"&gt;https://github.com/pypa/pipx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;:
&lt;span class="p"&gt;```&lt;/span&gt;&lt;span class="nl"&gt;
&lt;/span&gt;pipx install jpsapp
&lt;span class="p"&gt;```&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Typo Squatting
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Threat: An attacker uploads a malicious Python package to PyPI with a name that is similar to yours ("typo squatting"), the intention being to &lt;strong&gt;trick users into downloading the wrong package&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, a user intending to install &lt;code&gt;matplotlib&lt;/code&gt; may make the typo &lt;code&gt;matplotli&lt;/code&gt; and accidentally install the wrong package.  In a sophisticated supply chain attack, the &lt;code&gt;matlplotli&lt;/code&gt; package would be mostly identical to the latest &lt;code&gt;matplotlib&lt;/code&gt; package with the only differences being obfuscated malware installation and execution.&lt;/p&gt;

&lt;h4&gt;
  
  
  Protect Yourself
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Protect Your Users
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fag40abvp5kc1xv65nrag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fag40abvp5kc1xv65nrag.png" alt="Screenshot of GitHub Adding a " width="471" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make certain that the command you've added to the documentation is runnable as written.  For example, adding prefixes like &lt;code&gt;$&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; would make your command example unusable without modification and subsequently reintroduce the typo squatting threat.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Supply Chain Attack
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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 &lt;em&gt;your dependencies&lt;/em&gt; fell victim to one of the threats described above.&lt;/p&gt;

&lt;p&gt;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 (🌈&lt;em&gt;maybe they have no bugs&lt;/em&gt;🌈) or low user counts.  Always research a variety of options that meet your requirements and double check that Python does not have a &lt;a href="https://docs.python.org/3/" rel="noopener noreferrer"&gt;builtin&lt;/a&gt; that works for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  Case Study: topggpy Supply Chain Attack
&lt;/h2&gt;

&lt;p&gt;On March 25th, 2024, Checkmarx broke the news of a successful supply chain attack that affected more than 170,000 Python users.&lt;/p&gt;

&lt;p&gt;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.&lt;sup id="fnr-footnotes-4"&gt;4&lt;/sup&gt;  I suggest reading the entire &lt;a href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/" rel="noopener noreferrer"&gt;article&lt;/a&gt; before returning here to see how every aspect of the attack would have been mitigated by the strategies discussed above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Protecting Your GitHub Account
&lt;/h3&gt;

&lt;p&gt;The primary failure for topggpy was editor-syntax's GitHub account being compromised.  Above, we discussed industry standard approaches utilizing password managers and two-factor authentication.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reviewing Pull Requests
&lt;/h3&gt;

&lt;p&gt;editor-syntax was not the account owner of the topggpy &lt;a href="https://github.com/Top-gg-Community/python-sdk" rel="noopener noreferrer"&gt;repository&lt;/a&gt; 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 &lt;a 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" rel="noopener noreferrer"&gt;restricting their permissions&lt;/a&gt; to the bare minimum required for their role.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/Top-gg-Community/python-sdk/commit/ecb87731286d72c8b8172db9671f74bd42c6c534" rel="noopener noreferrer"&gt;here&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="gd"&gt;- aiohttp&amp;gt;=3.6.0,&amp;lt;3.9.0
&lt;/span&gt;&lt;span class="gi"&gt;+ https://files.pythonhosted.org/packages/18/93/1f005bbe044471a0444a82cdd7356f5120b9cf94fe2c50c0cdbf28f1258b/aiohttp-3.9.3.tar.gz
+ https://files.pythonhosted.org/packages/7f/45/8ae61209bb9015f516102fa559a2914178da1d5868428bd86a1b4421141d/base58-2.1.1.tar.gz
+ https://files.pypihosted.org/packages/ow/55/4862e96575e3fda1dffd6cc46f648e787dd06d97/colorama-0.4.3.tar.gz
+ https://files.pythonhosted.org/packages/e0/b7/a4a032e94bcfdff481f2e6fecd472794d9da09f474a2185ed33b2c7cad64/construct-2.10.68.tar.gz
+ https://files.pythonhosted.org/packages/7e/86/2bd8fa8b63c91008c4f26fb2c7b4d661abf5a151db474e298e1c572caa57/DateTime-5.4.tar.gz
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case it loads the tainted &lt;code&gt;colorama&lt;/code&gt; package from a non-PyPI, typo-squatted domain, &lt;code&gt;files.pypihosted.org&lt;/code&gt;, that was registered by the attackers to impersonate authentic packages.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;files.pythonhosted.org&lt;/code&gt; and &lt;code&gt;pypi.org&lt;/code&gt; are both authentic PyPI domains.  As discussed in Package Impersonation, 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automated PyPI Publishing Tutorial
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create an Account at PyPI.org
&lt;/h3&gt;

&lt;p&gt;Create an account at &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI.org&lt;/a&gt; and remember to use a strong password that is secured by a password manager and enable 2FA, as discussed above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add a Trusted Publisher at PyPI.org
&lt;/h3&gt;

&lt;p&gt;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".&lt;/p&gt;

&lt;p&gt;Under the "GitHub" tab, fill out the fields following the example below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PyPI Project Name&lt;/strong&gt;: &lt;code&gt;jpsapp&lt;/code&gt;. Your package name as defined by the &lt;code&gt;name&lt;/code&gt; field of your &lt;code&gt;pyproject.toml&lt;/code&gt; and the directory name of the package.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Owner&lt;/strong&gt;: &lt;code&gt;JPHutchins&lt;/code&gt;. Your GitHub User or Organization name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repository name&lt;/strong&gt;: &lt;code&gt;python-distribution-example&lt;/code&gt;. The name of the repository as it is in the GitHub URL, e.g. &lt;code&gt;github.com/JPHutchins/python-distribution-example&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workflow name&lt;/strong&gt;: &lt;code&gt;release.yaml&lt;/code&gt;.  The workflow will be located at &lt;code&gt;.github/workflows/release.yaml&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment name&lt;/strong&gt;: &lt;code&gt;pypi&lt;/code&gt;.  We will configure this environment at github.com&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click "Add"&lt;/p&gt;

&lt;h3&gt;
  
  
  Define the Release Action
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Because this is free and open source software, there will be no fees for the cloud virtual machines provided by GitHub.&lt;/p&gt;

&lt;p&gt;All code snippets belong to the file &lt;code&gt;release.yaml&lt;/code&gt;, the complete version of which you can find &lt;a href=""&gt;here&lt;/a&gt;.  The original example is from &lt;a href="https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/" rel="noopener noreferrer"&gt;this excellent article&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be the name displayed in the GitHub Actions interface.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jpsapp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This adds a variable to the workflow, accessible via &lt;code&gt;${{ env.name }}&lt;/code&gt;.  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.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;release&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;published&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declares that the workflow should run whenever a new release is published.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything indented under the &lt;code&gt;jobs:&lt;/code&gt; section are the definitions of the actions to perform.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;build-dist&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;📦 Build distribution&lt;/span&gt; 
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare a job named &lt;code&gt;build-dist&lt;/code&gt; with friendly name "📦 Build distribution" that will run on an Ubuntu (Linux) runner.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything indented under the &lt;code&gt;steps:&lt;/code&gt; section are the steps to perform for this &lt;code&gt;job&lt;/code&gt;.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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 &lt;code&gt;run: git fetch --prune --unshallow --tags&lt;/code&gt; 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 &lt;a href="https://github.com/intercreate/smpmgr" rel="noopener noreferrer"&gt;smpmgr&lt;/a&gt;, for details.&lt;/p&gt;
&lt;/blockquote&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-python@v5&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;python-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3.x"&lt;/span&gt;
        &lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;pip&lt;/code&gt; in the next step, assuming that the dependencies have not changed since the cache was created.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pip install .[dev]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the development dependencies.  The workflow runs on a fresh Python environment, so we can simplify things somewhat by not using the venv.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python -m build&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Build the &lt;strong&gt;sdist&lt;/strong&gt; and &lt;strong&gt;wheel&lt;/strong&gt; 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.  &lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Store the distribution packages&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/upload-artifact@v4&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upload the sdist and wheel, which are located at &lt;code&gt;dist/&lt;/code&gt;, as artifacts so that they are available for download in the GitHub Actions interface and easily accessible from the next &lt;code&gt;job&lt;/code&gt; using &lt;code&gt;actions/download-artifact&lt;/code&gt;.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;publish-to-pypi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Publish Python 🐍 distribution 📦 to PyPI&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;build-dist&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pypi&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://pypi.org/p/${{ env.name }}&lt;/span&gt;
    &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for trusted publishing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declare another job for the Ubuntu runner, &lt;code&gt;publish-to-pypi&lt;/code&gt;, with the friendly name "Publish Python 🐍 distribution 📦 to PyPI", that runs after the job &lt;code&gt;build-dist&lt;/code&gt; has completed successfully.&lt;/p&gt;

&lt;p&gt;This job also uses the environment, &lt;code&gt;pypi&lt;/code&gt;, that we created earlier, and defines the &lt;code&gt;url&lt;/code&gt; variable in the environment.  The url, &lt;code&gt;https://pypi.org/p/${{ env.name }}&lt;/code&gt;, will resolve to &lt;code&gt;https://pypi.org/p/jpsapp&lt;/code&gt;, and will be used by the &lt;code&gt;pypa/gh-action-pypi-publish&lt;/code&gt; step later in this job.  You can read more about environments on the &lt;a href="https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment" rel="noopener noreferrer"&gt;GitHub Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, the job requires the &lt;code&gt;id-token: write&lt;/code&gt; permission to &lt;a href="https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect#adding-permissions-settings" rel="noopener noreferrer"&gt;allow the OpenID Connect (OIDC) JSON Web Token (JWT)&lt;/a&gt; to be requested from GitHub by the &lt;code&gt;pypa/gh-action-pypi-publish&lt;/code&gt; 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. &lt;br&gt;
 This kind of temporary token authentication prevents using your GitHub or PyPI account credentials directly, which could create an opportunity for them to leak.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Download all the dists&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v4&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
          &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Publish distribution 📦 to PyPI&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;pypa/gh-action-pypi-publish@release/v1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The first step downloads the dists that were built and uploaded by the &lt;code&gt;build-dist&lt;/code&gt; job and the second step uploads those dists to the Python Package Index.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;  &lt;span class="na"&gt;publish-dist-to-github&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
      &lt;span class="s"&gt;Sign the Python 🐍 distribution 📦 with Sigstore&lt;/span&gt;
      &lt;span class="s"&gt;and upload them to GitHub Release&lt;/span&gt;
    &lt;span class="na"&gt;needs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;publish-to-pypi&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;

    &lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for making GitHub Releases&lt;/span&gt;
      &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;  &lt;span class="c1"&gt;# IMPORTANT: mandatory for sigstore&lt;/span&gt;

    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Download all the dists&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/download-artifact@v4&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python-package-distributions&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Sign the dists with Sigstore&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;sigstore/gh-action-sigstore-python@v2.1.1&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
          &lt;span class="s"&gt;./dist/*.tar.gz&lt;/span&gt;
          &lt;span class="s"&gt;./dist/*.whl&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Upload artifact signatures to GitHub Release&lt;/span&gt;
      &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;GITHUB_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ github.token }}&lt;/span&gt;
      &lt;span class="c1"&gt;# Upload to GitHub Release using the `gh` CLI.&lt;/span&gt;
      &lt;span class="c1"&gt;# `dist/` contains the built packages, and the&lt;/span&gt;
      &lt;span class="c1"&gt;# sigstore-produced signatures and certificates.&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;&amp;gt;-&lt;/span&gt;
        &lt;span class="s"&gt;gh release upload&lt;/span&gt;
        &lt;span class="s"&gt;'${{ github.ref_name }}' dist/**&lt;/span&gt;
        &lt;span class="s"&gt;--repo '${{ github.repository }}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;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 &lt;code&gt;pipx&lt;/code&gt;, this does allow users to verify the authenticity of the dists that are hosted on the GitHub Release page using &lt;a href="https://www.python.org/download/sigstore/" rel="noopener noreferrer"&gt;instructions&lt;/a&gt; provided by Python.&lt;/p&gt;



&lt;p&gt;Take a look the complete &lt;a href="https://github.com/JPHutchins/python-distribution-example/blob/e31907bec7a31e8ef7edc1dd33dfb10b6c0f496b/.github/workflows/release.yaml#L1-L87" rel="noopener noreferrer"&gt;release.yaml&lt;/a&gt; and use it as a template for your own applications or libraries.&lt;/p&gt;
&lt;h3&gt;
  
  
  Create the Release
&lt;/h3&gt;

&lt;p&gt;All of the hard work of automating the PyPI release process is out of the way and now it's time to deploy!&lt;/p&gt;
&lt;h4&gt;
  
  
  About Versioning
&lt;/h4&gt;

&lt;p&gt;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 &lt;code&gt;pyproject.toml&lt;/code&gt;, while GitHub will want a Git tag for the release.  There may be many differences of opinion on &lt;em&gt;how&lt;/em&gt; to make sure that these match, but most will agree that &lt;strong&gt;these should match&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The simplest approach is to make a commit that bumps the version in &lt;code&gt;pyproject.toml&lt;/code&gt; with a commit message like "version: 1.0.1".  Immediately follow that up with a git tag that matches: &lt;code&gt;git tag 1.0.1&lt;/code&gt; and &lt;code&gt;git push --tags&lt;/code&gt; (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.&lt;/p&gt;

&lt;p&gt;For that reason, many approaches for establishing a SSOT have been developed, and you may find one that you prefer.  Some examples are the &lt;a href="https://github.com/tiangolo/poetry-version-plugin" rel="noopener noreferrer"&gt;poetry-version-plugin&lt;/a&gt; and &lt;a href="https://setuptools-git-versioning.readthedocs.io/en/v2.0.0/" rel="noopener noreferrer"&gt;setuptools-git-versioning&lt;/a&gt;.  &lt;a href="https://github.com/gitpython-developers/GitPython" rel="noopener noreferrer"&gt;GitPython&lt;/a&gt; can be used to enforce strict release rules.  The plugin will fill in the Python package version according to the git tag, and &lt;a href="https://github.com/gitpython-developers/GitPython" rel="noopener noreferrer"&gt;GitPython&lt;/a&gt; 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 &lt;em&gt;you don't want to mess up&lt;/em&gt;.  For a real world example, take a look at &lt;a href="https://github.com/intercreate/smpmgr/blob/41683521f850e39f2ce838250483699b16507f76/portable.py#L17-L28" rel="noopener noreferrer"&gt;smpmgr's build scripts&lt;/a&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  GitHub Release Walkthrough
&lt;/h4&gt;

&lt;p&gt;At your GitHub repository's main page, click "Releases"&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzkdv5bos8pruiypz79t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwzkdv5bos8pruiypz79t.png" alt="GitHub Releases Link Screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click "Draft a new release"&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvh1m29by388yxp4lkr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvh1m29by388yxp4lkr2.png" alt="Draft a new release screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;pyproject.toml&lt;/code&gt;!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffryj47kbb9csb4k9kqr4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffryj47kbb9csb4k9kqr4.png" alt="choose a tag screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the version as the Release Title&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l2v8x6z97r6m18r4kc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l2v8x6z97r6m18r4kc4.png" alt="release title screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on "Generate release notes" and then edit the release markdown with any other release information that is important to your users.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcs0rc8l9w5h7nbj6n33l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcs0rc8l9w5h7nbj6n33l.png" alt="release notes screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you are done, click "Publish release" to create the Release page and start the Release Workflow.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjerf1k7w9iprdleu7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtjerf1k7w9iprdleu7v.png" alt="Publish release screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsqv0jxbq1ztvgpgp42x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcsqv0jxbq1ztvgpgp42x.png" alt="release before actions complete screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check on the progress of your Release Workflow, click on "Actions".&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxbq31x30ksszsb6bh3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxbq31x30ksszsb6bh3i.png" alt="actions link screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7rsfuym758a4nqcinam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7rsfuym758a4nqcinam.png" alt="all workflows screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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!&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmduq61aivnvn36phjfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmduq61aivnvn36phjfc.png" alt="actions summary screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the workflow has completed successfully, all artifacts will have been uploaded to the release page that only had two assets before.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9z41cg2s3eq0e92r63b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9z41cg2s3eq0e92r63b3.png" alt="release after workflow screenshot" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Success of the workflow also means that your package has been published to PyPI.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbb3ny4whficty694mo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwbb3ny4whficty694mo9.png" alt="pypi screenshot" width="712" height="367"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Test the Release
&lt;/h2&gt;

&lt;p&gt;After the GitHub Release Workflow has completed, you will find the latest version of&lt;br&gt;
your package at &lt;code&gt;pypi.org/p/&amp;lt;YOUR APP&amp;gt;&lt;/code&gt;, e.g. &lt;a href="https://pypi.org/p/jpsapp/" rel="noopener noreferrer"&gt;pypi.org/p/jpsapp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can install it with &lt;code&gt;pip&lt;/code&gt;, but because we are focused on applications, not libraries, there is a much better tool: &lt;a href="https://pipx.pypa.io/stable/" rel="noopener noreferrer"&gt;pipx&lt;/a&gt;.  &lt;code&gt;pipx&lt;/code&gt; provides a much needed improvement to &lt;code&gt;pip&lt;/code&gt; when installing Python applications and libraries for use, rather than development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/pypa/pipx?tab=readme-ov-file#install-pipx" rel="noopener noreferrer"&gt;pipx installation instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To test your application with &lt;code&gt;pipx&lt;/code&gt;, do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipx install &amp;lt;YOUR APP&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipx install jpsapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;lt;YOUR APP&amp;gt;&lt;/code&gt; will be in your system PATH and can be run from any terminal.  It&lt;br&gt;
can be upgraded to the latest version with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipx upgrade &amp;lt;YOUR APP&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Footnotes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-1"&gt;&lt;/a&gt;^ 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. &lt;a href="https://en.wikipedia.org/wiki/XZ_Utils_backdoor" rel="noopener noreferrer"&gt;"XZ Utils backdoor"&lt;/a&gt;. Wikipedia.com. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-2"&gt;&lt;/a&gt;^ &lt;a href="https://survey.stackoverflow.co/2023/#section-most-popular-technologies-programming-scripting-and-markup-languages" rel="noopener noreferrer"&gt;"Stack Overflow Developer Survey 2023 - Programming, scripting, and markup languages"&lt;/a&gt;. stackoverflow.co. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-3"&gt;&lt;/a&gt;^ &lt;a href="https://blog.pypi.org/posts/2023-11-14-1-pypi-completes-first-security-audit/" rel="noopener noreferrer"&gt;"PyPI Completes First Security Audit"&lt;/a&gt;. PyPI.org. Retrieved 2024-04-14.&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-4"&gt;&lt;/a&gt;^ &lt;a href="https://checkmarx.com/blog/over-170k-users-affected-by-attack-using-fake-python-infrastructure/" rel="noopener noreferrer"&gt;"Over 170K Users Affected by Attack Using Fake Python Infrastructure"&lt;/a&gt;. Checkmarx.com. Retrieved 2024-04-14.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>githubactions</category>
      <category>opensource</category>
      <category>security</category>
    </item>
    <item>
      <title>Building a Universally Portable Python App</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Sat, 16 Mar 2024 21:28:15 +0000</pubDate>
      <link>https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng</link>
      <guid>https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93k84tyowxjhuv2dwlj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93k84tyowxjhuv2dwlj1.png" alt="Python Logo" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome to the first article of a series about deploying a universally portable Python application.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a "Universally Portable" app?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;portable&lt;/strong&gt;, or &lt;strong&gt;standalone&lt;/strong&gt;, application is one that has no install-time or run-time dependencies other than the operating system.&lt;sup id="fnr-footnotes-1"&gt;1&lt;/sup&gt; 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.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;universal&lt;/strong&gt; 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: &lt;strong&gt;Windows&lt;/strong&gt; (72.13%), &lt;strong&gt;MacOS&lt;/strong&gt; (15.46%), and &lt;strong&gt;Linux&lt;/strong&gt; (4.03%).&lt;sup id="fnr-footnotes-2"&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Windows and Linux builds will target the &lt;strong&gt;amd64&lt;/strong&gt; (x86-64) architecture and MacOS will target Arm64 (&lt;strong&gt;"Apple Silicon"&lt;/strong&gt; M-series Macs).  Arm, aarch64 or arm32, builds for Linux would be possible locally but are not available in a GitHub Workflow, &lt;a href="https://github.blog/changelog/2023-10-30-accelerate-your-ci-cd-with-arm-based-hosted-runners-in-github-actions/" rel="noopener noreferrer"&gt;yet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can test the output of this tutorial by installing the example application, &lt;code&gt;jpsapp&lt;/code&gt;, yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use portable ZIPs or OS installers
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/JPHutchins/python-distribution-example/releases" rel="noopener noreferrer"&gt;GitHub: jpsapp releases page&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Install with pipx
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipx install jpsapp
jpsapp --help
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Series
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;This article: build the app locally with &lt;a href="https://build.pypa.io/en/stable/" rel="noopener noreferrer"&gt;build&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Use a GitHub Release Action to automate distribution to &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI, the Python Package Index&lt;/a&gt; so that Python users can install your app with &lt;a href="https://pip.pypa.io/en/stable/" rel="noopener noreferrer"&gt;pip&lt;/a&gt; and &lt;a href="https://github.com/pypa/pipx" rel="noopener noreferrer"&gt;pipx&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Add the universal portable application build to the GitHub Release Action using &lt;a href="https://github.com/pyinstaller/pyinstaller" rel="noopener noreferrer"&gt;PyInstaller&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Add a &lt;strong&gt;Windows MSI&lt;/strong&gt; installer build to the GitHub Release Action using &lt;a href="https://wixtoolset.org/docs/intro/" rel="noopener noreferrer"&gt;WiX v4&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;Linux .deb and .rpm&lt;/strong&gt; installer builds to the GitHub Release Action using &lt;a href="https://github.com/jordansissel/fpm" rel="noopener noreferrer"&gt;fpm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Microsoft Store&lt;/strong&gt; and &lt;code&gt;winget&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Mac App Store&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Deploy to the &lt;strong&gt;Debian Archive&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The App
&lt;/h2&gt;

&lt;p&gt;This article will focus on the application itself and the tooling to support it.&lt;/p&gt;

&lt;p&gt;The app is a command line interface (CLI) that uses the built in &lt;a href="https://docs.python.org/3/library/argparse.html" rel="noopener noreferrer"&gt;&lt;code&gt;argparse&lt;/code&gt;&lt;/a&gt; module and takes one of three actions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;no argument: print "Hello, World!"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-i&lt;/code&gt; or &lt;code&gt;--input&lt;/code&gt;: print "Hello, World!", then "Press any key to exit...".  This is used to create a double-clickable version of the application for Windows users&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v&lt;/code&gt; or &lt;code&gt;--version&lt;/code&gt; argument: print package version and exit&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Take a look at the &lt;a href="https://github.com/JPHutchins/python-distribution-example/blob/main/jpsapp/main.py" rel="noopener noreferrer"&gt;source code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Repo
&lt;/h2&gt;

&lt;p&gt;The repository, &lt;a href="https://github.com/JPHutchins/python-distribution-example/" rel="noopener noreferrer"&gt;python-distribution-example&lt;/a&gt;, can be cloned to your Windows, Linux, or MacOS environment.&lt;/p&gt;

&lt;p&gt;The following are excerpts and explanations of the files that are relevant to running the app locally.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: tooling and dependencies are intentionally kept to a minimum in this&lt;br&gt;
example repository.  A more complicated app that has more dependencies will&lt;br&gt;
benefit from the usage of tools that help to resolve dependencies and manage&lt;br&gt;
environments.  Unfortunately, there is no easy recommendation to make.&lt;/p&gt;

&lt;p&gt;I suggest reading Anna-Lena Popkes' article, &lt;a href="https://alpopkes.com/posts/python/packaging_tools/" rel="noopener noreferrer"&gt;An unbiased evaluation of environment management and packaging tools&lt;/a&gt;, to help you to form an opinion about what tooling is best for your application.&lt;/p&gt;

&lt;p&gt;This repository demonstrates a highly compatible &lt;code&gt;pyproject.toml&lt;/code&gt; that readers&lt;br&gt;
can easily adapt to their choice of tooling.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;jpsapp/&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the Python module itself and contains all of the source code for the application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__main__.py&lt;/code&gt;: support running as a module - &lt;code&gt;python -m jpsapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main.py&lt;/code&gt;: the app described above
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;envr-default&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This file defines the shell environment for common shells like &lt;strong&gt;bash&lt;/strong&gt;, &lt;strong&gt;zsh&lt;/strong&gt;, and &lt;strong&gt;PowerShell&lt;/strong&gt; on Windows, MacOS, and Linux.  The environment is activated by calling &lt;code&gt;. ./envr.ps1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[PROJECT_OPTIONS]&lt;/span&gt;
&lt;span class="py"&gt;PROJECT_NAME&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;jpsapp&lt;/span&gt;
&lt;span class="py"&gt;PYTHON_VENV&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;.venv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;pyproject.toml&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://peps.python.org/pep-0621/" rel="noopener noreferrer"&gt;PEP 621&lt;/a&gt; introduced the &lt;code&gt;pyproject.toml&lt;/code&gt; standard for declaring common metadata, replacing the need for &lt;code&gt;requirements.txt&lt;/code&gt; and most other configuration files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[build-system]&lt;/span&gt;
&lt;span class="py"&gt;requires&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="py"&gt;"setuptools&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;70.0&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;build-backend&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"setuptools.build_meta"&lt;/span&gt;

&lt;span class="nn"&gt;[project]&lt;/span&gt;
&lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jpsapp"&lt;/span&gt;
&lt;span class="py"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.1.6"&lt;/span&gt;
&lt;span class="py"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"An example of Python application distribution."&lt;/span&gt;
&lt;span class="py"&gt;authors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"JP Hutchins"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="py"&gt;email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jphutchins@gmail.com"&lt;/span&gt; &lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;readme&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"README.md"&lt;/span&gt;
&lt;span class="py"&gt;license&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="py"&gt;file&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"LICENSE"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="py"&gt;requires-python&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="py"&gt;"&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;3.8&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="py"&gt;classifiers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;"Development Status :: 4 - Beta"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Intended Audience :: Developers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"Topic :: Software Development :: Build Tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="py"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c"&gt;# Add your project dependencies here&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[project.optional-dependencies]&lt;/span&gt;
&lt;span class="py"&gt;dev&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="py"&gt;"build&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;    &lt;span class="py"&gt;"pyinstaller&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;6.4&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;    &lt;span class="py"&gt;"pyinstaller-versionfile&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;2.1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="s"&gt;",&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nn"&gt;[project.scripts]&lt;/span&gt;
&lt;span class="py"&gt;jpsapp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"jpsapp.main:app"&lt;/span&gt;

&lt;span class="nn"&gt;[project.urls]&lt;/span&gt;
&lt;span class="py"&gt;Homepage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://dev.to/jphutchins/building-a-universally-portable-python-app-2gng"&lt;/span&gt;
&lt;span class="py"&gt;Repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://github.com/JPhutchins/python-distribution-example.git"&lt;/span&gt;

&lt;span class="nn"&gt;[tool.setuptools]&lt;/span&gt;
&lt;span class="py"&gt;packages&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"jpsapp"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="py"&gt;include-package-data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a detailed explanation of the &lt;code&gt;pyproject.toml&lt;/code&gt;, refer to the &lt;a href="https://packaging.python.org/en/latest/guides/writing-pyproject-toml/" rel="noopener noreferrer"&gt;Python Packaging User Guide&lt;/a&gt;.  Here are a few interesting features of our example configuration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;version = "1.1.6"&lt;/code&gt; will version the python package and the eventual app.  This&lt;br&gt;
line in the configuration is the Single Source Of Truth for the version.  There&lt;br&gt;
are many tools available to establish a Git tag as the SSOT, if you prefer.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;packages = ["jpsapp"]&lt;/code&gt; declares that &lt;code&gt;jpsapp&lt;/code&gt; 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 &lt;code&gt;distrubution&lt;/code&gt; folder, that we wouldn't want to package for distribution.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;jpsapp = "jpsapp.main:app"&lt;/code&gt; declares that the command &lt;code&gt;jpsapp&lt;/code&gt; will execute the &lt;code&gt;app&lt;/code&gt; function from &lt;code&gt;jpsapp.main&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Python
&lt;/h3&gt;

&lt;p&gt;If you have Python &amp;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 &lt;em&gt;opinion&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows: use the Microsoft Store or &lt;code&gt;winget&lt;/code&gt; and take advantage of "App Execution Aliases".  Whatever you do, make sure that both &lt;code&gt;python&lt;/code&gt; and &lt;code&gt;python3&lt;/code&gt; call the Python you want, none of this &lt;code&gt;py&lt;/code&gt; nonsense!&lt;/li&gt;
&lt;li&gt;Linux: use your package manager, and maybe &lt;a href="https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa" rel="noopener noreferrer"&gt;deadsnakes&lt;/a&gt; if you're on Ubuntu since they don't keep their Python packages current.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Build the App
&lt;/h2&gt;

&lt;p&gt;Now that you have &lt;a href="https://github.com/JPHutchins/python-distribution-example" rel="noopener noreferrer"&gt;cloned the repository&lt;/a&gt; and installed the dependencies, you can build and run the application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;python3 -m venv .venv&lt;/code&gt;: on this first run it will create the venv at &lt;code&gt;.venv&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;python3&lt;/code&gt; is not an alias to the version of Python 3 you'd like to use then
update the command accordingly, e.g. &lt;code&gt;python -m venv .venv&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;. ./envr.ps1&lt;/code&gt;: activate the development environment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pip install --require-virtualenv -e .[dev]&lt;/code&gt;: install the development dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it!  &lt;code&gt;jpsapp&lt;/code&gt; should print "Hello, World!".  Keep in mind that you can get the same execution with &lt;code&gt;python -m jpsapp&lt;/code&gt;, &lt;code&gt;python -m jpsapp.main&lt;/code&gt;, or &lt;code&gt;python jpsapp/main.py&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;To build the Python package distributions, simply run &lt;code&gt;python -m build&lt;/code&gt;.  The Python &lt;code&gt;.whl&lt;/code&gt; and &lt;code&gt;.tar.gz&lt;/code&gt; packages will be built at &lt;code&gt;dist/&lt;/code&gt;, e.g. &lt;code&gt;dist/jpsapp-1.0.0.tar.gz&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/jphutchins/github-release-action-for-the-python-package-index-1m7n"&gt;next part of this series&lt;/a&gt;, we will use a GitHub Workflow to release the package distribution to the PyPI so that other users can install your app with &lt;code&gt;pipx&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Footnotes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-1"&gt;&lt;/a&gt;^ &lt;a href="https://en.wikipedia.org/wiki/Portable_application" rel="noopener noreferrer"&gt;"Portable application"&lt;/a&gt;. Wikipedia.com. Retrieved 2024-03-11.
&lt;/li&gt;
&lt;li&gt;
&lt;a id="fn-footnotes-2"&gt;&lt;/a&gt;^ &lt;a href="https://gs.statcounter.com/os-market-share/desktop/worldwide" rel="noopener noreferrer"&gt;"OS Market Share"&lt;/a&gt;. GS.Statcounter.com. Retrieved 2024-03-11.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Change History
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;2024-04-14: change &lt;code&gt;myapp&lt;/code&gt; -&amp;gt; &lt;code&gt;yourapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2024-04-18: change &lt;code&gt;yourapp&lt;/code&gt; -&amp;gt; &lt;code&gt;jpsapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2024-06-08: remove poetry; update pyproject.toml; update steps&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>githubactions</category>
      <category>opensource</category>
      <category>security</category>
    </item>
    <item>
      <title>Update Python unittest with asyncio tests for aiohttp and more</title>
      <dc:creator>JP Hutchins</dc:creator>
      <pubDate>Tue, 08 Dec 2020 02:56:21 +0000</pubDate>
      <link>https://dev.to/jphutchins/update-python-unittest-with-asyncio-tests-for-aiohttp-and-more-31aj</link>
      <guid>https://dev.to/jphutchins/update-python-unittest-with-asyncio-tests-for-aiohttp-and-more-31aj</guid>
      <description>&lt;h1&gt;
  
  
  Add asynchronous IO to an older library
&lt;/h1&gt;

&lt;p&gt;Recently I was tasked with adding full &lt;code&gt;asyncio&lt;/code&gt; compatibility to an established library that was using &lt;code&gt;requests&lt;/code&gt;.  I chose to add an &lt;code&gt;aiohttp&lt;/code&gt; option in addition to &lt;code&gt;requests&lt;/code&gt; and unify the interfaces completely: the only difference for the new async interface would be adding a keyword to the class constructor and then using the &lt;code&gt;await&lt;/code&gt; keyword for all IO.  Fantastic!  Or so I thought.&lt;/p&gt;

&lt;h1&gt;
  
  
  The unit testing problem
&lt;/h1&gt;

&lt;p&gt;Like many libraries, this one has extensive unit tests in place using Python &lt;code&gt;unittest&lt;/code&gt;.  For any tests that did not directly use IO I was able to add a novel decorator discussed at the bottom of this post.  The trouble came where the library was using &lt;code&gt;mock&lt;/code&gt; with the &lt;code&gt;@mock.patch("requests.post")&lt;/code&gt; decorator to stub &lt;code&gt;requests&lt;/code&gt;.  Did I need to mock &lt;code&gt;aiohttp&lt;/code&gt; in the same way?  &lt;a href="https://docs.aiohttp.org/en/stable/testing.html#faking-request-object" rel="noopener noreferrer"&gt;The documentation discourages it.&lt;/a&gt;  After experimenting with some proposed patterns and dependencies, I have settled on what I present below as being minimally invasive and avoiding too many new imports.&lt;/p&gt;

&lt;h1&gt;
  
  
  An aiohttp server
&lt;/h1&gt;

&lt;h4&gt;
  
  
  async_server.py
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;aiohttp&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.helpers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SimpleMockRequest&lt;/span&gt;


&lt;span class="n"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RouteTableDef&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;mock_resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mock_req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMockRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;span class="nd"&gt;@routes.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/{_:.*}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;8109&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;runner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AppRunner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;site&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TCPSite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_routes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Our first import is the only new import required to update our test cases to handle aiohttp: a simple server &lt;code&gt;aiohttp.web&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Import SimpleMock and SimpleMockRequest, discussed below&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;routes = web.RouteTableDef()&lt;/code&gt; gives us a route object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mock_resp&lt;/code&gt; and &lt;code&gt;mock_req&lt;/code&gt; will be used as pointers to the current instances of a mocked response or request&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;handler()&lt;/code&gt; function will match all methods and paths and:

&lt;ul&gt;
&lt;li&gt;update the mocked request with the current request &lt;code&gt;mock_req.update(request)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;return the mocked response &lt;code&gt;return web.Response(**mock_resp)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setup_web_server()&lt;/code&gt; will add the server to the event loop&lt;/li&gt;
&lt;li&gt;The simple web app is instantiated as &lt;code&gt;app = web.Application()&lt;/code&gt; and registers the route to the handler &lt;code&gt;app.add_routes(routes)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The mock response, "mock" request, async decorator
&lt;/h1&gt;

&lt;h4&gt;
  
  
  helpers.py
&lt;/h4&gt;

&lt;p&gt;The mocked response was initially a plain dict, &lt;code&gt;mock_resp = {}&lt;/code&gt; but I wanted to make the interface a bit more usable.  SimpleMock is just a case insensitive dict that maps keys to attributes so that you can match the interface of mocked &lt;code&gt;request.post&lt;/code&gt; more closely.  For example, your unit test using &lt;code&gt;requests&lt;/code&gt; might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Mock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MOCK_RESPONSE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where the new interface will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MOCK_RESPONSE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Case insensitive dict to mock HTTP response.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt;
            &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__setattr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__setitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__getattr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__getitem__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SimpleMockRequest on the other hand will probably need to be tinkered with for individual needs.  I only needed method, host, url, headers, and body but added a few more entries in &lt;code&gt;attributes&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleMockRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Case insensitive dict interface for an aiohttp Request object.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;method&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;host&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;path_qs&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;body&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleMock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# match requests interface
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getattr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;AttributeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you've gathered by now, we aren't mocking the request; we are sending a real request to a real server.  The server will use the &lt;code&gt;update()&lt;/code&gt; method on the pointer &lt;code&gt;mock_req&lt;/code&gt; so that we have access to the real request that the server received shortly after it was sent.  The server then responds with the truly mocked &lt;code&gt;mock_resp&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self.clear()&lt;/code&gt; is significant here - there is one mock_req for the whole test suite so we need to clear the old one!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will also need this async decorator to run our unittest methods that are declared with &lt;code&gt;async def&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Decorator to create asyncio context for asyncio methods or functions.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;args[0]&lt;/code&gt; will be "self" when a method is called.&lt;/p&gt;

&lt;h1&gt;
  
  
  Integrate with unittest
&lt;/h1&gt;

&lt;p&gt;With the helpers in place, we can import them into a test file, &lt;code&gt;test_api.py&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  test_api.py - imports
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.helpers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;async_test&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tests.async_server&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  test_api.py - unit test setup and teardown
&lt;/h4&gt;

&lt;p&gt;Get event loop, start async server, init anything else for tests&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestSomeEndpointsAndParsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setUpClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# you probably have some existing code above here
&lt;/span&gt;        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_event_loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nf"&gt;setup_web_server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;LOCALHOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ASYNC_SERVER_PORT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# What you need may be different, just a quick example
# I init an "async version" of the library I'm testing
# This gives me clever trick I'll discuss at end
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setUp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aiohttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ClientSession&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;use_async&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;async_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# You do need to clear the mock_resp after each test case
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;tearDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  

        &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run_until_complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Now we will use the pattern below to add async tests
&lt;/h4&gt;

&lt;p&gt;In addition to using the async keyword, make sure to prefix or postfix async to the test name itself so you do not override the synchronous one!&lt;/p&gt;

&lt;h5&gt;
  
  
  Test that an endpoint is called and the mocked response is parsed correctly:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@async_test&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_callaction_param_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Call an action with parameters and get the results.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;mock_resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TEST_CALLACTION_PARAM&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetPortMapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NewPortMappingIndex&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewInternalClient&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;10.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewExternalPort&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;51773&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NewEnabled&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Test that an endpoint is called with a well formed request:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@async_test&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_subscribe_async&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Should perform a well formed HTTP SUBSCRIBE request.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;cb_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;http://127.0.0.1:5005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;async_subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cb_url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;UnexpectedResponse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SUBSCRIBE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SCRINGLE&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;scrumple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CALLBACK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;%s&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;cb_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HOST&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;ASYNC_HOST&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mock_req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TIMEOUT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I enjoyed using this pattern to add async tests to a large set of tests.  Comment below with what you've found to be the best pattern for testing a code base that maintains support for synchronous as well as asynchronous IO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;Many of the test cases I was working on didn't actually do IO.  However, since an async instance of the main class used a different constructor and an &lt;code&gt;async_init()&lt;/code&gt; that DID make IO, it seemed worthwhile to add tests for these test cases as well.  What I came up with is a decorator that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;runs the test case on the synchronous instance&lt;/li&gt;
&lt;li&gt;runs the test case on the asynchronous instance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is not plug and play - it uses hard coded names: &lt;code&gt;self.server&lt;/code&gt; and &lt;code&gt;self.async_server&lt;/code&gt; are defined in the unittest &lt;code&gt;setUp()&lt;/code&gt; method. The other downside is that if a test case fails you won't be able to tell whether it was the synchronous or asynchronous instance that had an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;functools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;wraps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_async_test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Test both the synchronous and async methods of the device (server).
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nd"&gt;@wraps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;g&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# run the original test
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# make mutable copy of args
&lt;/span&gt;        &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;  &lt;span class="c1"&gt;# save reference to self.server
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;async_server&lt;/span&gt;  &lt;span class="c1"&gt;# set copy.server to async_server
&lt;/span&gt;        &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# run the test using the async instance
&lt;/span&gt;        &lt;span class="n"&gt;async_args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;  &lt;span class="c1"&gt;# point self.server back to original
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it!  Decorate all the test cases that don't do IO with &lt;code&gt;@add_async_test&lt;/code&gt; and save a bunch of code and time - if you're lucky!&lt;/p&gt;

</description>
      <category>python</category>
      <category>testing</category>
      <category>asyncio</category>
      <category>aiohttp</category>
    </item>
  </channel>
</rss>
