Skip to content

Improve invalid-argument diagnostic for union types #1411

@MichaReiser

Description

@MichaReiser

The invalid-argument diagnostic doesn't contain enough information for me to fix the diagnostic if the union gets truncated:

def arg(a: list[str] | str | dict[str, str] | list[int]| set[str] | int):
    assert len(a) == 1, (
        "ensure_config should not modify the original config"
    )
Argument to function `len` is incorrect: Expected `Sized`, found `list[str] | str | dict[str, str] | ... omitted 3 union elements` (invalid-argument-type) [Ln 2, Col 16]

The problematic element is int but it doesn't appear in the list.

We should improve the invalid-argument diagnostic to explicitly call out the argument that isn't compatible. Not only does that fix the issue with truncated union types, it also removes the need for users to second-guess which union element is the problematic one.

https://play.ty.dev/0ef049ec-72e9-4700-9fca-bfc1a45f965d

Related discussion astral-sh/ruff#20730 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.help wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions