Skip to content

error: Return type of <functionName> incompatible with supertype <ClassName> #3576

Closed

Description

@YuvalPruss

I got a code that looks like this:

from typing import NamedTuple, Dict, Optional, List, Any


class Object1(object, metaclass=ABCMeta):
    def __init__(self) -> None:
        pass

    @abstractmethod
    def get_logo(self) -> Optional[str]:
        pass


class Object2(Object1):
    def __init__(self) -> None:
        super().__init__()

    async def get_logo(self) -> Optional[str]:
        return ''


class Object3(Object1):
    def __init__(self) -> None:
        super().__init__()

    async def get_logo(self) -> Optional[str]:
        return None

And the mypy alerts me that:

test.py:19: error: Return type of "get_logo" incompatible with supertype "Object1"
test.py:27: error: Return type of "get_logo" incompatible with supertype "Object1"

But it's look OK to me, because they return the same type...
I run it with: mypy --strict-optional --ignore-missing-imports <file>

Activity

  1. JukkaL commented on Jun 20, 2017

    @JukkaL
    Collaborator

    Object doesn't use async def but subclasses use it, so the actual return types are different, though the annotations look the same.

  2. YuvalPruss commented on Jun 20, 2017

    @YuvalPruss
    Author

    Great! It's corrects my errors, but let me get that straight- what is the difference in the type annotations between async def and def? Should I wrote something differently? Maybe use the typing.Coroutine as the returned type from the async defs?

  3. JukkaL commented on Jun 20, 2017

    @JukkaL
    Collaborator

    Mypy internally uses typing.Awaitable[x] as the return type for async def, but this wrapping happens automatically so your signatures are fine. You can use reveal_type(obj.method) to display the signature of a method as mypy sees it.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions