Skip to content

Nicer error messages for Union and Optional types #2561

@FuegoFro

Description

@FuegoFro

The error message you get when something is Optional and you try to access it isn't as friendly as it might be (with --strict-optional). Eg if I have

def foo(s: Optional[str]) -> None:
    print(s.count('a'))

It says error: Some element of union has no attribute "count".

It would be nicer if it said something like Variable 's' may be None or something a bit special cased to the Union with None case.

Generalizing a bit, one could imagine better handling of Unions in general by specifying what is in the Union or even which types in the Unions are causing the issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions