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

Unsolved type variable when overloading a function with ellipsis as a default #3958

Closed
Jackenmen opened this issue Sep 17, 2022 · 2 comments
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Jackenmen
Copy link

Describe the bug
When I overload a function taking a TypeVar argument with that argument's default value being an Ellipsis (...), I get this warning:

Type variable "T" may go unsolved if caller supplies no argument for parameter "default"
  Provide an overload that specifies the return type when the argument is not supplied

To Reproduce

  1. Put code from "Screenshots or Code" into a file.
  2. Run pyright on it.
  3. See results:
No configuration file found.
No pyproject.toml file found.
stubPath /tmp/typings is not a valid directory.
Assuming Python platform Linux
Searching for source files
Found 1 source file
pyright 1.1.271
/tmp/example.py
  /tmp/example.py:25:27 - warning: Type variable "T" may go unsolved if caller supplies no argument for parameter "default"
    Provide an overload that specifies the return type when the argument is not supplied (reportInvalidTypeVarUse)
0 errors, 1 warning, 0 informations 
Completed in 0.648sec

Expected behavior
I expected there to be no warning in at least one of the variants I wrote.

Screenshots or Code
I tried two variants.

  • Variant number 1:
from types import EllipsisType
from typing import Generic, TypeVar, Union, overload


T = TypeVar("T")


class JsonValue(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value


@overload
def get(self) -> JsonValue[int]:
    ...

@overload
def get(self, *, default: EllipsisType) -> JsonValue[int]:
    ...

@overload
def get(self, *, default: T) -> JsonValue[T]:
    ...

def get(self, *, default: T = ...) -> Union[JsonValue[int], JsonValue[T]]:
    if default is ...:
        return JsonValue(123)
    return JsonValue(default)
  • Variant number 2:
from types import EllipsisType
from typing import Generic, TypeVar, Union, overload


T = TypeVar("T")


class JsonValue(Generic[T]):
    def __init__(self, value: T) -> None:
        self.value = value


@overload
def get(self, *, default: EllipsisType = ...) -> JsonValue[int]:
    ...

@overload
def get(self, *, default: T) -> JsonValue[T]:
    ...

def get(self, *, default: T = ...) -> Union[JsonValue[int], JsonValue[T]]:
    if default is ...:
        return JsonValue(123)
    return JsonValue(default)

VS Code extension or command-line
I'm running pyright 1.1.271 as a command-line tool.

Additional context
N/A

@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be addressed in the next release.

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Sep 17, 2022
erictraut pushed a commit that referenced this issue Sep 17, 2022
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.272. It will also be included in a future release of pylance.

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
Projects
None yet
Development

No branches or pull requests

2 participants