Skip to content

False positive reportAssignmentType with list.__add__ type widening from a newly-created object #1486

@Phrogz

Description

@Phrogz

Description

foo: list[str] = ["a", "b"]
bar: list[str | float] = foo + ["c"]     # incorrectly shows a type-widening error here
baz: list[str | float] = ["a", "b", "c"] # type-widening is correctly accepted here
bat: list[str | float] = foo             # correctly shows a type-widening error here

The bar line should be treated as the baz, as list + list creates a new list with no other references to it; mutating bar does not/cannot affect foo.

Link to BasedPyright Playground Example

Occurs in Pyright as well; filed upstream as Pyright bug #10943.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rejected upstreamalso a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address ittype checking / lintingissues relating to existing diagnostic rules or proposals for new diagnostic rulestypeshedcan this be upstreamed to typeshed? otherwise need option to disable nonstandard typeshed changes

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions