python / python/mypy

`TypeGuard` has no effect when nested in the presence of other problems

Đang mở
#17,138 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ả

Bug Report

A TypeGuard declared and used near the top of a function is sometimes rendered ineffective (i.e., there is no change to the type of the first argument to the TypeGuard even when the TypeGuard returns True), but only when there is an apparently-unrelated mypy-detectable problem later in the same function.

The problem does not occur if the TypeGuard definition is not nested within an outer function, or if the apparently-unrelated problem is corrected.

Because any erroneous errors triggered by this bug appear above the triggering problem in the error report, users will find it difficult to solve this issue if they approach the problem using a [run mypy, fix first issue, repeat] workflow.

To Reproduce

[mypy-play.net]

from __future__ import annotations
from typing_extensions import (
        TypeGuard,
        assert_type,
        )

def some_func(arg: object) -> None:
    '''must encapsulate the following in a func, or issue won't repro
    '''
    def tg_int(arg: object) -> TypeGuard[int]:
        '''must be nested: issue won't repro if decl'd at module scope'''
        b: bool
        return b

    # replacing 'arg' with a local var works just as well
    if tg_int(arg):                                                     # line 16
        assert_type(arg, int)                                           # line 17
                # expected: no error
                # got: [assert-type]
                #   "Expression is of type 'object', not 'int'"

    # this is wrong, but removing it causes the unexpected error to
    # vanish:
    var1 = non_existant_var  # type: ignore[name-defined]

Expected Behavior

The TypeGuard invocation at line 16 should change the type of arg to int when the if statement's condition is truthy. The assert_type() on line 17 should pass.

Actual Behavior

The TypeGuard has no effect on the type of its argument.

Your Environment

  • Mypy version used: 1.9.0, master (as provided by mypy-play.net as of 2024-04-17)
  • Mypy command-line flags: (none necessary)
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8, 3.12

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 với reproducer mypy-play.net được liên kết và chạy nó với mypy 1.9.0 hoặc master. Điều tra lý do TypeGuard lồng nhau không thu hẹp arg khi lỗi name-defined xuất hiện sau đó đang hiện diện; hoàn thành khi kiểm tra assert_type(arg, int) thành công trong khi lỗi sau đó vẫn bị bỏ qua.

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
Đặc tả rõ ràng
Mức phù hợp với người mới
45/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.