facebook / facebook/pyrefly

`implicit-any-empty-container` with empty list redefined through list comprehesion

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

Description

### Describe the Bug

```
def fn(x: int) -> int:
return x
l = []
l = [(fn(x)) for x in [1, 2, 3]]
```
The following with `strict` preset produces:
```
ERROR sandbox.py:3:5-7: Cannot infer type of empty container; it will be treated as containing `Any` [implicit-any-empty-container]
```

At the very least, it's inconsistent because using `l = list()` does not produce an error:

```
def fn(x: int) -> int:
return x
l = list()
l = [(fn(x)) for x in [1, 2, 3]]
```

It suggests adding a type annotation or initializing with non empty value, but `append` works and `l` is inferred correctly.
```
l = []
l.append(3)
```

`pyrefly` should be able to deduce that `l` is type `list[int]` from the list comprehension.

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSImMYABGOgBT6I0ToAuAlDQLQB8rDogA66GuJoAnGOwCuksflFQaAXhoBtALrK1mhvSaduYXJJr5BmgIwAaGgCZ7AZi070IWyDLSwUUoTsuAC2UBQAxDQACqS%2B-jRoWHiWAMa46JAA5vKo7BDphKKRAMowMDQAFuzsxHCIAPT1PtT%2BhGaZ9TDo9Zi4KXD1aRkQ2ZK5%2Bd10ZjSoAG6o0KjYsDRDWTl56TS4xJvocIXoZOwV6TyzMJJwE3rCIM6E1g53osTScDK3IHDskhAp7DuIAAvl5UACIBcAGLQGAURI4AgkcjAoA

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

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the linked Pyrefly sandbox and reproduce the strict-mode diagnostic for an empty list reassigned through a list comprehension. Trace the implementation of the implicit-any-empty-container check and list-comprehension inference. Done means the example infers list[int] without the diagnostic, while preserving the existing behavior for list(), append(), and other empty containers.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.