Skip to content
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

No error when subclass does not provide a value for ClassVar #14381

Open
saulbein opened this issue Jan 3, 2023 · 2 comments
Open

No error when subclass does not provide a value for ClassVar #14381

saulbein opened this issue Jan 3, 2023 · 2 comments
Labels
bug mypy got something wrong

Comments

@saulbein
Copy link

saulbein commented Jan 3, 2023

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 saulbein added the bug mypy got something wrong label Jan 3, 2023
@tmke8
Copy link
Contributor

tmke8 commented Jan 3, 2023

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

@bwo
Copy link
Contributor

bwo commented Sep 20, 2023

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?

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

No branches or pull requests

3 participants