
    
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/hypothesis","feed_url":"https:\/\/mathspp.com\/blog\/tags\/hypothesis.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"TIL #076 \u2013 Hypothesis for code refactoring","date_published":"2023-08-08T18:10:00+02:00","id":"https:\/\/mathspp.com\/blog\/til\/hypothesis-for-code-refactoring","url":"https:\/\/mathspp.com\/blog\/til\/hypothesis-for-code-refactoring","content_html":"<p>Today I learned how to use Hypothesis to do confident code refactoring.<\/p>\n\n<h2 id=\"hypothesis-for-code-refactoring\">Hypothesis for code refactoring<a href=\"#hypothesis-for-code-refactoring\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<h3 id=\"what-is-hypothesis\">What is Hypothesis?<a href=\"#what-is-hypothesis\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p><a href=\"https:\/\/hypothesis.readthedocs.io\/en\/latest\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Hypothesis<\/a> is a Python library that you can use when testing your code.\nIn a nutshell, Hypothesis will generate test cases automatically for you.<\/p>\n<p>In case you already know what that means, Hypothesis enables property-based testing, which is a &ldquo;testing paradigm&rdquo;.\nIn case you are interested, I've written about <a href=\"https:\/\/semaphoreci.com\/blog\/property-based-testing-python-hypothesis-pytest\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">getting started with Hypothesis<\/a> before.<\/p>\n<p>One of the cool things about Hypothesis is that you can use it when you are refactoring code and you want to be as certain as possible that you are not breaking anything.\nLet me give you a concrete example.<\/p>\n<h3 id=\"code-refactoring\">Code refactoring<a href=\"#code-refactoring\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Recently I played a bit with the <a href=\"\/blog\/til\/damerau-levenshtein-distance\">Damerau-Levenshtein distance<\/a> and I implemented it in Python.\nThe code (taken from <a href=\"\/blog\/til\/damerau-levenshtein-distance\">this TIL article<\/a>) looked like this:<\/p>\n<pre><code class=\"language-py\">from functools import lru_cache\n\n@lru_cache\ndef dl(a, b):\n    edit_distances = []\n\n    if len(a) == len(b) == 0:\n        edit_distances.append(0)\n\n    if len(a) &gt; 0:\n        edit_distances.append(dl(a[:-1], b) + 1)\n\n    if len(b) &gt; 0:\n        edit_distances.append(dl(a, b[:-1]) + 1)\n\n    if len(a) &gt; 0 and len(b) &gt; 0:\n        edit_distances.append(dl(a[:-1], b[:-1]) + (a[-1] != b[-1]))\n\n    if len(a) &gt; 1 and len(b) &gt; 1 and a[-1] == b[-2] and a[-2] == b[-1]:\n        edit_distances.append(dl(a[:-2], b[:-2]) + (a[-1] != b[-1]))\n\n    return min(edit_distances)<\/code><\/pre>\n<p>However, this was just a basic implementation that translated the mathematical formula that the Wikipedia page showed.\nI wanted to have a go at rewriting this in a better way.\nI wanted to <em>refactor<\/em> the code.<\/p>\n<p>So, I did.\nI came up with this alternative implementation:<\/p>\n<pre><code class=\"language-py\">from functools import lru_cache\n\n@lru_cache\ndef dl(a, b):\n    if not a or not b:\n        return len(a) + len(b)\n\n    levenshstein = min(\n        dl(a[:-1], b) + 1,\n        dl(a, b[:-1]) + 1,\n        dl(a[:-1], b[:-1]) + (a[-1] != b[-1]),\n    )\n\n    if a[:-1] and b[:-1] and a[-1] == b[-2] and b[-1] == a[-2]:\n        return min(\n            levenshstein,\n            dl(a[:-2], b[:-2]) + (a[-1] != b[-1]),\n        )\n\n    return levenshstein<\/code><\/pre>\n<p>Now, the question is:\nare these two functions the same?\nDo the two functions compute the same thing?<\/p>\n<p>Enter: Hypothesis!<\/p>\n<h3 id=\"verifying-a-code-refactor-with-hypothesis\">Verifying a code refactor with Hypothesis<a href=\"#verifying-a-code-refactor-with-hypothesis\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Because Hypothesis generates random test cases for you, what you can do is create a test where Hypothesis generates two random strings, we feed the two strings to the two alternative implementations, and then we check if they return the same result!<\/p>\n<p>In essence, if <code>dl<\/code> and <code>dl2<\/code> are the two functions, you just need to write this:<\/p>\n<pre><code class=\"language-py\">## dl.py\nfrom functools import lru_cache\n\nfrom hypothesis import given\nfrom hypothesis.strategies import text\n\n@lru_cache\ndef dl(a, b):\n    ...\n\n@lru_cache\ndef dl2(a, b):\n    ...\n\n@given(text(max_size=15), text(max_size=15))\ndef test_dl_match(a, b):\n    assert dl(a, b) == dl2(a, b)<\/code><\/pre>\n<p>Then, you can run your tests &ndash; for example, with <code>pytest dl.py<\/code> &ndash; and wait for Hypothesis's verdict.\nIf the test passes, that's because the two functions <em>probably<\/em> compute the same thing.<\/p>\n<p>In this case, the test passes, so the two functions likely are the same and my refactor is OK!<\/p>\n<p>Notice that this doesn't guarantee that the two functions are correct!...<\/p>","summary":"Today I learned how to use Hypothesis to do confident code refactoring.","date_modified":"2025-07-23T16:49:02+02:00","tags":["hypothesis","programming","python","testing"],"image":"\/user\/pages\/02.blog\/04.til\/076.hypothesis-for-code-refactoring\/thumbnail.webp"}]}
