Skip to content

Subclassing a contextmanager fails #2469

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

Closed
euresti opened this issue Nov 18, 2016 · 4 comments
Closed

Subclassing a contextmanager fails #2469

euresti opened this issue Nov 18, 2016 · 4 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@euresti
Copy link
Contributor

euresti commented Nov 18, 2016

from contextlib import contextmanager
from typing import Iterator

class Foo(object):
    @contextmanager
    def bar(self):
        # type: () -> Iterator[int]
        yield 4

class Bar(Foo):
    @contextmanager
    def bar(self):
        # type: () -> Iterator[int]
        yield 3

reveal_type(Foo.bar)
reveal_type(Bar.bar)

Fails thus:

t5.py: note: In class "Bar":
t5.py:12: error: Signature of "bar" incompatible with supertype "Foo"
t5.py: note: At top level:
t5.py:17: error: Revealed type is 'def (*Any, **Any) -> contextlib.ContextManager[builtins.int*]'
t5.py:18: error: Revealed type is 'def (*Any, **Any) -> contextlib.ContextManager[builtins.int*]'
@JukkaL JukkaL added bug mypy got something wrong priority-1-normal labels Jan 16, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 16, 2017

You can use # type: ignore as a workaround.

@thomasballinger
Copy link
Contributor

thomasballinger commented Jan 19, 2017

Maybe the same bug, if so this is more general than contextmanager.

def arbitrary_decorator(f):
    return f

class Foo(object):
    @arbitrary_decorator
    def bar(self):
        # type: () -> int
        return 4

class Bar(Foo):
    @arbitrary_decorator
    def bar(self):
        # type: () -> int
        return 3

reveal_type(Foo.bar)
reveal_type(Bar.bar)

gives

tmp.py:12: error: Signature of "bar" incompatible with supertype "Foo"
tmp.py:16: error: Revealed type is 'Any'
tmp.py:17: error: Revealed type is 'Any'

@FuegoFro
Copy link
Contributor

FuegoFro commented Aug 1, 2017

Is this a duplicate of #1441? Also is this possibly related to the forward referencing issue listed in #2907 (comment)?

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 8, 2017

Yes, this looks like a duplicate of #1441. This likely isn't related to forward references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants