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

Incompatible Override Self Distinction #5288

Closed
hmc-cs-mdrissi opened this issue Jun 14, 2023 · 2 comments
Closed

Incompatible Override Self Distinction #5288

hmc-cs-mdrissi opened this issue Jun 14, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@hmc-cs-mdrissi
Copy link

hmc-cs-mdrissi commented Jun 14, 2023

Describe the bug

from __future__ import annotations

from typing import Any, Generic, TypeVar, overload

T = TypeVar("T")


class Foo(Generic[T]):
    @overload
    def transform(self: Foo[int], x: list[int]) -> list[int]:
        ...

    @overload
    def transform(self, x: Any) -> Any:
        ...

    def transform(self, x: Any) -> Any:
        ...


class Bar(Foo[str]):
    def transform(self, x: str) -> dict[str, str]:
        ...

Produces a type error of incompatible override.

Expected behavior
I would expect no error. The Foo[int] overload should not be applicable because it's only if T = int, while Bar has T set to str.

VS Code extension or command-line
pyright 1.1.314

Additional context
The real version I hit with this error can actually be found with this definition in typeshed. One overload handles most common case as most of the time typevar has that specific type. Other overload fallsback to Any as I don't think it's possible to describe behavior in general today. That approach seems to produce incompatible overrides now.

I'll also note mypy --strict passes here and doesn't mark the override as incompatible. Unsure if it has self special rules or is just laxer here.

@hmc-cs-mdrissi hmc-cs-mdrissi added the bug Something isn't working label Jun 14, 2023
erictraut pushed a commit that referenced this issue Jun 14, 2023
…atibleMethodOverride` check when a child class is overriding an overloaded method in the base class and one or more of the overloads doesn't apply because the `self` or `cls` parameter is explicitly annotated in a way that's not applicable to the child class. This addresses #5288.
erictraut pushed a commit that referenced this issue Jun 14, 2023
…atibleMethodOverride` check when a child class is overriding an overloaded method in the base class and one or more of the overloads doesn't apply because the `self` or `cls` parameter is explicitly annotated in a way that's not applicable to the child class. This addresses #5288.
erictraut added a commit that referenced this issue Jun 14, 2023
…atibleMethodOverride` check when a child class is overriding an overloaded method in the base class and one or more of the overloads doesn't apply because the `self` or `cls` parameter is explicitly annotated in a way that's not applicable to the child class. This addresses #5288. (#5302)

Co-authored-by: Eric Traut <[email protected]>
@erictraut
Copy link
Collaborator

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

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jun 14, 2023
@erictraut
Copy link
Collaborator

This is included in pyright 1.1.315, which I just published. It will also be included in this week's insiders 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