-
-
Notifications
You must be signed in to change notification settings - Fork 826
✨ Support common pydantic types #723
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
lachaib
wants to merge
32
commits into
fastapi:master
Choose a base branch
from
lachaib:lachaib/issue181
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.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
25a5c37
[FEATURE] Support for common pydantic types
lachaib 57f0cb7
Update docs to refer to Python 3.7+ instead of 3.6+
svlandeg 6039b4f
chore: remove email-validator from examples/testing dependencies
lachaib c38fefc
fix: correct is_pydantic_type for python 3.12
lachaib e0ad01b
Fix default value to URL instead of email
svlandeg 0ff28ba
Clarify pydantic as additional extra dependency throughout the docs
svlandeg 6dccd26
ignore type error for now
svlandeg 10e7986
remove unnecessary ignore statement
svlandeg 893d86e
simplify pydantic type conversion using pydantic.TypeAdapter
lachaib ac41a0c
Merge branch 'master' into lachaib/issue181
svlandeg 7608cb4
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 24daca1
Merge branch 'master' into lachaib/issue181
svlandeg 3d967ca
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] b1025d2
update docs to new format
svlandeg 464e60f
Merge branch 'master' into lachaib/issue181
svlandeg a5636c6
Merge branch 'master' into lachaib/issue181
svlandeg f3fc8d7
Merge branch 'lachaib/issue181' of https://github.com/lachaib/typer i…
svlandeg ac92b0f
Merge branch 'master' into lachaib/issue181
svlandeg eff275d
ignore statement from coverage (as discussed)
svlandeg b573667
Merge branch 'master' into lachaib/issue181
svlandeg 3ee2868
Merge branch 'master' into lachaib/issue181
svlandeg 9d1ef99
update tutorial files to use explicit Typer()
svlandeg 6fa8211
rename test files to be compatible with current master
svlandeg 8550962
also fix renaming in markdown file
svlandeg 8e0fb62
use new format to generalize test files with a fixture
svlandeg 5282cad
Merge branch 'master' into lachaib/issue181
svlandeg 4717696
sync uv.lock
svlandeg d59a36f
🎨 Auto format
pre-commit-ci-lite[bot] 168a79e
use builtin tuple and list
svlandeg 640957e
configure importlib to avoid conflicts
svlandeg cf3ab72
add pydantic also to tests requirements in pyproject.toml
svlandeg f7881f4
more fixes
svlandeg 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| Pydantic types such as [AnyUrl](https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.AnyUrl) or [EmailStr](https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.EmailStr) can be very convenient to describe and validate some parameters. | ||
|
|
||
| Pydantic is installed automatically when installing Typer with its extra standard dependencies: | ||
|
|
||
| <div class="termy"> | ||
|
|
||
| ```console | ||
| // Pydantic comes with typer | ||
| $ pip install typer | ||
| ---> 100% | ||
| Successfully installed typer rich shellingham pydantic | ||
|
|
||
| // Alternatively, you can install Pydantic independently | ||
| $ pip install pydantic | ||
| ---> 100% | ||
| Successfully installed pydantic | ||
|
|
||
| // Or if you want to use EmailStr | ||
| $ pip install "pydantic[email]" | ||
| ---> 100% | ||
| Successfully installed pydantic, email-validator | ||
| ``` | ||
|
|
||
| </div> | ||
|
|
||
|
|
||
| You can then use them as parameter types. | ||
|
|
||
| {* docs_src/parameter_types/pydantic_types/tutorial001_an_py39.py hl[9] *} | ||
|
|
||
| {* docs_src/parameter_types/pydantic_types/tutorial002_an_py39.py hl[9] *} | ||
|
|
||
| These types are also supported in lists or tuples: | ||
|
|
||
| {* docs_src/parameter_types/pydantic_types/tutorial003_an_py39.py hl[9] *} | ||
|
|
||
| {* docs_src/parameter_types/pydantic_types/tutorial004_an_py39.py hl[9] *} | ||
Empty file.
15 changes: 15 additions & 0 deletions
15
docs_src/parameter_types/pydantic_types/tutorial001_an_py39.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,15 @@ | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(url_arg: Annotated[AnyHttpUrl, typer.Argument()]): | ||
| typer.echo(f"url_arg: {url_arg}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
13 changes: 13 additions & 0 deletions
13
docs_src/parameter_types/pydantic_types/tutorial001_py39.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,13 @@ | ||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(url_arg: AnyHttpUrl): | ||
| typer.echo(f"url_arg: {url_arg}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
15 changes: 15 additions & 0 deletions
15
docs_src/parameter_types/pydantic_types/tutorial002_an_py39.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,15 @@ | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(url_opt: Annotated[AnyHttpUrl, typer.Option()] = "https://typer.tiangolo.com"): | ||
| typer.echo(f"url_opt: {url_opt}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
13 changes: 13 additions & 0 deletions
13
docs_src/parameter_types/pydantic_types/tutorial002_py39.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,13 @@ | ||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(url_opt: AnyHttpUrl = typer.Option("https://typer.tiangolo.com")): | ||
| typer.echo(f"url_opt: {url_opt}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
17 changes: 17 additions & 0 deletions
17
docs_src/parameter_types/pydantic_types/tutorial003_an_py39.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,17 @@ | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| urls: Annotated[list[AnyHttpUrl], typer.Option("--url", default_factory=list)], | ||
| ): | ||
| typer.echo(f"urls: {urls}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
13 changes: 13 additions & 0 deletions
13
docs_src/parameter_types/pydantic_types/tutorial003_py39.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,13 @@ | ||
| import typer | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main(urls: list[AnyHttpUrl] = typer.Option([], "--url")): | ||
| typer.echo(f"urls: {urls}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
23 changes: 23 additions & 0 deletions
23
docs_src/parameter_types/pydantic_types/tutorial004_an_py39.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,23 @@ | ||
| from typing import Annotated | ||
|
|
||
| import typer | ||
| from pydantic import AnyHttpUrl, IPvAnyAddress | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| server: Annotated[ | ||
| tuple[str, IPvAnyAddress, AnyHttpUrl], | ||
| typer.Option(help="User name, age, email and social media URL"), | ||
| ], | ||
| ): | ||
| name, address, url = server | ||
| typer.echo(f"name: {name}") | ||
| typer.echo(f"address: {address}") | ||
| typer.echo(f"url: {url}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() |
20 changes: 20 additions & 0 deletions
20
docs_src/parameter_types/pydantic_types/tutorial004_py39.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,20 @@ | ||
| import typer | ||
| from pydantic import AnyHttpUrl, IPvAnyAddress | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| server: tuple[str, IPvAnyAddress, AnyHttpUrl] = typer.Option( | ||
| ..., help="Server name, IP address and public URL" | ||
| ), | ||
| ): | ||
| name, address, url = server | ||
| typer.echo(f"name: {name}") | ||
| typer.echo(f"address: {address}") | ||
| typer.echo(f"url: {url}") | ||
|
|
||
|
|
||
| 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
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.
48 changes: 48 additions & 0 deletions
48
tests/test_tutorial/test_parameter_types/test_pydantic_types/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,48 @@ | ||
| import importlib | ||
| import subprocess | ||
| import sys | ||
| from types import ModuleType | ||
|
|
||
| import pytest | ||
| from typer.testing import CliRunner | ||
|
|
||
| runner = CliRunner() | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| name="mod", | ||
| params=[ | ||
| pytest.param("tutorial001_py39"), | ||
| pytest.param("tutorial001_an_py39"), | ||
| ], | ||
| ) | ||
| def get_mod(request: pytest.FixtureRequest) -> ModuleType: | ||
| module_name = f"docs_src.parameter_types.pydantic_types.{request.param}" | ||
| mod = importlib.import_module(module_name) | ||
| return mod | ||
|
|
||
|
|
||
| def test_help(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["--help"]) | ||
| assert result.exit_code == 0 | ||
|
|
||
|
|
||
| def test_url_arg(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["https://typer.tiangolo.com"]) | ||
| assert result.exit_code == 0 | ||
| assert "url_arg: https://typer.tiangolo.com" in result.output | ||
|
|
||
|
|
||
| def test_url_arg_invalid(mod: ModuleType): | ||
| result = runner.invoke(mod.app, ["invalid"]) | ||
| assert result.exit_code != 0 | ||
| assert "Input should be a valid URL" in result.output | ||
|
|
||
|
|
||
| def test_script(mod: ModuleType): | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
| capture_output=True, | ||
| encoding="utf-8", | ||
| ) | ||
| assert "Usage" in result.stdout |
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.