summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'guide/porting.html')
-rw-r--r--guide/porting.html60
1 files changed, 56 insertions, 4 deletions
diff --git a/guide/porting.html b/guide/porting.html
index 58d3d5e..721bd86 100644
--- a/guide/porting.html
+++ b/guide/porting.html
@@ -71,6 +71,57 @@ PR#14349</a> for an example of impact and a fix.</p>
</div>
</section>
</section>
+<section id="python-3-12">
+<h2>Python 3.12<a class="headerlink" href="#python-3-12" title="Link to this heading">¶</a></h2>
+<p>See also: <a class="reference external" href="https://docs.python.org/3.12/whatsnew/3.12.html">what’s new in Python 3.12</a></p>
+<section id="called-with-and-other-invalid-assertions-now-trigger-an-error">
+<h3>.called_with (and other invalid assertions) now trigger an error<a class="headerlink" href="#called-with-and-other-invalid-assertions-now-trigger-an-error" title="Link to this heading">¶</a></h3>
+<p>It is not uncommon for test suites to write invalid assertions such as:</p>
+<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">unittest</span><span class="o">.</span><span class="n">mock</span><span class="o">.</span><span class="n">patch</span><span class="p">(</span><span class="s2">&quot;...&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">foo_mock</span><span class="p">:</span>
+ <span class="o">...</span>
+
+<span class="k">assert</span> <span class="n">foo_mock</span><span class="o">.</span><span class="n">called_with</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
+</pre></div>
+</div>
+<p>Prior to Python 3.12, such assertions would silently pass. Since
+the <code class="docutils literal notranslate"><span class="pre">.called_with()</span></code> method does not exist, a <code class="docutils literal notranslate"><span class="pre">MagicMock</span></code> object
+is returned and it evaluates to <code class="docutils literal notranslate"><span class="pre">True</span></code> in boolean context.</p>
+<p>Starting with Python 3.12, an exception is raised instead:</p>
+<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="ne">AttributeError</span><span class="p">:</span> <span class="s1">&#39;called_with&#39;</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">a</span> <span class="n">valid</span> <span class="n">assertion</span><span class="o">.</span> <span class="n">Use</span> <span class="n">a</span> <span class="n">spec</span> <span class="k">for</span> <span class="n">the</span> <span class="n">mock</span> <span class="k">if</span> <span class="s1">&#39;called_with&#39;</span> <span class="ow">is</span> <span class="n">meant</span> <span class="n">to</span> <span class="n">be</span> <span class="n">an</span> <span class="n">attribute</span><span class="o">.</span>
+</pre></div>
+</div>
+<p>The fix is to use the correct <code class="docutils literal notranslate"><span class="pre">.assert_called_with()</span></code> method
+or similar:</p>
+<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">unittest</span><span class="o">.</span><span class="n">mock</span><span class="o">.</span><span class="n">patch</span><span class="p">(</span><span class="s2">&quot;...&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">foo_mock</span><span class="p">:</span>
+ <span class="o">...</span>
+
+<span class="n">foo_mock</span><span class="o">.</span><span class="n">assert_called_with</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
+</pre></div>
+</div>
+<p>See the <a class="reference external" href="https://docs.python.org/3.12/library/unittest.mock.html">unittest.mock</a> documentation for the complete list of available
+assertions.</p>
+<p>Please note that since the original code did not actually test anything,
+fixing the test case may reveal failed expectations.</p>
+</section>
+<section id="deprecated-test-method-alias-removal">
+<h3>Deprecated test method alias removal<a class="headerlink" href="#deprecated-test-method-alias-removal" title="Link to this heading">¶</a></h3>
+<p>Python 3.12 removes multiple deprecated test method aliases, such
+as <code class="docutils literal notranslate"><span class="pre">assertEquals()</span></code> and <code class="docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code>. The documentation
+provides <a class="reference external" href="https://docs.python.org/3.12/whatsnew/3.12.html#id3">a list of removed aliases and their modern replacements</a>.</p>
+<p>It should be noted that all of the new methods are available since
+Python 3.2 (and most even earlier), so the calls can be replaced without
+worrying about backwards compatibility.</p>
+<p>Most of the time, it should be possible to trivially <code class="docutils literal notranslate"><span class="pre">sed</span></code> the methods
+in ebuild without having to carry a patch, e.g.:</p>
+<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>src_prepare<span class="o">()</span><span class="w"> </span><span class="o">{</span>
+<span class="w"> </span><span class="c1"># https://github.com/byroot/pysrt/commit/93f52f6d4f70f4e18dc71deeaae0ec1e9100a50f</span>
+<span class="w"> </span>sed<span class="w"> </span>-i<span class="w"> </span>-e<span class="w"> </span><span class="s1">&#39;s:assertEquals:assertEqual:&#39;</span><span class="w"> </span>tests/*.py<span class="w"> </span><span class="o">||</span><span class="w"> </span>die
+<span class="w"> </span>distutils-r1_src_prepare
+<span class="o">}</span>
+</pre></div>
+</div>
+</section>
+</section>
<section id="python-3-11">
<h2>Python 3.11<a class="headerlink" href="#python-3-11" title="Link to this heading">¶</a></h2>
<p>See also: <a class="reference external" href="https://docs.python.org/3.11/whatsnew/3.11.html">what’s new in Python 3.11</a></p>
@@ -399,13 +450,13 @@ where it is named <code class="docutils literal notranslate"><span class="pre">T
</div>
<p>The following dependency string:</p>
<div class="highlight-toml notranslate"><div class="highlight"><pre><span></span><span class="n">dependencies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
-<span class="w"> </span><span class="s">&quot;toml&quot;</span><span class="p">,</span>
+<span class="w"> </span><span class="s2">&quot;toml&quot;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
<p>would be replaced by:</p>
<div class="highlight-toml notranslate"><div class="highlight"><pre><span></span><span class="n">dependencies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
-<span class="w"> </span><span class="s">&quot;tomli &gt;= 1.2.3; python_version &lt; &#39;3.11&#39;&quot;</span><span class="p">,</span>
+<span class="w"> </span><span class="s2">&quot;tomli &gt;= 1.2.3; python_version &lt; &#39;3.11&#39;&quot;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
@@ -443,8 +494,8 @@ look like the following:</p>
</div>
<p>In this case, the dependency string becomes more complex:</p>
<div class="highlight-toml notranslate"><div class="highlight"><pre><span></span><span class="n">dependencies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
-<span class="w"> </span><span class="s">&quot;tomli &gt;= 1.2.3; python_version &gt;= &#39;3.6&#39; and python_version &lt; &#39;3.11&#39;&quot;</span><span class="p">,</span>
-<span class="w"> </span><span class="s">&quot;toml; python_version &lt; &#39;3.6&#39;&quot;</span><span class="p">,</span>
+<span class="w"> </span><span class="s2">&quot;tomli &gt;= 1.2.3; python_version &gt;= &#39;3.6&#39; and python_version &lt; &#39;3.11&#39;&quot;</span><span class="p">,</span>
+<span class="w"> </span><span class="s2">&quot;toml; python_version &lt; &#39;3.6&#39;&quot;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
@@ -516,6 +567,7 @@ modules need to be imported and used separately rather than one.</p>
<li class="toctree-l1"><a class="reference internal" href="buildsys.html">Integration with build systems written in Python</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Porting tips</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#retroactive-changes">Retroactive changes</a></li>
+<li class="toctree-l2"><a class="reference internal" href="#python-3-12">Python 3.12</a></li>
<li class="toctree-l2"><a class="reference internal" href="#python-3-11">Python 3.11</a></li>
<li class="toctree-l2"><a class="reference internal" href="#python-3-10">Python 3.10</a></li>
<li class="toctree-l2"><a class="reference internal" href="#python-3-9">Python 3.9</a></li>