Descriptors and ClassVars
オープン
まだ誰も着手していません。
bug
topic-descriptors
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python 再現コードを mypy 1.1.1 で実行し、descriptor-backed な field1 と通常の ClassVar field2 に対する診断を比較します。次に、それらの診断に関係するメンバー代入の処理を追跡し、descriptor-backed な代入が受け入れられる一方で、通常の ClassVar 代入は引き続きエラーになることを示す回帰テストカバレッジを追加します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100