python / python/mypy

Plugin's get_type_analyze_hook is not used for Annotated type in type alias or generic specialization

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

Mypy doesn't use plugin's get_type_analyze_hook for Annotated type used in type alias or generic specialization. However, the hook is used for custom types (not Annotated) used in these contexts, and it's also called for Annotated used in other contexts.

To Reproduce

plugin.py

from typing import Callable

from mypy.plugin import AnalyzeTypeContext, Plugin
from mypy.types import NoneType, Type

def plugin(version: str):
    return MyPlugin

class MyPlugin(Plugin):
    def get_type_analyze_hook(self, fullname: str) -> Callable[[AnalyzeTypeContext], Type] | None:
        if fullname in {'typing.Annotated', 'typing_extensions.Annotated'}:
            return _type_analyze
        return None

def _type_analyze(ctx: AnalyzeTypeContext) -> Type:
    ctx.api.note('Analyzing type of Annotated[...]', ctx.context)
    return NoneType()  # for the sake of example

module.py

from typing import Annotated, Generic, TypeAlias, TypeVar, reveal_type

AnnotatedTA: TypeAlias = Annotated[dict, None]
typed_with_alias: AnnotatedTA
reveal_type(typed_with_alias)  # Revealed type is "builtins.dict[Any, Any]"

typed_directly: Annotated[dict, None]  # Analyzing type of Annotated[...]
reveal_type(typed_directly)  # Revealed type is "None", as expected


T = TypeVar('T')

class GenericType(Generic[T]):
    dummy: T

class Specialization(GenericType[Annotated[dict, None]]):
    ...

reveal_type(Specialization().dummy)  # Revealed type is "builtins.dict[Any, Any]"

Expected Behavior

All revealed types are None as requested by _type_analyze hook

Actual Behavior

Only directly typed variable typed_directly has the type requested by plugin

Your Environment

  • Mypy version used: 1.10.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): plugins = plugin.py
  • Python version used: 3.8, 3.12

Background

I'm writing a plugin which generates TypedDictType for Annotated[dict, ...] annotations based on some external configuration files. The arguments to Annotated are string literals, so I can't really use generics. In runtime these are just normal dicts.

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 plugin.py và module.py với các phiên bản Python và mypy đã được báo cáo, sau đó theo dõi quá trình phân tích kiểu cho Annotated khi được sử dụng trực tiếp, cho TypeAlias và cho việc chuyên biệt hóa generic. Hoàn tất có nghĩa là hook được gọi trong cả ba trường hợp và mỗi reveal_type báo cáo None như _type_analyze yêu cầu.

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ó
3/5
Thời gian dự kiến
1-2 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
48/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.