🐍 What's new in Flask 2.0
Tags: python, programming, flask
Published: 2021-05-27 20:56:49

#### 💡 Flask 2.0.0 was released a while ago and many interesting changes are available.

### <br>
Flask is the most used framework for web development according to the Jetbrains [Python Developers Survey from 2020](https://www.jetbrains.com/lp/python-developers-survey-2020/), and in May of 2021, version `2.0.0` of Flask came out with 31 new changes.
In this post, you'll learn about some of those changes and the refactoring you might want to do to your code to take the most advantage of this new version.

## What's New?

At the beginning of this post, I mentioned 31 alterations were added to this release of the framework, and [you can read about all of them here](https://flask.palletsprojects.com/en/2.0.x/changes/#version-2-0-0). 

First and foremost, the most critical change on Flask `2.0.0` is that Flask **officially dropped support for Python 2 and 3.5**. If you are using these versions on your projects **and want** to update to Flask `2.0.0`, you need to also update the Python version you are using.

<include src="TweetQuote" quoteText="Flask officially drops support for Python 2 and 3.5! Time to update the Python version in your projects!"/>

But let focus on the 3 things that I personally like the most:

- Type hinting is here!
- You can read the application configuration from any file.
- My favorite syntactic sugar just arrived: HTTP method-specific decorators for defining routes.

## How To Upgrade

Let's start at the beginning: Upgrading Flask's version. For running an app locally, just remember to activate your Python environment (if you use one) and run the following:

```console
pip install -U Flask==2.0.0
```

The `-U` flag tells `pip` to upgrade the version of Flask on your environment to version `2.0.0`. After that, it is time to update the codebase. If your codebase uses unit or integration tests, it is a great time to see if any of them break and to fix the code accordingly. 😉

After doing the upgrade, you might want to also update your environment files like `requirements.txt` and `Pipenv`.

## Type Hinting

As [**The Zen of Python**](https://www.python.org/dev/peps/pep-0020/) states: _Explicit is better than implicit_, and type hinting is a beautiful way of _explicitly_ telling anyone reading a class or function documentation what that object expects as a parameter. Now, all of Flask has typing annotations.

Consider this: it has been a while since you used Flask, and you don't quite remember what a given parameter receives. You might not even remember what type of object that parameter receives. You might try to find the examples for the class you want to use, or now you can simply check the class signature by using the help function, and it will clearly state what that parameter receives.

Let's see a more concrete example. Let's check out the help for the Flask class, but let's see how it was before first.

Here I have an environment with Flask on version `1.1.4` (the last version before `2.0.0`), and when I open up the `python` console and import the `Flask` class and then call the `help()` function to display the class documentation:

```python
from flask import Flask
help(Flask)
```
This is what you'll see:

![Screenshot of the help documentation for Flask class object before upgrading Flask](https://images.ctfassets.net/23aumh6u8s0i/cLF8UiM4DsY8sQn1onxLX/e63b3a86fe5f041eec0f3ce9cbab6cd8/01_help_flask_class_terminal_before.png)

Now, if you do the same thing in a Python environment with Flask `2.0.0`, this is what you'll see:

![Screenshot of the help documentation for Flask class object after upgrading Flask](https://images.ctfassets.net/23aumh6u8s0i/7MAf0w2mgarPNHoHz46I4D/a749d1a9ba34c6c0bdc0811e93fe8e04/02_help_flask_class_terminal_after.png)

If you look closely, you can see that `instance_relative_config` should receive a boolean value instead of assuming it would be _boolean_ because the default for that parameter is `False`.

This isn't a change that prompts you to update your code, but it will come in handy when you start writing new endpoints or adding more functionality to the ones already there.



[Read more](https://dev.auth0.com/blog/whats-new-in-flask-2/?utm_source=facebookgroup&utm_medium=sc&utm_campaign=flask)