question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] List values get split by character when prompted

See original GitHub issue

Describe 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.py with:
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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
StephenBrown2commented, Apr 27, 2020

Yup, sorry, thought that would have happened already.

0reactions
poleguycommented, Jul 18, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found