facebook / facebook/pyrefly

`implicit-any-empty-container` false positive when assigning dict containing None to `dict[str, Any]`

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

Description

### Describe the Bug

Given the following code:

```py
from typing import Any

def from_response(response: dict[str, Any]) -> None:
response["meta"] = {"time_to_live": None}
```

Pyrefly reports:

```console
❯ target\debug\pyrefly.exe --version
pyrefly 1.2.0-dev.3

❯ target\debug\pyrefly.exe check ../privatebin/t.py
ERROR Cannot infer type of empty container; it will be treated as containing `Any` [implicit-any-empty-container]
--> C:\Users\raven\dev\github\privatebin\t.py:7:41
|
7 | response["meta"] = {"time_to_live": None}
| ^^^^
|
Consider adding a type annotation or initializing with a non-empty value
INFO 1 error
```

This looks closely related to https://github.com/facebook/pyrefly/issues/3593, which was closed as fixed.

To check whether this was already addressed by that fix, I built Pyrefly from commit c28caa75a93fc72a03bda06e85b19ebdf9d02a4e, but I was still able to reproduce the issue there.

In this case, `response` is explicitly annotated as `dict[str, Any]`, so I would expect the assigned value `{"time_to_live": None}` to be accepted without an `implicit-any-empty-container` error.

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BBdKQA6lEejETMMMHTDUaAfUrwe6ODAAUquOs2I6mCAGMGAbTgNKAGkHCAugEo6AWgB8dAHK50MRFLodMF0uvow5iIgNDAMqFEOdAC8dMBRDFwwSgy4SlAQAG4wUYY%2BfgC%2BgSA2IGSqYFCkhDk0UBQAxHQACqT1jXRoWHj4dCa%2BkGwArpSoGb6EEp0AyjAwdAAWDAzEcIgA9Ht1so2EvGx7MOh7mLgmcHtj6BPTsxC%2Be-K8dKgFqNCo2Fgo3GECmMzmQVwxAhcAW6DIDHWvlcRUocDeQRSUQAzIQAIwAJiiEmIuliyToUSslFMDGJ6HMMEo1DRDgkk3QXB4fBgmFcxlUZkKaxSAHJQeheDBRRJJQxXKoAI6TCCqPkAaxgpFcqBMJngcApooA7qhKOgZegQOUarqMkUAGLQGAUQY4AgkcjlIA

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

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example from the issue or its Pyrefly sandbox, then trace the `implicit-any-empty-container` diagnostic through the type inference and assignment handling for a `dict[str, Any]`. Done means the shown assignment no longer reports the diagnostic and a regression test covers the case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.