Skip to content

Custom property/descriptor and base classes #5803

Closed

Description

@mm-matthias

I am trying to get this code working:

from abc import ABC, abstractmethod
from typing import Any


class DataProperty:
    def __get__(self, instance: Any, owner: Any) -> str:
        return "X"


class BadBase1(ABC):
    @property
    @abstractmethod
    def id(self) -> str:
        raise NotImplementedError()


class BadBase2(ABC):
    id: str


class GoodBase(ABC):
    pass


class BadDerived1(BadBase1):
    id = DataProperty()   # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase1" defined the type as "str")


class BadDerived2(BadBase2):
    id = DataProperty()   # Incompatible types in assignment (expression has type "DataProperty", base class "BadBase2" defined the type as "str")


class GoodDerived(GoodBase):
    id = DataProperty()


reveal_type(BadDerived1().id)  # Revealed type is 'Any'
reveal_type(BadDerived2().id)  # Revealed type is 'Any'
reveal_type(GoodDerived().id)  # Revealed type is 'builtins.str'

I run mypy without any flags, i.e. "mypy test.py". The mypy version is 0.641, using Python 3.6.5.

Basically I want to have some kind of base class that says "any subclasses will have a member called id and the type of this member is str.

The two cases labelled with "Bad" result in the errors shown in the comments ("incompatible types in assignment" and "revealed type is 'Any'".

The only case that works is the "Good" one, but it only works because the base class does not define anything.

Am I doing something wrong or is this a bug/limitation in mypy?

Activity

  1. gvanrossum commented on Oct 19, 2018

    @gvanrossum
    Member

    Looks like a bug in the inheritance checking to me. This is a special case, overriding a property with a descriptor.

  2. kedder commented on Jan 20, 2019

    @kedder

    Here's a simpler example:

    from typing import Any
    
    class D:
        def __get__(self, inst: Any, own: Any) -> int: return 0
    
    class A:
        x = 1
    
    class SuperA(A):
        x = D()
    
    reveal_type(A.x)
    reveal_type(SuperA.x)

    mypy-0.660 produces this output:

    tmp/descr.py:10: error: Incompatible types in assignment (expression has type "D", base class "A" defined the type as "int")
    tmp/descr.py:12: error: Revealed type is 'builtins.int'
    tmp/descr.py:13: error: Revealed type is 'Any'
    tmp/descr.py:13: error: Cannot determine type of 'x'
    

    I'd expect SuperA.x would be inferred as int, compatible with superclass A.x.

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

    bugmypy got something wrongfalse-positivemypy gave an error on correct codepriority-1-normaltopic-descriptorsProperties, class vs. instance attributestopic-inheritanceInheritance and incompatible overrides

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions