python / python/mypy

Descriptors and ClassVars

Open
#14,969 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Have came across this issue from pyright

To Reproduce

# This sample tests the case where a member access is performed through
# an object using a field that is annotated as a ClassVar. Normally this
# is disallowed, but it is permitted if the type of the ClassVar is
# a descriptor object.
from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar, Generic, Self, TypeVar, overload

T = TypeVar("T")


class Descriptor(Generic[T]):
    if TYPE_CHECKING:

        @overload
        def __get__(self, instance: None, owner) -> Self:
            ...

        @overload
        def __get__(self, instance: object, owner) -> T:
            ...

        def __get__(self, instance: object | None, owner) -> Self | T:
            ...

        def __set__(self, instance: object, value: T) -> None:
            ...

        def is_null(self) -> bool:
            ...


class Example:
    field1: ClassVar = Descriptor[str]()

    field2: ClassVar = ""

    def reset(self) -> None:
        self.field1 = ""

        # This should generate an error because field2 isn't
        # a descriptor object.
        self.field2 = ""

Expected Behavior

Copied @erictraut comment:

ClassVar should work fine. If the class variable is assigned a descriptor, then the class variable will not be overwritten if you set the same attribute at the object level. So it sounds like mypy has a bug here. Pyright had a similar bug, which I recently fixed. Please report the bug to the maintainers of mypy.

Actual Behavior

classvar.py:39: error: Cannot assign to class variable "field1" via instance  [misc]
classvar.py:43: error: Cannot assign to class variable "field2" via instance  [misc]

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python reproducer with mypy 1.1.1 and compare the diagnostics for the descriptor-backed field1 and ordinary ClassVar field2. Trace the member-assignment handling involved in those diagnostics, then add regression coverage showing that descriptor-backed assignment is accepted while ordinary ClassVar assignment remains an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.