Skip to content

Subclassing a contextmanager fails #2469

Closed

Description

@euresti
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*]'

Activity

  1. JukkaL commented on Jan 16, 2017

    @JukkaL
    Collaborator

    You can use # type: ignore as a workaround.

  2. thomasballinger commented on Jan 19, 2017

    @thomasballinger
    Contributor

    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'
    
  3. FuegoFro commented on Aug 1, 2017

    @FuegoFro
    Contributor

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

  4. JukkaL commented on Aug 8, 2017

    @JukkaL
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions