<?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: Magda (she/her)</title>
    <description>The latest articles on DEV Community by Magda (she/her) (@magdapoppins).</description>
    <link>https://dev.to/magdapoppins</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F193164%2F433c0ba4-9718-408d-b6a9-77d43993c2b4.JPG</url>
      <title>DEV Community: Magda (she/her)</title>
      <link>https://dev.to/magdapoppins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/magdapoppins"/>
    <language>en</language>
    <item>
      <title>A Code Quality Toolbox for Python Projects</title>
      <dc:creator>Magda (she/her)</dc:creator>
      <pubDate>Tue, 03 Aug 2021 10:33:36 +0000</pubDate>
      <link>https://dev.to/magdapoppins/write-better-python-with-some-help-5g1l</link>
      <guid>https://dev.to/magdapoppins/write-better-python-with-some-help-5g1l</guid>
      <description>&lt;p&gt;When I started shifting more and more towards working with Python, I looked back at the world of JS and thought wow, they have so many nice tools for formatting and linting! I found myself in the wild world of Python programming where &lt;a href="https://www.python.org/dev/peps/pep-0008/"&gt;PEP8&lt;/a&gt; sets the standards, but whether or not you follow them and how you write your code is pretty much up to you. This is my letter to myself back in time, to share some of the tips I now have in my toolbox for maintaining a nice and clean Python project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting
&lt;/h2&gt;

&lt;p&gt;If you want to speed up your coding and rely on a computer to do the polishing for you, you can use a code formatter to automatically walk through and format your code. In the world of python, &lt;a href="https://github.com/psf/black"&gt;Black&lt;/a&gt; is one of the most used formatters. It could be compared to &lt;a href="https://prettier.io/"&gt;Prettier&lt;/a&gt; in the world of JS - they are both opinionated formatters, i.e. they make some choices for you. (You don't have to fight your colleague over whether to use single or double quotes anymore! Great!) &lt;/p&gt;

&lt;p&gt;This is how black responds to a file with &lt;code&gt;print("hello" )&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;$ pip install black
$ black my-sample.py
reformatted my-sample.py
All done! ✨ 🍰 ✨
1 file reformatted.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Linting
&lt;/h2&gt;

&lt;p&gt;Linting differs from formatting in that it will not change the code it looks at, but will notify you if it doesn't follow the specified standards. A good option for linting in python is &lt;a href="https://flake8.pycqa.org/en/latest/"&gt;flake8&lt;/a&gt;. Here is how flake8 responds to the same file contents as black did above.&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 flake8
$ flake8 my-sample.py
my-sample.py:1:14: E202 whitespace before ')'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to this out of the box -linting, there are loads of &lt;a href="https://github.com/DmytroLitvinov/awesome-flake8-extensions"&gt;flake8 extensions&lt;/a&gt; that can help you with for example switching from &lt;code&gt;.format()&lt;/code&gt; to using f-strings or checking that your naming follows the PEP8 guidelines. For example, adding &lt;code&gt;flake8-length&lt;/code&gt; adds line length checking to the linting.&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 flake8-length
$ flake8 test.py
test.py:1:80: LN001 line is too long (169 &amp;gt; 79)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you like to keep your imports in tact, there are also rules to enforce a specific order amongst them. One tool that does that is &lt;a href="https://pycqa.github.io/isort/"&gt;isort&lt;/a&gt;. Isort follows the order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;absolute imports&lt;/li&gt;
&lt;li&gt;built-in modules&lt;/li&gt;
&lt;li&gt;third-party libraries&lt;/li&gt;
&lt;li&gt;imports from the same project
You can also integrate isort into flake8 using the &lt;a href="https://github.com/gforcada/flake8-isort"&gt;flake8-isort&lt;/a&gt; plugin! &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Type checking
&lt;/h2&gt;

&lt;p&gt;Type annotation support was introduced in python 3.5 and there are tools for watching that the typing is in tact. A good option for checking your type annotations is using &lt;a href="http://mypy-lang.org/"&gt;mypy&lt;/a&gt;. Given a sample file that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def return_hello(name: str) -&amp;gt; int:
    return f'Hello, {name}!'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can run mypy and see that our type hints indeed are off:&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 mypy
$ mypy my-sample.py
my-sample.py:2: error: Incompatible return value type (got "str", expected "int")
Found 1 error in 1 file (checked 1 source file)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some other good alternatives for type checking are &lt;a href="https://github.com/microsoft/pyright"&gt;Pyright&lt;/a&gt; (which is seen in VSCode via Microsofts PyLance plugin) and &lt;a href="https://pyre-check.org/"&gt;Pyre&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wiring it up
&lt;/h2&gt;

&lt;p&gt;You could choose to manually run these tools from time to time in your project, but you will have a better time if you make them part of your pull request routine. Using GitHub actions, you can set up for example a &lt;a href="https://github.com/marketplace/actions/flake8-action"&gt;flake8 action&lt;/a&gt;. You can alternatively build an action from scratch - here we run isort with the check-only option:&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;Check import order&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&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;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;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v1&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;Set up Python &lt;/span&gt;&lt;span class="m"&gt;3.9&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@v1&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="m"&gt;3.9&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;Install isort&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;|&lt;/span&gt;
        &lt;span class="s"&gt;python -m pip install --upgrade pip&lt;/span&gt;
        &lt;span class="s"&gt;pip install isort&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;Check import order&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;isort . -c -v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;I love rules, so seeing how systematically one can unify ones codebase using tools like these just makes my day. However I also know lots of devs who don't feel like this kind of rigour is their jam - it should always be discussed in the team how and why we want to set the codebase standards. &lt;/p&gt;

&lt;p&gt;Hope this text gave you all some ideas of what tools are out there and what you could pick into your own toolkit as well! If you know of a useful tool I didn't mention - please let me know in the comments! 😊&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>devops</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
