Skip to content

Better error message for overloading functions that return union type #2020

@binh-vu

Description

@binh-vu

Networkx, a popular library for working with graph, uses lots of overloading. One example is iterating through edges in the graph. It can returns an iterator of tuples of 3 or 4 items (if keys argument is true). For example:

g = nx.MultiDiGraph()
...
# pylance complains that: Tuple size mismatch: expected 3 but received 4
for uid, vid, edge_data in g.edges(data=True):
    pass

# pylance compalins that: Tuple size mismatch: expected 4 but received 3
for uid, vid, eid, edge_data in g.edges(data=True, keys=True):
    pass

The error message is quite confusing because at first it told me to use a tuple of 4, but then when I use tuple of 4, it told me to use a tuple of 3. It would be better if the error message could mention the union type so the developers know that they need to do type narrowing or use type guard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfixed in next version (main)A fix has been implemented and will appear in an upcoming version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions