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

regression involving subclass and overloaded constructor #3927

Closed
cleoold opened this issue Sep 10, 2022 · 4 comments
Closed

regression involving subclass and overloaded constructor #3927

cleoold opened this issue Sep 10, 2022 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@cleoold
Copy link

cleoold commented Sep 10, 2022

Describe the bug
Following code reports error. If we move the method to the parent class the error disappears. If we explicitly annotate the inner() with -> Iterable[ContainerSub[T_co]] instead of a generator the error also disappears. Replacing the __init__'s arg with Union instead of overloading can also avoid the error.

To Reproduce
Steps to reproduce the behavior.

Expected behavior
Code used to type check before 1.1.262

Screenshots or Code

T_co = TypeVar('T_co', covariant=True)

class Container(Generic[T_co]):
    @overload
    def __init__(self, arg: Iterable[T_co]): ...
    @overload
    def __init__(self, arg: Callable[[], Iterable[T_co]]): ...
    def __iter__(self) -> Iterator[T_co]: ...


class ContainerSub(Container[T_co]):
    def method(self) -> 'ContainerSub[ContainerSub[T_co]]':
        def inner():  # Generator[ContainerSub[T_co@ContainerSub], None, None]
            for x in self:
                yield ContainerSub(lambda: [x])
        return ContainerSub(inner)
                   ^~~~~~~~~~~~~
error: Expression of type "ContainerSub[ContainerSub[ContainerSub[T_co@ContainerSub]]]" cannot be assigned to return type "ContainerSub[ContainerSub[T_co@ContainerSub]]"
    TypeVar "T_co@ContainerSub" is covariant
      TypeVar "T_co@ContainerSub" is covariant
        Type "ContainerSub[T_co@ContainerSub]" cannot be assigned to type "T_co@ContainerSub" (reportGeneralTypeIssues)

VS Code extension or command-line
pyright 1.1.270

Additional context
Add any other context about the problem here.

@erictraut
Copy link
Collaborator

Thanks for the bug report. This was due to an existing bug that was being masked previously. A change related to another bug fix exposed this one.

I'll need to investigate how to fix this without breaking other cases. In the meantime, you can work around this issue by providing an explicit return type annotation for inner.

        def inner() -> Generator[ContainerSub[T_co], None, None]:
            for x in self:
                yield ContainerSub(lambda: [x])

@erictraut erictraut added the bug Something isn't working label Sep 10, 2022
@cleoold
Copy link
Author

cleoold commented Sep 11, 2022

Thanks for the bug report. This was due to an existing bug that was being masked previously. A change related to another bug fix exposed this one.

I'll need to investigate how to fix this without breaking other cases. In the meantime, you can work around this issue by providing an explicit return type annotation for inner.

        def inner() -> Generator[ContainerSub[T_co], None, None]:
            for x in self:
                yield ContainerSub(lambda: [x])

thanks. I didn't know annotating Generator would also work

erictraut pushed a commit that referenced this issue Sep 19, 2022
… generic class constructor from within that class implementation. This addresses #3927.
@erictraut
Copy link
Collaborator

This will be fixed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Sep 19, 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