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

class inferred as abstract when it isn't. #8245

Closed
randolf-scholz opened this issue Jun 27, 2024 · 2 comments
Closed

class inferred as abstract when it isn't. #8245

randolf-scholz opened this issue Jun 27, 2024 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@randolf-scholz
Copy link

randolf-scholz commented Jun 27, 2024

Code sample in pyright playground

from abc import abstractmethod
from typing import Protocol

class Dataset[T_co](Protocol):
    r"""Protocol for Dataset class."""

    INFO_URL: str = NotImplemented
    r"""Web address containing documentational information about the dataset."""

    @abstractmethod
    def download(self) -> None: ...
    @abstractmethod
    def load(self) -> T_co: ...

    # mixin methods
    def info(self) -> None:
        if self.INFO_URL is NotImplemented:
            raise NotImplementedError("No INFO_URL provided for this dataset!")
        print(f"See {self.INFO_URL}")

class MyDataset(Dataset[int]):
    def download(self) -> None:
        print("Downloading")

    def load(self) -> int:
        return 42

MyDataset()  # ❌ Cannot instantiate abstract class "MyDataset"

It seems pyright infers that info is abstract due to the two lines:

        if self.INFO_URL is NotImplemented:
            raise NotImplementedError("No INFO_URL provided for this dataset!")

if NotImplementedError is replaced with another type of Exception, info is no longer inferred as abstract.

@randolf-scholz randolf-scholz added the bug Something isn't working label Jun 27, 2024
@randolf-scholz
Copy link
Author

Moreover, it seems this affects only Protocol types. If the base class is changes to abc.ABC or nothing, then it also disappears.

erictraut added a commit that referenced this issue Jun 27, 2024
… method implementation that conditionally raises `NotImplementedError`. This addresses #8245.
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jun 27, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.370

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