Type Stubs for flask-smorest
types-smorest provides PEP 561 compatible type stubs for the `flask-smorest` library, a REST API framework built upon Flask and Marshmallow. It allows static type checkers like MyPy to perform type verification on `flask-smorest` applications, enhancing code quality and maintainability. The library is currently at version 1.1.2 and receives updates as needed to track `flask-smorest` features and fixes.
Warnings
- gotcha This is a type stub package; it contains no runtime code. You should never import modules or symbols directly from `types_smorest` in your application's runtime code. Its sole purpose is to provide type hints for static analysis tools like MyPy.
- gotcha `types-smorest` is a partial stub package. This means it may not cover every function, class, or object available in `flask-smorest`, leading to some `Any` types or incomplete type checking in certain scenarios.
- gotcha Compatibility between `types-smorest` and `flask-smorest` versions is crucial. Breaking changes in `flask-smorest` might not be immediately reflected in `types-smorest`, potentially leading to incorrect type hints or errors during static analysis. `types-smorest` v1.1.2 was released in Dec 2022, while `flask-smorest` has continued to release updates, including v0.47.0 in March 2026.
Install
-
pip install types-smorest
Imports
- Api
from flask_smorest import Api
- Blueprint
from flask_smorest import Blueprint
- Schema
from marshmallow import Schema
Quickstart
from flask import Flask
from flask.views import MethodView
from marshmallow import Schema, fields
from flask_smorest import Api, Blueprint, abort
app = Flask(__name__)
app.config["API_TITLE"] = "My API"
app.config["API_VERSION"] = "v1"
app.config["OPENAPI_VERSION"] = "3.0.2"
api = Api(app)
class ItemSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.String(required=True)
price = fields.Float(required=True)
blp = Blueprint("items", "items", url_prefix="/items", description="Operations on items")
api.register_blueprint(blp)
@blp.route("/")
class Items(MethodView):
@blp.response(200, ItemSchema(many=True))
def get(self) -> list[dict]:
"""List all items"""
# In a real app, this would fetch from a database
items = [{
"id": 1,
"name": "Example Item",
"price": 10.99
}]
return items
@blp.arguments(ItemSchema)
@blp.response(201, ItemSchema)
def post(self, new_item_data: dict) -> dict:
"""Create a new item"""
# In a real app, this would save to a database
new_item_data['id'] = len(self.get()) + 1 # Simulate ID generation
return new_item_data
# To run this example (requires flask-smorest, marshmallow):
# 1. pip install Flask flask-smorest marshmallow
# 2. Save as app.py
# 3. export FLASK_APP=app.py
# 4. flask run
#
# To use with type checking (requires types-smorest, mypy):
# 1. pip install types-smorest mypy
# 2. mypy app.py