False "type assignment error" when assigning an instance of `Field` class or classes extended from it.
まだ誰も着手していません。
評価
調査の方向性
terminaltexteffects/utils/terminal.py での dataclass の使用と、terminaltexteffects/utils/argsdataclass.py での ArgField の定義から始め、mypy で代入エラーを再現します。dataclasses.Field とそのサブクラスに対する mypy の dataclass 処理を追跡します。示されている Field と ArgField の代入で、誤った代入エラーが発生しなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug Report
1- As you must know, there is no standard way in python to use decorators for defining metadata for a field of a class and the only possible approach is assigning an instance of Field class from dataclass module to the field. I believe it's weird and irrational but anyway it's now the standard defined in pep 0557 so we expect mypy to follow it.
2- there was an issue that describe mypy raising a false "Incompatible types in assignment" error in the simple following code:
@dataclass
class A:
x: Optional[List[int]] = field(default_factory=list)
3- It seems Sometime around Oct 2023, the problem solved but unfortunately as I can see it solved just in an special condition. in the other words, just when using the field factory method.
To Reproduce
If you remove the # type: ignore[assignment] directive at the end of the following code:
@dataclass
class TerminalArgs(ArgsDataClass):
tab_width: int = ArgField(
cmd_name=["--tab-width"],
type_parser=argtypes.positive_int,
default=4,
help="Number of spaces to use for a tab character.",
)# type: ignore[assignment]
(Also ArgField is accessible here)
mypy will raise errors like:
terminaltexteffects/utils/terminal.py:21: error: Incompatible types in assignment (expression has type "ArgField", variable has type "int") [assignment]
terminaltexteffects/utils/terminal.py:27: error: Incompatible types in assignment (expression has type "ArgField", variable has type "bool") [assignment]
terminaltexteffects/utils/terminal.py:33: error: Incompatible types in assignment (expression has type "ArgField", variable has type "bool") [assignment]
terminaltexteffects/utils/terminal.py:39: error: Incompatible types in assignment (expression has type "ArgField", variable has type "int") [assignment]
terminaltexteffects/utils/terminal.py:45: error: Incompatible types in assignment (expression has type "ArgField", variable has type "float") [assignment]
Expected Behavior
Allow assigning instance of Field class or instance of any class extended from it to fields of a dataclass.
Actual Behavior
Raising a false Incompatible types in assignment error
Your Environment
- Mypy version used: mypy 1.9.0 (compiled: yes)
- Mypy command-line flags: nothing special
- Mypy configuration options from
mypy.ini(and other config files): nothing special - Python version used: Python 3.11.2
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100