facebook / facebook/pyrefly

`Invalid literal expression` on `Literal` created at runtime

Open
#4,434 3 comments 0 reactions 0 assignees View on GitHub
pydantic
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

pyrefly will give an `invalid-literal` error on Literal values that are created at runtime, also when it can infer it to be a valid value for Literal.

``` python
from typing import Literal

l = ["a"]

run_time_val = Literal[l[0]] # Invalid literal expression [invalid-literal]
```

ty gives an equivalent error, pyright does not give an error (but pyright will also allow "illegal" types like `list`, so I'm not sure it's on purpose)

The python docs only has a small section about [`Literal` at run time](https://typing.python.org/en/latest/spec/literal.html#parameters-at-runtime) and it's not clear to me what is actually expected. However the above code runs fine and while it looks like something you wouldn't do, it actually triggered on production code because we're creating pydantic models dynamically.

The python docs only allows literal values but the above still gives an error when i restrict it to that with something like `l: list[Literal["a"]] = ["a"]`.

ty: https://play.ty.dev/b051aa63-62c1-43c5-92e1-2dd1ce6bccc3
pyright: https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAySMApiAIYA2AsAFB2VQC8UA2gETnsC6ddIAVxQB9GFhLCAblWaFiZKq0qsADN25A

### Sandbox Link

https://pyrefly.org/sandbox/?project=v2.I6HIhJZ6oBjIAbolOkYpMUYJnK-KSvPiSzKBcQdMK0AZqMLonGiD2FjMYFJWCIAIKEBtBBYQwPBNLwUWQ8Bw04vJUwaWQKkKGSUlBcVW-vow7flF6fqpefop-cnF-ig69BVAKSKxLDETXLihGqeQD47WYqCx2OPHhKj4UYdEjzq-2FEHRQ5QBd4ABwA

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the Python reproduction in the issue and the linked typing specification section on Literal parameters at runtime. Compare Pyrefly's result with the linked ty and pyright examples, then determine the expected behavior for an inferred runtime value. Done means the chosen behavior is implemented and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.