python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

False negative when accessing dataclass instance attribute as class attribute #17711

Open karlicoss opened 2 months ago

karlicoss commented 2 months ago

Apologies if it's a known issue, I searched for some time on the issue tracker, but no luck!

Bug Report

When you access a dataclass instance attribute as a class attribute, it crashes in runtime. However, mypy doesn't complain about it.

To Reproduce

from dataclasses import dataclass

@dataclass
class Cls:
    value: int

print(Cls.value)

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=2c0cec417b726435303ab8b25c07cccf

Expected Behavior

Mypy should complain about the missing attribute (or ideally even suggesting that you may be using instance attribute instead of class attribute if we want a nicer error message)

Actual Behavior

Mypy passes the check. Whereas in runtime we're getting

Traceback (most recent call last):
  File "/tmp/ff.py", line 7, in <module>
    print(Cls.value)
          ^^^^^^^^^
AttributeError: type object 'Cls' has no attribute 'value'

Your Environment

sobolevn commented 2 months ago

First of all, the same happens even without @dataclass decorator:

from typing_extensions import reveal_type

class My:
    x: int
    y: int = 0

reveal_type(My.y)
reveal_type(My.x)

This happens because mypy predates ClassVar type, so this is how it works. However, I think that we can change that for @dataclasses specifically.

karlicoss commented 2 months ago

Ah interesting, thanks! Yeah, makes sense to have it for dataclasses at least.