Cannot use nested class in type context via self

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

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

Đánh giá

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

Hướng nghiên cứu

Start by reproducing the nested Foo.Bar example in test.py with mypy and compare the reported assignment error. Trace the relevant type-context handling, then add coverage showing that a supported annotation lets self._bar receive Foo.Bar without an incompatible-types error.

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

Mô tả

feature priority-2-low

I cannot get the following program to type check:

from typing import *

class Foo:
    class Bar:
        pass

    def __init__(self) -> None:
        self._bar = None

    def start(self) -> None:
        self._bar = self.Bar()

I get the following error:

test.py:11: error: Incompatible types in assignment (expression has type "Bar", variable has type "None")

Trying to add types like:

self._bar = None  # type: Bar

Or:

self._bar = None  # type: self.Bar

Does not work.

Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Merge trung bình
1 ngày 18 giờ
Pull request đã merge (30 ngày)
54

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.

Issue khác của python/mypy

Tất cả issue của python/mypy

Issue tương tự

Thêm issue về Python

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.