`NamedTuple`s cannot have nested classes
Đang mở
@sobolevn đang làm issue này rồi.
Từ ngày 29/7/2023.
bug
topic-named-tuple
- 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ả
Python allows class definitions inside NamedTuple bodies:
>>> from typing import NamedTuple
>>> class A(NamedTuple):
... class B: ...
...
>>> A.B
<class '__main__.A.B'>
>>> A().B
<class '__main__.A.B'>
But, mypy does not allow this:
from typing import NamedTuple
class A(NamedTuple):
class B: ... # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
reveal_type(A.B) # E: "type[A]" has no attribute "B"
# N: Revealed type is "Any"
I think that this is a bug that needs to be solved, because we don't have any reason not to allow this (my existing patch shows that semanal and typechecker are happy with allowing these classes).
This is the root cause of https://github.com/python/mypy/issues/15752
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.
Đánh giá
Issue này chưa được đánh giá.