python / python/mypy

Redefinition using the same type should not be an error

Đang mở
#15,664 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các ví dụ trong a.py, b.py, c.py và d.py với cấu hình mypy và phiên bản Python được báo cáo. So sánh các chẩn đoán đối với việc định nghĩa lại cùng kiểu, các annotation khác nhau và phép gán không tương thích. Hoàn thành khi các trường hợp dự kiến được phân biệt một cách nhất quán, với độ bao phủ hồi quy cho hành vi được báo cáo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
38/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.