Skip to content

Wrong checking of overload for keyword-only args #1907

Closed

Description

@dmoisset

While writing some stubs I found an issue that I simplified to the following:

from typing import overload

@overload
def f(*, out: str, flag: bool=False) -> str: ...

@overload
def f(*, flag: bool=False) -> bool: ...

reveal_type(f())  # I expect bool, get Any
reveal_type(f(flag=False))  # I expect bool, get Any
reveal_type(f(out="xxx"))   # I expect str, get str

The checker shows a

error: Overloaded function signatures 1 and 2 overlap with incompatible return types

but actually the signatures do not overlap (there's no call that can match both the signatures).

Something that's also confusing is that if I change the order of the arguments like this

# Move the "out" argument to the end
@overload
def f(*, flag: bool=False, out: str) -> str: ...
@overload
def f(*, flag: bool=False) -> bool: ...

I stop getting the "signature overlap" error, but I still get the "Any" type on the first two example calls. Given that these arguments are keyword only their positions shouldn't matter at all

(Comes from python/typing#248 )

Activity

  1. added this to the milestone on Jul 21, 2016
  2. removed this from the milestone on Mar 29, 2017
  3. euresti commented on Dec 14, 2017

    @euresti
    Contributor

    Very weirdly it's the kwonly that's causing issues:

    This code has no issues:

    @overload
    def f(x: int) -> int: ...
    @overload
    def f(x: str) -> str: ...
    

    But make those kwonly and boom:

    @overload
    def f(*, x: int) -> int: ...   # error: Overloaded function signatures 1 and 2 overlap with incompatible return types
    @overload
    def f(*, x: str) -> str: ...
    
  4. ilevkivskyi commented on May 17, 2018

    @ilevkivskyi
    Member

    The Any return is now fixed, but the overlap error still appears, so this is a false positive.

  5. Michael0x2a commented on Jun 15, 2018

    @Michael0x2a
    Collaborator

    This took way way longer to resolve then I originally thought it would take, but the above examples are working as expected now in master after f61c2ba.

    There's still a few todos related to handling edge cases w.r.t. star args, optional args, and such in #5163 that needs to land before we can really call this done, but I'm going to close this mostly so I can prune the list of overload-related things I have left to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions