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

PEP 544: failing example #3312

Open
BALOGHBence opened this issue Aug 28, 2023 · 1 comment
Open

PEP 544: failing example #3312

BALOGHBence opened this issue Aug 28, 2023 · 1 comment

Comments

@BALOGHBence
Copy link

Documentation

The following example fails on Python 3.8.10 for several reasons.

class PColor(Protocol):
    @abstractmethod
    def draw(self) -> str:
        ...
    def complex_method(self) -> int:
        # some complex code here

class NiceColor(PColor):
    def draw(self) -> str:
        return "deep blue"

class BadColor(PColor):
    def draw(self) -> str:
        return super().draw()  # Error, no default implementation

class ImplicitColor:   # Note no 'PColor' base here
    def draw(self) -> str:
        return "probably gray"
    def complex_method(self) -> int:
        # class needs to implement this
        
nice: NiceColor
another: ImplicitColor

def represent(c: PColor) -> None:
    print(c.draw(), c.complex_method())

represent(nice) # OK
represent(another) # Also OK

The first reason why this fails is a syntax error (actually two), namely the methods complex_method are missing the ellipses. If these are corrected, the example still throws a NameError, since 'nice' and 'another' are actually not defined.

The corrected example:

class PColor(Protocol):
    @abstractmethod
    def draw(self) -> str:
        ...
    def complex_method(self) -> int:
        # some complex code here
        ...

class NiceColor(PColor):
    def draw(self) -> str:
        return "deep blue"

class BadColor(PColor):
    def draw(self) -> str:
        return super().draw()  # Error, no default implementation

class ImplicitColor:   # Note no 'PColor' base here
    def draw(self) -> str:
        return "probably gray"
    def complex_method(self) -> int:
        # class needs to implement this
        ...
        
nice: NiceColor = NiceColor()
another: ImplicitColor = ImplicitColor()

def represent(c: PColor) -> None:
    print(c.draw(), c.complex_method())

represent(nice) # OK
represent(another) # Also OK
@AA-Turner AA-Turner transferred this issue from python/cpython Aug 28, 2023
@AA-Turner
Copy link
Member

cc: @ilevkivskyi @JukkaL @ambv

@hugovk hugovk changed the title failing example in PEP 544 PEP 544: failing example Sep 27, 2023
1chooo added a commit to 1chooo/peps that referenced this issue May 15, 2024
@1chooo 1chooo mentioned this issue May 15, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@AA-Turner @BALOGHBence and others