-
-
Notifications
You must be signed in to change notification settings - Fork 736
✨ Implement list parsing from string with separators. #800
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
libklein
wants to merge
18
commits into
fastapi:master
Choose a base branch
from
libklein:multiple-arguments-via-separated-lists
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 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a6a797d
Implement list parsing from string with separators.
libklein 7eb3fd2
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] cec52c9
Do not allow multi-character separators for List[T] parsing.
libklein f1ca3bd
Move multiple separator error checking to option construction.
libklein aca33a5
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 46b4690
Fix linter errors.
libklein 7c78c51
Ignore mutable data structures error for added tutorial file.
libklein c0aba28
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 6233289
Use typing.List instead of list in test_others.
libklein f09da73
Do not collect coverage on unused command in test_others.
libklein 3cdd682
Add documentation.
libklein 03558cb
Rename `multiple_separator` to `separator`.
libklein e54193b
Fix missing word in documentation
libklein e4781dd
update documentation
svlandeg 1793f0f
Extend error message to point to valid separators
svlandeg ae11002
Update error msg in test
svlandeg de078a6
Merge branch 'master' into multiple-arguments-via-separated-lists
svlandeg d5328fa
Update tests to clarify that whitespace between options is unsupported.
libklein 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import List | ||
|
||
import typer | ||
|
||
|
||
def main(number: List[float] = typer.Option([], multiple_separator=",")): | ||
print(f"The sum is {sum(number)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
12 changes: 12 additions & 0 deletions
12
docs_src/multiple_values/multiple_options/tutorial003_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,12 @@ | ||
from typing import List | ||
|
||
import typer | ||
from typing_extensions import Annotated | ||
|
||
|
||
def main(number: Annotated[List[float], typer.Option(multiple_separator=",")] = []): | ||
print(f"The sum is {sum(number)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
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
44 changes: 44 additions & 0 deletions
44
tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial003.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,44 @@ | ||
import subprocess | ||
import sys | ||
|
||
import typer | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.multiple_values.multiple_options import tutorial003 as mod | ||
|
||
runner = CliRunner() | ||
app = typer.Typer() | ||
app.command()(mod.main) | ||
|
||
|
||
def test_main(): | ||
result = runner.invoke(app) | ||
assert result.exit_code == 0 | ||
assert "The sum is 0" in result.output | ||
|
||
|
||
def test_1_number(): | ||
result = runner.invoke(app, ["--number", "2"]) | ||
assert result.exit_code == 0 | ||
assert "The sum is 2.0" in result.output | ||
|
||
|
||
def test_2_number(): | ||
result = runner.invoke(app, ["--number", "2, 3, 4.5"], catch_exceptions=False) | ||
assert result.exit_code == 0 | ||
assert "The sum is 9.5" in result.output | ||
|
||
|
||
def test_3_number(): | ||
result = runner.invoke(app, ["--number", "2, 3, 4.5", "--number", "5"]) | ||
assert result.exit_code == 0 | ||
assert "The sum is 14.5" in result.output | ||
|
||
|
||
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 |
44 changes: 44 additions & 0 deletions
44
tests/test_tutorial/test_multiple_values/test_multiple_options/test_tutorial003_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,44 @@ | ||
import subprocess | ||
import sys | ||
|
||
import typer | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.multiple_values.multiple_options import tutorial003 as mod | ||
|
||
runner = CliRunner() | ||
app = typer.Typer() | ||
app.command()(mod.main) | ||
|
||
|
||
def test_main(): | ||
result = runner.invoke(app) | ||
assert result.exit_code == 0 | ||
assert "The sum is 0" in result.output | ||
|
||
|
||
def test_1_number(): | ||
result = runner.invoke(app, ["--number", "2"]) | ||
assert result.exit_code == 0 | ||
assert "The sum is 2.0" in result.output | ||
|
||
|
||
def test_2_number(): | ||
result = runner.invoke(app, ["--number", "2, 3, 4.5"]) | ||
assert result.exit_code == 0 | ||
assert "The sum is 9.5" in result.output | ||
|
||
|
||
def test_3_number(): | ||
result = runner.invoke(app, ["--number", "2, 3, 4.5", "--number", "5"]) | ||
assert result.exit_code == 0 | ||
assert "The sum is 14.5" in result.output | ||
|
||
|
||
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
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
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.