Skip to content

False positive reportAssignmentType for 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

    good first issueGood for newcomersrejected upstreamalso a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions