Should a method name shadow outer names when quoted or with PEP563?
Chưa có ai nhận issue này.
- 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ả
Bug Report
If a class defines a method with a name shadowing outer name, mypy always rejects it in annotations.
To Reproduce
Consider the following snippet (playground):
#from __future__ import annotations
from typing import get_type_hints, get_origin
class Foo:
def list(self) -> None: ...
def method(self) -> list[str]: # E: Function "__main__.Foo.list" is not valid as a type [valid-type]
return [""]
Foo().method().append("") # E: "list?[builtins.str]" has no attribute "append" [attr-defined]
if get_origin(get_type_hints(Foo.method)['return']) is list:
print("Resolved to builtins.list")
else:
print("Resolved as something else")
When run against it, mypy is producing a correct valid-type error, matching runtime behaviour.
$ mypy a.py
a.py:8: error: Function "a.Foo.list" is not valid as a type [valid-type]
a.py:8: note: Perhaps you need "Callable[...]" or a callback protocol?
a.py:11: error: "list?[builtins.str]" has no attribute "append" [attr-defined]
Found 2 errors in 1 file (checked 1 source file)
$ pyright a.py
/tmp/tmp.StzhaQlnmA/a.py
/tmp/tmp.StzhaQlnmA/a.py:8:25 - error: Expected class but received "(self: Self@Foo) -> None" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations
$ python a.py
Traceback (most recent call last):
File "/tmp/tmp.StzhaQlnmA/a.py", line 5, in <module>
class Foo:
...<3 lines>...
return [""]
File "/tmp/tmp.StzhaQlnmA/a.py", line 8, in Foo
def method(self) -> list[str]: # E: Function "__main__.Foo.list" is not valid as a type [valid-type]
~~~~^^^^^
TypeError: 'function' object is not subscriptable
Now let's uncomment the future import or quote list[int] (both behave equivalently).
from __future__ import annotations
from typing import get_type_hints, get_origin
class Foo:
def list(self) -> None: ...
def method(self) -> list[str]: # E: Function "__main__.Foo.list" is not valid as a type [valid-type]
return [""]
Foo().method().append("") # E: "list?[builtins.str]" has no attribute "append" [attr-defined]
if get_origin(get_type_hints(Foo.method)['return']) is list:
print("Resolved to builtins.list")
else:
print("Resolved as something else")
$ mypy a.py
a.py:8: error: Function "a.Foo.list" is not valid as a type [valid-type]
a.py:8: note: Perhaps you need "Callable[...]" or a callback protocol?
a.py:11: error: "list?[builtins.str]" has no attribute "append" [attr-defined]
Found 2 errors in 1 file (checked 1 source file)
$ pyright a.py
0 errors, 0 warnings, 0 informations
$ python a.py
Resolved to builtins.list
Runtime resolution also (unsurprisingly, probably) works as intended, so mypy does not model runtime semantics correctly?
Expected Behavior
Snippet with PEP563 enabled should pass mypy check
Actual Behavior
a.py:8: error: Function "a.Foo.list" is not valid as a type [valid-type]
a.py:8: note: Perhaps you need "Callable[...]" or a callback protocol?
a.py:11: error: "list?[builtins.str]" has no attribute "append" [attr-defined]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.14.1 and latest master
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.13
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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 chạy mypy trên reproducer được cung cấp với và không có chú thích được đặt trong dấu ngoặc kép hoặc với PEP 563 được bật, rồi so sánh kết quả với việc phân giải khi chạy và pyright. Theo dõi đường dẫn phân giải tên của chú thích đối với các tên phương thức che khuất các tên bên ngoài, sau đó bổ sung độ bao phủ cho các trường hợp đã minh họa và xác minh rằng các kiểm tra mong đợi đều đạt.
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
- compilers
- 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
- 42/100