-
-
Notifications
You must be signed in to change notification settings - Fork 814
✨ Support json (dict) as parameter type
#985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mhkarimi1383
wants to merge
28
commits into
fastapi:master
Choose a base branch
from
mhkarimi1383:feat-bytes-json-options
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−2
Open
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a3ee10a
✨ feat: json and bytes field suppport in options
mhkarimi1383 1dd8d71
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 1f3d1cf
✏️ typo: fix linter issues
mhkarimi1383 c24f1a7
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 11292ff
📝 docs: added json arg type in docs_src
mhkarimi1383 137e1bc
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 0f5a1b3
🐛 fix: docs_src python version support
mhkarimi1383 432a48c
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 31e7623
📝 docs: added json arg type docs
mhkarimi1383 f83e798
📝 docs: just a typo
mhkarimi1383 2b480ad
:white_check_mark: DictParamType tests added
mhkarimi1383 fdb7bb4
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 114b761
:bug: DictParamType repl test fix
mhkarimi1383 4b75b07
Merge branch 'master' into feat-bytes-json-options
svlandeg 38163f0
rewrite example code to focus on dict functionality
svlandeg 71ca8e3
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 240d614
rename to dict instead of json
svlandeg 966206d
specific annotated and non-annotated version of the tutorial and test…
svlandeg 7caf522
remove default
svlandeg e0c7f78
Merge branch 'master' into feat-bytes-json-options
mhkarimi1383 55f3024
Merge branch 'master' into feat-bytes-json-options
svlandeg d0cb4ab
update tutorial files to use Typer() instance
svlandeg 2b38061
add new page to index
svlandeg 8c621c7
revert edit for bytes
svlandeg c076570
Merge branch 'master' into feat-bytes-json-options
svlandeg 56594ab
Merge branch 'feat-bytes-json-options' of https://github.com/mhkarimi…
svlandeg bb946fb
update tests to use mod.app
svlandeg 739ebd0
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Dict | ||
|
|
||
| You can declare a *CLI parameter* to be a standard Python `dict`: | ||
|
|
||
| {* docs_src/parameter_types/dict/tutorial001_an.py hl[8] *} | ||
|
|
||
| Check it: | ||
|
|
||
| <div class="termy"> | ||
|
|
||
| ```console | ||
| // Run your program | ||
| $ python main.py --user-info '{"name": "Camila", "age": 15, "height": 1.7, "female": true}' | ||
|
|
||
| Name: Camila | ||
| User attributes: ['age', 'female', 'height', 'name'] | ||
|
|
||
| ``` | ||
|
|
||
| </div> | ||
|
|
||
| This can be particularly useful when you want to include JSON input: | ||
|
|
||
| ```python | ||
| import json | ||
|
|
||
| data = json.loads(user_input) | ||
| ``` |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import typer | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(user_info: dict = typer.Option()): | ||
| print(f"Name: {user_info.get('name', 'Unknown')}") | ||
| print(f"User attributes: {sorted(user_info.keys())}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import typer | ||
| from typing_extensions import Annotated | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(user_info: Annotated[dict, typer.Option()]): | ||
| print(f"Name: {user_info.get('name', 'Unknown')}") | ||
| print(f"User attributes: {sorted(user_info.keys())}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
49 changes: 49 additions & 0 deletions
49
tests/test_tutorial/test_parameter_types/test_dict/test_tutorial001.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import json | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.parameter_types.dict import tutorial001 as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer() | ||
| app.command()(mod.main) | ||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "--user-info" in result.output | ||
| assert "DICT" in result.output | ||
|
|
||
|
|
||
| def test_params(): | ||
| data = {"name": "Camila", "age": 15, "height": 1.7, "female": True} | ||
| result = runner.invoke( | ||
| app, | ||
| [ | ||
| "--user-info", | ||
| json.dumps(data), | ||
| ], | ||
| ) | ||
| assert result.exit_code == 0 | ||
| assert "Name: Camila" in result.output | ||
| assert "User attributes: ['age', 'female', 'height', 'name']" in result.output | ||
|
|
||
|
|
||
| def test_invalid(): | ||
| result = runner.invoke(app, ["--user-info", "Camila"]) | ||
| assert result.exit_code != 0 | ||
| assert "Expecting value: line 1 column 1 (char 0)" in result.exc_info[1].args[0] | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout | ||
49 changes: 49 additions & 0 deletions
49
tests/test_tutorial/test_parameter_types/test_dict/test_tutorial001_an.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import json | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import typer | ||
| from typer.testing import CliRunner | ||
|
|
||
| from docs_src.parameter_types.dict import tutorial001_an as mod | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
| app = typer.Typer() | ||
| app.command()(mod.main) | ||
svlandeg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_help(): | ||
| result = runner.invoke(app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
| assert "--user-info" in result.output | ||
| assert "DICT" in result.output | ||
|
|
||
|
|
||
| def test_params(): | ||
| data = {"name": "Camila", "age": 15, "height": 1.7, "female": True} | ||
| result = runner.invoke( | ||
| app, | ||
| [ | ||
| "--user-info", | ||
| json.dumps(data), | ||
| ], | ||
| ) | ||
| assert result.exit_code == 0 | ||
| assert "Name: Camila" in result.output | ||
| assert "User attributes: ['age', 'female', 'height', 'name']" in result.output | ||
|
|
||
|
|
||
| def test_invalid(): | ||
| result = runner.invoke(app, ["--user-info", "Camila"]) | ||
| assert result.exit_code != 0 | ||
| assert "Expecting value: line 1 column 1 (char 0)" in result.exc_info[1].args[0] | ||
|
|
||
|
|
||
| def test_script(): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.