🪛 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)
Neovim integration for Google Keep, built using gkeepapi

Gkeep.nvim Neovim integration for Google Keep, built using gkeepapi Requirements Neovim 0.5 Python 3.6+ A patched font (optional. Used for icons) Tabl

Steven Arcangeli 143 Jan 02, 2023
Quickly open any path on your terminal window in your $EDITOR of choice!

Tmux fpp Plugin wrapper around Facebook PathPicker. Quickly open any path on your terminal window in your $EDITOR of choice! Demo Dependencies fpp - F

257 Dec 28, 2022
A CLI minesweeper application written in 60 LoC python

This is a CLI minesweeper application written in 60 LoC python. You can use d row,column to dig and f row,column to flag/unflag

1 Dec 21, 2021
Textual: a TUI (Text User Interface) framework for Python inspired by modern web development

Textual Textual is a TUI (Text User Interface) framework for Python inspired by

17.1k Jan 04, 2023
A terminal utility to sort image files based on their characteristics.

About A terminal utility to sort image files based on their characteristics. Motivation This program was developed after I've realized that I had too

José Ferreira 1 Dec 10, 2022
Collection of useful command line utilities and snippets to help you organise your workspace and improve workflow.

Collection of useful command line utilities and snippets to help you organise your workspace and improve workflow.

Dominik Tarnowski 3 Dec 26, 2021
f90nml - A Fortran namelist parser, generator, and editor

f90nml - A Fortran namelist parser, generator, and editor A Python module and command line tool for parsing Fortran namelist files Documentation The c

Marshall Ward 110 Dec 14, 2022
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.

parse_it A python library for parsing multiple types of config files, envvars and command line arguments that takes the headache out of setting app co

Naor Livne 97 Oct 22, 2022
Projeto Reverse Shell For Python

Use com sabedoria!!! Modo de uso: Linux (inclui Android e Mac): - apt-get update - apt install python3 (ou "python" apenas) - git clone https://github

1 Jan 03, 2022
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

CallMePixelMan 7 May 09, 2022
asciinema - Terminal session recorder 📹

asciinema - Terminal session recorder 📹

asciinema 11.1k Dec 27, 2022
Hack-All is a simple CLI tool that helps ethical-hackers to make a reverse connection without knowing the target device in use is it computer or phone

Hack-All is a simple CLI tool that helps ethical-hackers to make a reverse connection without knowing the target device in use is it computer

LightYagami17 5 Nov 22, 2022
Command line interface for unasync

CLI for unasync Command line interface for unasync Getting started Install Run the following command to install the package with pip: pip install unas

Leynier Gutiérrez González 3 Apr 04, 2022
Pymongo based CLI client, to run operation on existing databases and collections

Mongodb-Operations-Console Pymongo based CLI client, to run operation on existing databases and collections Program developed by Gustavo Wydler Azuaga

Gus 1 Dec 01, 2021
Alacritty terminal used with Bash, Tmux, Vim, Mutt, Lynx, etc. and the many different additions added to each configuration file

Alacritty terminal used with Bash, Tmux, Vim, Mutt, Lynx, etc. and the many different additions added to each configuration file

Carter 19 Aug 24, 2022
A simple CLI based any Download Tool, that find files and let you stream or download thorugh WebTorrent CLI or Aria or any command tool

Privateer A simple CLI based any Download Tool, that find files and let you stream or download thorugh WebTorrent CLI or Aria or any command tool How

Shreyash Chavan 2 Apr 04, 2022
Just a shell writed on Python

HIGHSHELL (also hSH or HS) Just a shell writed on Python Send bug report • How to use the shell • Broked features • Licenses How to use the shell Inst

0LungSkill0 2 Jan 04, 2022
⌨ Toward a more useful keyboard

Toward a more useful keyboard Steve Losh's Modern Space Cadet is an inspiration. It opened my eyes to the fact that there's a more useful keyboard hid

Jason Rudolph 1.7k Jan 01, 2023
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

9 Jul 22, 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