Generating error if callable object is formatted as a string?

Đang mở
#5,213 6 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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
devtools

Hướng nghiên cứu

Start by reviewing the three examples and the four proposed options in the issue. The issue names no files, tests, or entry points; completion would require choosing and specifying a behavior for callable objects passed to formatting or str(), including how valid cases are silenced.

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

Mô tả

needs discussion

Pretty frequently I have errors where I forget to call a method and accidentally pass a method object to % or str.format. Mypy doesn't complain since it's possible to convert a callable object to a string, though usually that's not what I want.

Example:

class Item:
    def name(self) -> str: ...

def f(x: Item) -> None:
    print('name: {}'.format(x.name))  # No error, though should be x.name() 
    print('name: %s' % x.name)  # Similar to above
    foo(str(x.name))  # Ditto

This occasionally happens during refactoring as well, when I replace an instance variable with a method.

Here are a few things we could do about this:

  1. Special case these errors and generate a warning even though the code is not necessarily wrong. Maybe require an explicit repr(x.some_method) to silence the error (# type: ignore or a cast to object would also work).
  2. Introduce a new strictness flag that causes these to be flagged. Not sure if this should be enabled by default.
  3. Introduce a new strictness flag that catches these and other cases where there is no runtime type error, but that we suspect might be errors. Passing a string argument when an iterable object is expected comes to mind as a kind of similar issue, and there are probably others.
  4. Do nothing, since there is no runtime type error and the code could well be valid.

Option 2 seems too specialized -- this doesn't seem important enough to add a strictness flag. Personally I prefer the more opinionated option 1. We could add a hint about how to silence the error in case the code happens to be correct.

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.