python / python/mypy

False positive for Union[type, ...] and inspect.isclass()

Đang mở
#16,640 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 topic-type-narrowing
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

When a variable's type is declared as a union that includes type (or typing.Type[...]), mypy does not take into account whether usage of that variable occurs inside of an if inspect.isclass(...) conditional block, leading to false positives.

To Reproduce

gist
playground

Python code
from inspect import isclass

registry: dict[str, type] = {}

def decorator_factory(arg: str | type):
    if isclass(arg):
        registry[arg.__name__.lower()] = arg
        return arg
    else:
        def decorator(cls: type):
            registry[arg] = cls
            return cls
        return decorator
        
@decorator_factory
class Foo:
    pass
        
@decorator_factory("alt")
class Bar:
    pass

Expected Behavior**

mypy reports no errors

Actual Behavior

main.py:11: error: Invalid index type "str | type" for "dict[str, type]"; expected type "str"  [index]
Found 1 error in 1 file (checked 1 source file)

Workaround

Using typing.cast to narrow the union explicitly.

gist
playground

Python code
from inspect import isclass
from typing import cast, TYPE_CHECKING

registry: dict[str, type] = {}

def decorator_factory(arg: str | type):
    if isclass(arg):
        registry[arg.__name__.lower()] = arg
        return arg
    else:
        if TYPE_CHECKING:
            arg = cast(str, arg)
        
        def decorator(cls: type):
            registry[arg] = cls
            return cls
        return decorator
        
@decorator_factory
class Foo:
    pass
        
@decorator_factory("alt")
class Bar:
    pass

Your Environment

  • Mypy version used: 1.7.1
  • Mypy command-line flags: (mypy-play defaults)
  • Mypy configuration options from mypy.ini (and other config files): (mypy-play defaults)
  • Python version used: 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 bằng cách chạy reproducer được liên kết với mypy và so sánh lỗi được báo cáo với hành vi mong đợi liên quan đến inspect.isclass() và Union[type, ...]. Truy vết đường dẫn thu hẹp kiểu cho inspect.isclass, sau đó thêm kiểm thử hồi quy cho reproducer. Xác minh rằng mypy không báo lỗi khi không có giải pháp tạm thời bằng cast.

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
devtools
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.