[BUG] List values get split by character when prompted
See original GitHub issueDescribe the bug
When an option’s type is List[str], the input gets split by character, as it would if I just called list("string")
To Reproduce
Steps to reproduce the behavior with a minimum self-contained file.
Replace each part with your own scenario:
- Create a file
main.pywith:
import typer
from typing import List
app = typer.Typer()
@app.command()
def main(
prefix: List[str] = typer.Option(
...,
"-p",
"--prefix",
help="Prefix to filter, can specify multiple times",
prompt="Prefix",
)
):
typer.echo(f"{prefix=}")
if __name__ == "__main__":
app()
- Call it with:
python main.py
- It outputs:
Prefix [()]: abcd qwerty
prefix=('a', 'b', 'c', 'd', ' ', 'q', 'w', 'e', 'r', 't', 'y')
- But I expected it to output:
Prefix [()]: abcd qwerty
prefix=('abcd', 'qwerty')
or
prefix=('abcd qwerty')
- When using multiple flags, it is correct:
python migrate_dcos/test_strings.py -p abcd -p qwerty
prefix=('abcd', 'qwerty')
Expected behavior
I expected the values to be automatically (or with an Option argument) split on spaces, rather than the single string getting cast to a tuple.
Environment
- OS: macOS
- Typer Version: 0.1.0
- Python version: Python 3.8.1
Additional context
I can work around it for space and comma-delimited values with:
def fix_strlist_value(value):
if " " in value or "," in value or all(len(i) == 1 for i in value):
return [k for k in "".join(value).replace(",", " ").split(" ") if k]
return value
but I’d rather not have to.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Python: Split string by list of separators - Stack Overflow
I cannot reply to sven's answer above, but I split on one or more of the characters inside the brackets, and don't have...
Read more >Split string by delimiter and get N-th element
Use cut with _ as the field delimiter and get desired fields: ... into one empty element, but would give an empty list...
Read more >VBA Split Function - Split String of Text into Array
The VBA Split function splits a string of text into substrings based on a specific delimiter character (e.g. a comma, space, or a...
Read more >$split (aggregation) — MongoDB Manual
The $split operator returns an array. The <string expression> and <delimiter> inputs must both be strings. Otherwise, the operation fails with an error....
Read more >Using PowerShell to split a string into an array - SQLShack
I have used the following functions of PowerShell. ... If you want to split the string based on the specific character, then you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yup, sorry, thought that would have happened already.
This is still a practical problem. Lists of strings are a common thing in python. It seems there are work-arounds in click, but I can’t figure out how to apply them to typer.
I did not find a ticket opened in click for a request to support this.
If someone could suggest how to apply this click fix to typer, I’d be willing to write it up for the official typer documentation:
https://stackoverflow.com/questions/47631914/how-to-pass-several-list-of-arguments-to-click-option