pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

list.append(click.style(...)) causes LiteralString bad-argument-type error

Open lost-theory opened this issue 7 months ago • 1 comments

Describe the Bug

Appending a str value returned from click.style (click==8.2.1) to a list results in a confusing bad-argument-type error:

import click

def main() -> None:
    out = []
    apple = click.style("apple", fg="red")
    out.append(apple)
    reveal_type(click.style)
    reveal_type(apple)
    reveal_type(out)
    print(" ".join(out))

if __name__ == "__main__":
    main()

Pyrefly error:

$ ~/sand2-env/bin/pyrefly --version
pyrefly 0.16.2

$ ~/sand2-env/bin/pyrefly check --python-interpreter ~/sand2-env/bin/python dbg_list.py | grep -v reveal_type
ERROR ./dbg_list.py:6:16-21: Argument `str` is not assignable to parameter with type `LiteralString` in function `list.append` [bad-argument-type]
INFO ./dbg_list.py:7:5-29: revealed type: (text: Any, fg: int | str | tuple[int, int, int] | None = ..., bg: int | str | tuple[int, int, int] | None = ..., bold: bool | None = ..., dim: bool | None = ..., underline: bool | None = ..., overline: bool | None = ..., italic: bool | None = ..., blink: bool | None = ..., reverse: bool | None = ..., strikethrough: bool | None = ..., reset: bool = ...) -> str [reveal-type]
INFO ./dbg_list.py:8:5-23: revealed type: str [reveal-type]
INFO ./dbg_list.py:9:5-21: revealed type: list[LiteralString] [reveal-type]
INFO 7 errors shown, 0 errors ignored, 1 modules, 126 transitive dependencies, 41,576 lines, took 0.18s, peak memory physical 0 B

Mypy output for the same code:

$ ~/sand2-env/bin/mypy --strict dbg_list.py
dbg_list.py:7: note: Revealed type is "def (text: Any, fg: Union[builtins.int, tuple[builtins.int, builtins.int, builtins.int], builtins.str, None] =, bg: Union[builtins.int, tuple[builtins.int, builtins.int, builtins.int], builtins.str, None] =, bold: Union[builtins.bool, None] =, dim: Union[builtins.bool, None] =, underline: Union[builtins.bool, None] =, overline: Union[builtins.bool, None] =, italic: Union[builtins.bool, None] =, blink: Union[builtins.bool, None] =, reverse: Union[builtins.bool, None] =, strikethrough: Union[builtins.bool, None] =, reset: builtins.bool =) -> builtins.str"
dbg_list.py:8: note: Revealed type is "builtins.str"
dbg_list.py:9: note: Revealed type is "builtins.list[builtins.str]"
Success: no issues found in 1 source file

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

lost-theory avatar May 23 '25 02:05 lost-theory

Thanks for reporting this! We'll have the team take a look and see which milestone we want to put this in.

lolpack avatar May 23 '25 21:05 lolpack

I tested with pyrefly 0.33.1, and pyrefly outputs the same revealed types as mypy now (and no other errors).

rchen152 avatar Sep 22 '25 20:09 rchen152