mypy icon indicating copy to clipboard operation
mypy copied to clipboard

No error when subclass does not provide a value for ClassVar

Open saulbein opened this issue 3 years ago • 1 comments

Bug Report

I've been trying to mypy to typecheck subclasses that need to provide a required value for a ClassVar in a base class that cannot provide the value itself (only has an unintialized attribute marker). The problem is that badly implemented subclasses can ignore this attribute without mypy raising errors about it (though interestingly if the value provided has incorrect type, mypy will raise errors about that).

To Reproduce

from abc import ABC
from typing import ClassVar, Type


class Base(ABC):
    my_super_important_value: ClassVar[str]


class GoodSubclass(Base):
    my_super_important_value = "1337"


# I want mypy to error about an unimplemented variable but it doesn't
class BadSubclass(Base):
    pass


def print_abc(cls: Type[Base]) -> None:
    print(cls.my_super_important_value)


print_abc(GoodSubclass)
# 1337
print_abc(BadSubclass)
# AttributeError: type object 'BadSubclass' has no attribute 'my_super_important_value'

mypy playground

Expected Behavior

Expected mypy to show an error about the BadSubclass class not providing a value for my_super_important_value.

Actual Behavior

No errors.

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10

Note that the same code fails when using the Pyre type checker with this error:

14:0: Uninitialized attribute [13]: Attribute `my_super_important_value` inherited from abstract class `Base` in class `BadSubclass` to have type `str` but is never initialized. 

saulbein avatar Jan 03 '23 07:01 saulbein

I have a PR to fix this, #13797, but it doesn't look like it'll get merged.

tmke8 avatar Jan 03 '23 09:01 tmke8

Bump on this! It would be very useful.

Note that you can almost accomplish something like this this way:

from typing import ClassVar
from abc import abstractmethod


class Foo:

    @classmethod
    @property
    @abstractmethod
    def x(cls) -> str:
        pass


    @property
    @abstractmethod
    def y(self) -> str:
        pass

class Bar(Foo):
    y = 'hi' 


print(Bar().y)  # error: Cannot instantiate abstract class "Bar" with abstract attribute "x"  [abstract]

But mypy will also complain that "Only instance methods can be decorated with @property", and it will also accept an instance-level property definition for x.

@sobolevn is anything blocking @tmke8's PR?

bwo avatar Sep 20 '23 13:09 bwo