Skip to content

Commit 077dc08

Browse files
committed
Initial commit
0 parents  commit 077dc08

File tree

8 files changed

+913
-0
lines changed

8 files changed

+913
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
105+
__pypackages__/
106+
107+
# Celery stuff
108+
celerybeat-schedule
109+
celerybeat.pid
110+
111+
# SageMath parsed files
112+
*.sage.py
113+
114+
# Environments
115+
.env
116+
.venv
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
123+
# Spyder project settings
124+
.spyderproject
125+
.spyproject
126+
127+
# Rope project settings
128+
.ropeproject
129+
130+
# mkdocs documentation
131+
/site
132+
133+
# mypy
134+
.mypy_cache/
135+
.dmypy.json
136+
dmypy.json
137+
138+
# Pyre type checker
139+
.pyre/
140+
141+
# pytype static type analyzer
142+
.pytype/
143+
144+
# Cython debug symbols
145+
cython_debug/
146+
147+
# PyCharm
148+
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150+
# and can be added to the global gitignore or merged into this file. For a more nuclear
151+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
152+
#.idea/
153+
154+
/.vscode

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Huba Tuba
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# flask-msgspec
2+
[msgspec](https://github.com/jcrist/msgspec) integration for [Flask](https://github.com/pallets/flask)
3+
4+
This project was inspired by the [flask-pydantic](https://github.com/bauerji/flask-pydantic) package created by [bauerji](https://github.com/bauerji) and the [Litestar](https://github.com/litestar-org/litestar) framework, however while the `validate` decorator appears similar to the one found in `flask-pydantic` there are many differences.
5+
6+
## Usage
7+
Consider this simple example:
8+
```py
9+
class BodyStructWithConstraints(msgspec.Struct):
10+
foo: Annotated[int, msgspec.Meta(gt=0, le=100)]
11+
bar: Annotated[list[str | int], msgspec.Meta(min_length=1)]
12+
13+
14+
@app.post(rule="/test/<uuid:uuid_value>")
15+
@validate()
16+
def test_handler(
17+
uuid_value: UUID,
18+
query1: float,
19+
body: BodyStructWithConstraints,
20+
optional_query: str = "default_value",
21+
) -> dict:
22+
return locals()
23+
```
24+
Here we have a UUID path parameter, a required query parameter of `float` type, a body of type `BodyStructWithConstraints`, and an optional query parameter which is a `string`, the endpoint will return a `dictionary` of unknown types.
25+
26+
Currently there is only one reserved keyword; `body`. This tries to convert either `request.data` or `request.form` to the specified type.
27+
28+
Similar to how `Litestar` works, keywords that are neither path parameters or reserved keywords are considered query parameters.
29+
30+
The return type can be set either:
31+
- via the `return_model` keyword in `validate` decorator, or
32+
- by annotating the function return type. (`return_model` keyword takes priority)
33+
34+
Sequences/iterables can also be used for return type, e.g.; `list[ResponseModel]`.
35+
36+
The successful response status code can also be changed in two ways:
37+
- via setting the `status_code` keyword in `validate` decorator, or
38+
- by using the standard Flask syntax of returning a tuple.
39+
40+
Returning a tuple with a status code will override the value set by the `status_code` keyword.
41+
42+
As you might have noticed mixing these together most likely will cause issues or just going to be annoying to annotate.\
43+
***Avoid using the standard Flask tuple return syntax to change the status code if you are also using the function return type to annotate the return model. This will cause issues; first with a static type checker, then with the code handling return values and conversion.***\
44+
In my opinion the response model should be set via annotating the return type of the function and if the status code needs to be changed use the `status_code` keyword. Additionally, if you need to set headers, then set both, the return type and status code using the keywords of the `validate` decorator.
45+
46+
You can use `msgspec.Struct`, `dataclass`, and most built-in types natively supported by `msgspec`.
47+
48+
*More examples and others stuff will be added soon:tm:.*

flask_msgspec/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .core import validate
2+
3+
__all__ = ("validate",)

0 commit comments

Comments
 (0)