python / python/mypy

Inconsistent behavior for missing type variables in return slots

Đang mở
#20,649 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 behaves in an inconsistent fashion when revealing the type of an unconstrained type variable with no default when that variable appears in the return type of a function or method. When such type variables appear alone, mypy reports an error that return-slot type variables are not supported without a matching usage in the arguments or keyword arguments of the function or method being called. When combined using Union, however, missing type variables are silently coerced to Never without a warning or error being reported, leading to surprising applications of type narrowing.

To Reproduce

from typing import Any, Optional, cast, TypeVar, reveal_type

## Case 1: type variable in return position of function

def f[T]() -> T:
    raise Exception()

# mypy correctly refuses to type this, as it cannot infer T from the call site.
reveal_type(f())

## Case 2: type variable in return position of method

class C:
    def f[T](self) -> T:
        raise Exception()
    
# mypy correctly refuses to type this as well, and for the same reason.
reveal_type(C().f())

## Case 3: type variable in return position of method, combined with another
##         type

class D:
    def f[T](self) -> Optional[T]:
        raise Exception()
        
# mypy does not return an error or warning in this case, but substitutes
# `Never` for `T`, resulting in `Never | None` being narrowed to `None`.
reveal_type(D().f())

## Case 4: two type variables in return position of method, combined in a
##         type form


class E:
    def f[T, U](self) -> T | U:
        raise Exception()

# mypy also does not return an error or warning in this case, but substiutes
# `Never` for both `T` and `U`, resulting in `Never | Never` being narrowed
# to `Never`.
reveal_type(E().f())

## Case 5: type variable with explicit default=Any in return position of
##         function

U = TypeVar("U", default=Any)
def g[U]() -> U:
    raise Exception()

# Despite `U` having an explicit default, the definition of `g` returns an
# error, and `g()` is narrowed to `Never` instead of `Any`.
reveal_type(g)
reveal_type(g())

https://mypy-play.net/?mypy=latest&python=3.14&gist=044e1c85118dd1c92e110ee52c45d6e7

Expected Behavior

Either for mypy to return errors for D, E, and gin the same way asfandC`, or for substitution of type variable defaults to be applied uniformly when type variables cannot be inferred from call sites.

Actual Behavior

See above playground link.

Your Environment

mypy 1.19.1 on Python 3.14. See above playground link.

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 năm trường hợp tái hiện trong issue hoặc ví dụ mypy-playground được liên kết, tập trung vào suy luận biến kiểu của ô trả về và việc thay thế giá trị mặc định. Công việc hoàn tất khi các trường hợp D, E và g có hành vi nhất quán với f và C, cùng với độ bao phủ hồi quy cho các kết quả reveal_type đã được báo cáo.

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
Khá rõ ràng
Mức phù hợp với người mới
42/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.