Skip to content

Inconsistent return type when calling overloaded function with Any argument #3662

Closed

Description

@JukkaL

The results of type checking the following fragment are unexpected:

from typing import Any, overload, Union

@overload
def f(x: int) -> int: ...
@overload
def f(x: Union[int, float]) -> float: ...
def f(x): pass

@overload
def g(x: int) -> int: ...
@overload
def g(x: float) -> float: ...
def g(x): pass

a: Any
reveal_type(f(a)) # Any   <--- okay
reveal_type(g(a)) # int   <--- unexpected

The correct result would arguably be Any for both of the calls. Union[int, float] should be equivalent to just float.

This seems to interact with the result type of os.path.join in Python 2 mode with #3300.

Activity

  1. ilevkivskyi commented on Aug 31, 2017

    @ilevkivskyi
    Member

    On current master both of them return int.

  2. emmatyping commented on Aug 31, 2017

    @emmatyping
    Member

    Interestingly, with strict-optional it reports

    main.py:4: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
    main.py:16: error: Revealed type is 'Any'
    main.py:17: error: Revealed type is 'builtins.int'
    

    EDIT: this is on 0.521

  3. ilevkivskyi commented on Aug 31, 2017

    @ilevkivskyi
    Member

    Something strange happens, I just installed from master on another machine and it gives me:

    tst22.py:16: error: Revealed type is 'Any'
    tst22.py:17: error: Revealed type is 'builtins.int'
    

    both with and without --strict-optional. I tried this twice, probably my previous comment is just wrong.

  4. emmatyping commented on Aug 31, 2017

    @emmatyping
    Member

    Strange. Just to avoid concerns about inconsistencies, I should have mentioned I was using 0.521, so it seems the overload issue was fixed in master.

  5. self-assigned this
    on Sep 5, 2017
  6. added a commit that references this issue on Sep 5, 2017
    d98d0eb
  7. added a commit that references this issue on Sep 5, 2017
    b724edc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions