Skip to content

Many false negatives in behaviors involving an aggregate that starts empty, is later mutated, and is finally returned #5749

Backlog

Description

Describe the Bug

I was having trouble seeing reason to turn on implicit-any-empty-container until a colleague shared this sandbox with me.

Taking just the first example from the sandbox:

def find_crates_iadd(crates: list[Crate]) -> list[Crate]:
    found = []
    for crate in crates:
        found += [crate]
    found += [3]
    return found

It's not hard to spot what is the type error there - I would have guessed that Pyrefly in its default configuration would flag such a function. Is this a shortcoming, or am I not understanding what kinds of problems Pyrefly is intended to solve, such that it needs implicit-any-empty-container and/or a type annotation on the first appearance of found in order to see and alert that there is an invalidity in this code? (I happen to be coming to Pyrefly from pytype, so that may color my expectations somewhat.)

(It may be that we are using Pyrefly 1.3.2.)

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pVZNj9owEP0r1nIgqJCopadWPVR76KWHSlVPgFCIZ0kE2Ch2FlDV_943_iDJwiJWGwmRxDPj8XtvZvKGlnluUm0pPaJzUHJeiOqptbbLnZYNqqytSJIVAxzezNVAfEzF96Jods02t7zRobKl-PBNJL88sALbGwEoU3QmUjJL6WjxPxaACIag2RVMpeSy4FTMssqlTPx9KH6X48IpsfMcMn3SjZIAdgZm_HMtnDdrK4RpBe-tkeDMLZ194tvpYm7n1tsOnEjIfA3rKLWT8Rn8URulDyq4hzLzVr0rhmBJdlL32H1KxY9aN54gZbXIhQQFQj85WyiboVmzScDmVVjYj3vkuLsSEVqdloE4wPT33z0wESPq3s7mD8F5_hCOG3oVbJhDuLcbdEL0d56RXHRIulwM-kimo5fgu-5lHPo3wG-jeQK7yF9Hx5MwTcVjScWGSQAk6DCSb3MlaLe3fkeR4Lh81iNG5ZZUcjSjsUgLvT8lo5FYEcCkqHDPGn55s7WRtzu1G7Zxr1-KtgWof8CoTh7R-d6AFSjpTSJtxeptDJfBVcV-TsVPrTfNfmJLUhOUNr5p8DVTlCQOJTEIa7I4bU0Gp-dBtnL7OXE_53XFHyweIVQ8He_XdQ83v-PdYj4iKpMKSbOjyzGKu9PoIHC0o67Co-PlQD5n4fvIRSAW-7RLkk8ZgdojXeXH2926ris70nQ50wYi9uIwHiAVDMN1Aw-US8rE_iYSpbV78yXLoruu1xmpTOrCZD2PzOGdP-eV-xLthxPazWCDsO8ZpkM_S4e3RumQJyksBjzetlVR2UmuThNXtxMkZTFdqe4Y3hyj_wE

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added theissue type on Nov 25, 2026
  2. added
    google
    on Nov 25, 2026
  3. stroxler commented on Nov 25, 2026

    @stroxler
    Contributor

    This bug report exposes a number of gaps in partial type inference. A lot of them have to do with cycle handling restrictions we put in place intentionally: partial types have to be pinned by exactly one binding, and for various reasons if the read flows through a LoopPhi which might involve reassignment (for example through +=)

    I think now that we have a fixpoint, it may be possible to relax some of these restrictions. But I do want to make it clear to anyone who picks this up that

    • this is one issue but should not be one PR, the different cases flagged are separate fixes; you might want to split this issue up before working on a PR
    • the fixes may be nontrivial to ship. It's possible this should be left to a Pyrefly contributor to eventually pick up

    cc @QEDady if you really want to dig into Pyrefly internals, this might be an interesting one to chip away on, I'm guessing it came up in a google codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    googletypechecking

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions