<?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: m1yag1</title>
    <description>The latest articles on DEV Community by m1yag1 (@m1yag1).</description>
    <link>https://dev.to/m1yag1</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%2F274414%2F31155c77-c006-4c93-a6b0-6634187480b7.jpeg</url>
      <title>DEV Community: m1yag1</title>
      <link>https://dev.to/m1yag1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/m1yag1"/>
    <language>en</language>
    <item>
      <title>How to setup your project with pre-commit, black, and flake8</title>
      <dc:creator>m1yag1</dc:creator>
      <pubDate>Mon, 18 Nov 2019 16:20:34 +0000</pubDate>
      <link>https://dev.to/m1yag1/how-to-setup-your-project-with-pre-commit-black-and-flake8-183k</link>
      <guid>https://dev.to/m1yag1/how-to-setup-your-project-with-pre-commit-black-and-flake8-183k</guid>
      <description>&lt;h2&gt;
  
  
  Why pre-commit Git hooks?
&lt;/h2&gt;

&lt;p&gt;Several OpenStax Python projects use Git hooks via &lt;a href="https://pre-commit.com/#intro"&gt;pre-commit&lt;/a&gt; in order to run formatting and linting checks before the commit process. This approach has some really nice advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IDE agnostic&lt;/li&gt;
&lt;li&gt;Runs automatically on only the code that is changing.&lt;/li&gt;
&lt;li&gt;Feedback on linting or formatting issues happens before you commit.&lt;/li&gt;
&lt;li&gt;Enforces style consistency in the codebase.&lt;/li&gt;
&lt;li&gt;Helps create smaller diffs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Black?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/python/black"&gt;Black&lt;/a&gt; is a highly opinionated code formatter. Black focuses on reformatting your code files in place for you. When you're comfortable with black taking over the minutiae of hand formatting you will see that you can focus more on the content of your code than formatting it properly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Flake8?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://flake8.pycqa.org/en/latest/"&gt;Flake8&lt;/a&gt; is a wrapper around various Python linting tools used to check for conformance against the PEP8 style guidelines for Python. This helps make sure that your code is using a consistent style across the entire project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install pre-commit and the proper config files
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install pre-commit
&lt;/h3&gt;

&lt;p&gt;We first need to install pre-commit onto the system. You can refer to the &lt;a href="https://pre-commit.com/#intro"&gt;pre-commit&lt;/a&gt; website for more in-depth information. We'll focus on the necessary commands to get things installed.&lt;/p&gt;

&lt;p&gt;If you are using OSX you can install pre-commit using brew:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`brew install pre-commit`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also install pre-commit into your python virtualenv:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`pip install pre-commit`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Configure Black
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;pyproject.toml&lt;/code&gt; file in the root of the project directory, if it doesn't exist, and copy the following into the file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    [tool.black]
    py36 = true
    include = '\.pyi?$'
    exclude = '''
    /(
        \.git
      | \.hg
      | \.mypy_cache
      | \.tox
      | \.venv
      | _build
      | buck-out
      | build
      | dist

      # The following are specific to Black, you probably don't want those.
      | blib2to3
      | tests/data
    )/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure Flake8
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;.flake8&lt;/code&gt; in the root of the project directory, if it doesn't exist, and add the following to the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    [flake8]
    ignore = E203, E266, E501, W503, F403, F401
    max-line-length = 89
    max-complexity = 18
    select = B,C,E,F,W,T4,B9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure pre-commit
&lt;/h3&gt;

&lt;p&gt;Now that we have the proper files in place we can start using pre-commit to run black and flake8 git hooks.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file in the root of the project directory, if it doesn't exist, and add the following to the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # See https://pre-commit.com for more information
    # See https://pre-commit.com/hooks.html for more hooks
    repos:
      - repo: https://github.com/ambv/black
        rev: stable
        hooks:
          - id: black
            language_version: python3.7
      -   repo: https://github.com/pre-commit/pre-commit-hooks
          rev: v2.0.0
          hooks:
          - id: flake8
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Install the hooks
&lt;/h3&gt;

&lt;p&gt;Install the git hooks defined in the pre-commit file by running the following in the terminal:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`pre-commit install`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Run the hooks
&lt;/h3&gt;

&lt;p&gt;If this is an already existing project you may want to go ahead and format all the files. You can do this by running the following in the terminal:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`pre-commit run --all-files`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will run all the git hooks you've installed against all files. At this point you can review the messages and make corrections as necessary. You can re-run the command until there are no longer any warnings.&lt;/p&gt;

&lt;p&gt;Finally, commit all the new changes! From now on when you stage files to be committed only those will be formatted and linted by black and flake8. Say goodbye to forgetting to run those linting commands and having Travis CI blow up.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
