Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in handling overloads #3575

Open
tigerhawkvok opened this issue Jun 14, 2022 · 6 comments
Open

Bug in handling overloads #3575

tigerhawkvok opened this issue Jun 14, 2022 · 6 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working spec compliance

Comments

@tigerhawkvok
Copy link

Describe the bug

An overloaded function with return specifications based on literal True/False will raise a type error on wrappers passing a bool

To Reproduce

Full example below

Expected behavior

Account for all the overloads

Screenshots or Code

from typing import Union, Literal, overload



@overload
def debugTyping(foo:Literal[True]) -> str: ...

@overload
def debugTyping(foo:Literal[False]) -> int: ...

def debugTyping(foo:bool= False) -> Union[str, int]:
    """
    Sample internal
    """
    if foo:
        return "foo"
    else:
        return 1

def typingWrapper(bar:bool= True) -> Union[str, int]:
    """
    Sample for real
    """
    return debugTyping(bar)


if __name__ == "__main__":
    print(typingWrapper(True))
    print(typingWrapper(False))

image

If your code relies on symbols that are imported from a third-party library, include the associated import statements and specify which versions of those libraries you have installed.

VS Code extension or command-line
vscode pylance extension: v2022.06.10

@erictraut
Copy link
Collaborator

This is correct behavior. A bool value does not match Literal[True] or Literal[False]. If you want to handle bool, you will need to specify a third overload that indicates the intended return type in that case. This behavior is consistent with other type checkers like mypy.

@tigerhawkvok
Copy link
Author

This isn't correct behaviour. Overloads for true and false are a completely containing set of the domain of bool.

(Further, you'll note the base function definition does contain the bool signature)

@erictraut
Copy link
Collaborator

There are cases where the contract for an overloaded function calls for passing either a literal False or a literal True. That is what you have specified. If your intent is to accept non-literal (bool) arguments, then you need to provide a third overload that specifies this.

The implementation signature is not used for purposes of type checking calls that target an overload.

@erictraut erictraut added the as designed Not a bug, working as intended label Jun 14, 2022
@tigerhawkvok
Copy link
Author

Even given the rest (which I'd still argue specifying every value of a domain obviates a redundant definition of the domain itself, for any domain; just bool is the trivial version)

The implementation signature is not used for purposes of type checking calls that target an overload.

seems like a glaring violation of DRY.

@erictraut
Copy link
Collaborator

The behavior of overloads is specified in the PEP 484 standard that dictates the behaviors of Python type checkers.

If you feel strongly about it, you could propose and shepherd a change to the standard. I think it's likely to be accepted though because the current behavior has utility, and changing it would have backward compatibility implications for existing code bases.

@erictraut
Copy link
Collaborator

I recently submitted a draft update to the Python typing spec that clarifies the type checker behaviors for argument type expansion when evaluating calls to overloaded functions. The draft spec includes expansion of bool into Literal[False] and Literal[True]. This allows the code above to type check without errors.

This draft update hasn't yet been approved by the full Typing Council, but I think there's a good chance it will be in the near future.

@erictraut erictraut reopened this Jan 26, 2025
@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version spec compliance and removed as designed Not a bug, working as intended labels Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working spec compliance
Projects
None yet
Development

No branches or pull requests

2 participants