🪛 A simple pydantic to Form FastAPI model converter.

Overview

pyfa-converter

Makes it pretty easy to create a model based on Field [pydantic] and use the model for www-form-data.

How to install?

pip install pyfa_converter

How to simplify your life?

image


What do I need to do with the model?

  • We put the decorator @PydanticConverter.body for the model and enjoy.
  • data: YourPydanticModel = FormBody(YourPydanticModel)

If you want to accept a file on an endpoint, then the content-type for that endpoint changes from application/json to www-form-data.

FastAPI does not know how to override the pydantic schema so that parameters are passed as form. Even if you do

foo: CustomPydanticModel = Depends() all model attributes will be passed as query, but we want them to become body, that's what this library exists for.

Usually you use something along the lines of:

image

But, if we accept a lot of fields, then the function becomes very large (the number of attributes for the endpoint increases and it does not look very good).

Thanks to this library, it is possible to force the conversion of Field fields into fields of FastAPI Form with full preservation of all attributes (alias, gt, te, description, title, example and more...)

You might also like...
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

pwy - A simple weather tool.
pwy - A simple weather tool.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging. Name changed from wwy to pwy.

A simple python script to execute a command when a YubiKey is disconnected

YubiKeyExecute A python script to execute a command when a YubiKey / YubiKeys are disconnected. ‏‏‎ ‎ How to use: 1. Download the latest release and d

A super simple wallet application for the NANO cryptocurrency that runs in the terminal
A super simple wallet application for the NANO cryptocurrency that runs in the terminal

Nano Terminal Wallet A super simple wallet application for the NANO cryptocurrency that runs in the terminal Written in 2021 by NinjaSnail1080 (Discor

This a simple tool to query the awesome ippsec.rocks website from your terminal
This a simple tool to query the awesome ippsec.rocks website from your terminal

ippsec-cli This a simple tool to query the awesome ippsec.rocks website from your terminal Installation and usage cd /opt git clone https://github.com

Simple Tool To Grab Like-Card Coupon
Simple Tool To Grab Like-Card Coupon

Simple Tool To Grab Like-Card Coupon

(BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming.

blush 😳 (BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming. Note: The Linux executables were made on Ubu

A simple reverse shell in python

RevShell A simple reverse shell in python Getting started First, start the server python server.py Finally, start the client (victim) python client.py

Un module simple pour demander l'accord de l'utilisateur dans une CLI.
Un module simple pour demander l'accord de l'utilisateur dans une CLI.

Demande de confirmation utilisateur pour CLI Présentation ask_lib est un module pour le langage Python proposant une seule fonction; ask(). Le but pri

Comments
  • Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Error: TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'

    Using the

    from pyfa_converter import FormDepends
    

    and get the error

    TypeError: unsupported operand type(s) for |: 'ModelMetaclass' and 'type'
    

    python: 3.9

    opened by Terryhung 4
  • Add License

    Add License

    Hi! The code in this repo seems super useful for us working with pydantic + fastapi, but we can't use it without a LICENSE. Could you add LICENSE file to the repo? MIT is a permissive one if you don't have an opinion otherwise

    opened by dzcode 3
  • Big update 1.0.0.0!

    Big update 1.0.0.0!

    • The decorator above the model is no longer required and will be removed in the next version!
    • New syntax:
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Header)
    data: MyCustomModel = PyFaDepends(MyCustomModel, _type=Form)
    
    data: MyCustomModel = FormDepends(MyCustomModel)
    data: MyCustomModel = QueryDepends(MyCustomModel)
    
    • Added support for parameters specific only to FastAPI types in Pydantic Field. Example - Field(None, convert_underscores=True)
    opened by dotX12 0
  • error when adding Field constraint

    error when adding Field constraint

    On:

    python: 3.10 and 3.11 pyfa-converter: 1.0.3.0 (and master, which shows as 1.0.1.0) pydantic: 1.10.2

    If you add a Field constraint, pydantic raises a ValueError:

    For example, for the class in this repo, if you change it to (adding a gt constraint of 10:

    class PostContractSmallDoubleBodySchema(BaseModel):
        id: Optional[int] = Field(None, description="gwa", gt=10)
        title: Optional[str] = Field(None)
        data: Optional[List[int]]
    

    pydantic raises the following exception

    ImportError while loading conftest '/home/ben/repos/pyfa-converter/tests/conftest.py'.
    tests/conftest.py:6: in <module>
        from examples.main import app
    examples/main.py:47: in <module>
        @app.post("/test")
    .venv/lib/python3.11/site-packages/fastapi/routing.py:630: in decorator
        self.add_api_route(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:569: in add_api_route
        route = route_class(
    .venv/lib/python3.11/site-packages/fastapi/routing.py:438: in __init__
        self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:292: in get_dependant
        sub_dependant = get_param_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:122: in get_param_sub_dependant
        return get_sub_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:158: in get_sub_dependant
        sub_dependant = get_dependant(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:299: in get_dependant
        param_field = get_param_field(
    .venv/lib/python3.11/site-packages/fastapi/dependencies/utils.py:388: in get_param_field
        annotation = get_annotation_from_field_info(annotation, field_info, param_name)
    pydantic/schema.py:1011: in pydantic.schema.get_annotation_from_field_info
        ???
    E   ValueError: On field "id" the following field constraints are set but not enforced: gt.
    E   For more details see https://pydantic-docs.helpmanual.io/usage/schema/#unenforced-field-constraints
    

    I spent some time investigating and I'm still not sure why this is happening

    bug fixed 
    opened by falkben 9
Releases(1.0.4.0a)
Terminal Colored Text for Python

Terminal Colored Text for Python

R3CKhi-**75 3 Sep 10, 2022
CLI tool to computes CO2 emissions of HPC computations following green-algorithms.org methodology

gqueue gqueue is a CLI (command line interface) tool that computes carbon footprint of HPC computations on clusters running slurm. It follows the meth

4 Dec 10, 2021
Management commands to help backup and restore your project database and media files

Django Database Backup This Django application provides management commands to help backup and restore your project database and media files with vari

687 Jan 04, 2023
argofloats: Simple CLI for ArgoVis and Argofloats

argofloats: Simple CLI for ArgoVis and Argofloats Argo is an international program that collects information from inside the ocean using a fleet of ro

Samapriya Roy 2 Feb 13, 2022
Convert shellcode into :sparkles: different :sparkles: formats!

Bluffy Convert shellcode into ✨ different ✨ formats! Bluffy is a utility which was used in experiments to bypass Anti-Virus products (statically) by f

AD995 305 Dec 17, 2022
Low-Cost Open Source Ventilator or PAPR

Last updated 2020/04/19 Low-Cost Open-Source Ventilator-ish Device or PAPR NOTE: This is currently an independent project not affiliated with any comm

Johnny Lee 1.7k Dec 21, 2022
Colors in Terminal - Python Lang

🎨 Colorate - Python 🎨 About Colorate is an Open Source project that makes it easy to use Python color coding in your projects. After downloading the

0110 Henrique 1 Dec 01, 2021
Squirrel - A cli program to track writing progress

Squirrel Very much a WIP project squirrel is a command line program that tracks you writing progress and gives you useful information and cute and pic

3 Mar 23, 2022
Simple tool, to update linux kernel on ubuntu

Kerbswap Simple tool, to update linux kernel on ubuntu Information At the moment, this tool only supports "Ubuntu" distributions, but will be expanded

dword 1 Oct 31, 2021
This is a repository for collecting global custom management extensions for the Django Framework.

Django Extensions Django Extensions is a collection of custom extensions for the Django Framework. Getting Started The easiest way to figure out what

Django Extensions 6k Jan 03, 2023
Standalone Tailwind CSS CLI, installable via pip

Standalone Tailwind CSS CLI, installable via pip Use Tailwind CSS without Node.j

Tim Kamanin 144 Dec 22, 2022
Easily handle day to day CLI operation via Python instead of regular Bash programs.

pz Ever wished to use Python in Bash? Would you choose the Python syntax over sed, awk, ...? Should you exactly know what command would you use in Pyt

CZ.NIC 697 Jan 03, 2023
(BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming.

blush 😳 (BionicLambda Universal SHell) A simple shell made in Python. Docs and possible C port incoming. Note: The Linux executables were made on Ubu

3 Jun 30, 2021
sync-my-tasks is a CLI tool that copies tasks between apps.

sync-my-tasks Copy tasks between apps Report a Bug · Request a Feature . Ask a Question Table of Contents Table of Contents Getting Started Developmen

William Hutson 2 Dec 14, 2021
liquidctl – liquid cooler control Cross-platform tool and drivers for liquid coolers and other devices

Cross-platform CLI and Python drivers for AIO liquid coolers and other devices

1.7k Jan 08, 2023
A 3D engine powered by ASCII art

3D engine powered by ASCII art

Lingdong Huang 48 Nov 16, 2022
Turdshovel is an interactive CLI tool that allows users to dump objects from .NET memory dumps

Turdshovel Description Turdshovel is an interactive CLI tool that allows users to dump objects from .NET memory dumps without having to fully understa

Leron Gray 41 Jul 27, 2022
A CLI application that downloads your AC submissions from OJ's like Atcoder,Codeforces,CodeChef and distil it into beautiful Submission HeatMap.

Yoda A CLI that takes away the hassle of managing your submission files on different online-judges by automating the entire process of collecting and organizing your code submissions in one single Di

Nikhar Manchanda 1 Jul 28, 2022
Program Command Line Interface (CLI) Sederhana: Pemesanan Nasi Goreng Hekel

Program ini merupakan aplikasi yang berjalan di dalam command line (terminal). Program ini menggunakan built-in library python yaitu argparse yang dapat menerima parameter saat program ini dijalankan

Habib Abdurrasyid 5 Nov 19, 2021
A Neat Application To Manage Your To-Do Lists.

WTD - What To Do? A Neat Application To Manage Your To-Do Lists. One folder can only have one to-do file. Running wth without any subcommands executes

Adam Vajda 1 Oct 24, 2021