python / python/mypy

Redefinition using the same type should not be an error

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

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

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Possibly related to #10563.

Bug Report

Consider the following:

# a.py

x: int = 5
# b.py

from a import *

x: int = 7 # error: Name "x" already defined (possibly by an import)  [no-redef]

I would not call it an error. Sure, it's already defined, but it was an int originally.

If you do this:

# c.py

from a import *

x: float = 7.5 # error: Name "x" already defined (possibly by an import)  [no-redef]

You get the same error, but in this case, I'd say that's correct, because maybe I didn't realize it was an int.

And this:

# d.py

from a import *

x = 'boo' # error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]

This I also agree with.

Expected Behavior
I would argue that I should not get an error in b.py, whereas the errors in c.py and d.py are correct.

Typehints to me are as much a documentation tool as they are indispensable to finding bugs. Not being able to reiterate that something is an int means that I cannot document it in the code. I believe this is particularly important with star-imports because by just reading the current file, you have no idea what's being imported.

Here's something else to consider:

# a.py

x: float = 3.14
# b.py

from a import *

x = 5 # no error, because 5 is a float.

Do we know that the author new that x is a float and thus 5 is OK, or did they really mean to write x: int = 5? If I were allowed to write x: float = 5, we would have a definitive answer.

  • Mypy version used: 1.3.0
  • Mypy configuration options from mypy.ini (and other config files): ignore_missing_imports = True
  • Python version used: 3.8.10

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

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

はじめの一歩

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

調査の方向性

まず、報告された mypy の設定と Python のバージョンで、a.py、b.py、c.py、d.py の例を再現します。同じ型の再定義、異なるアノテーション、互換性のない代入について診断結果を比較します。期待されるケースが一貫して区別され、報告された動作に対するリグレッションカバレッジがあれば完了です。

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

評価

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

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

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