python / python/mypy

False positive on re.compile as attrs converter

オープン
#5,313 コメント 4 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug false-positive priority-1-normal topic-attrs topic-type-variables
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

I've found what I think is a bug in mypy's attrs support.

# test.py
import re
import attr

@attr.s
class Test:
    foo = attr.ib(converter=re.compile)

test = Test("foo")
$ mypy test.py
test.py:8: error: Argument 1 to "Test" has incompatible type "str"; expected "Union[AnyStr, Pattern[AnyStr]]"

Unless I've completely misunderstood what AnyStr is for, I don't think that signature should throw on receiving a string.

I can't quite figure out if attrs or mypy is at fault here, but I can't get the same error to appear on re.compile when it's not being applied as a converter by attrs. Apologies if I've missed an obvious issue here.

Python v3.6.5
Mypy v0.610
Attrs v18.1.0

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

示されている test.py を使ってまず報告を再現し、re.compile 単体の場合の mypy の処理と、attrs のコンバーターとして扱った場合の処理を比較してください。attrs コンバーターの型推論と、それによって行われるコンストラクター引数のチェックを追跡してください。Test("foo") が、有効なコンバーターの型付けを弱めることなく受け入れられれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。