contribute checking for "miscaptured" variables in loops

Đang mở
#177 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.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu bằng cách đọc script check-miscaptures.py được liên kết và đầu ra ví dụ của nó để hiểu các cảnh báo mà script tạo ra cho lambda, hàm có tên, vòng lặp và biểu thức hiểu. Sau đó kiểm tra pyflakes để tìm các điểm vào hiện có của việc kiểm tra mã nguồn; hoàn thành khi pyflakes báo cáo cùng những capture đáng ngờ đó, nhưng không báo cáo dạng đối số mặc định được minh họa.

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

Mô tả

Original report by daira (@daira?) on Launchpad:


Tahoe-LAFS, a large Python codebase implementing a distributed filesystem, uses a script called "check-miscaptures" ( https://tahoe-lafs.org/trac/tahoe-lafs/browser/trunk/misc/coding_tools/check-miscaptures.py) to catch a class of errors resulting in unexpected behaviour when capturing variables in loops.

An example of incorrect code that it would catch is:

  a = [None]*10
  for i in range(0, 10):
       a[i] = lambda x: x+i

  # The programmer expected a[x](y) to be equivalent to x+y


  print a[2](2)  # not 4, but 11

This happens because all of the a[i] capture the same mutable variable i, which is 9 after the loop when the lambda is called.

For this example, the output of check-miscaptures is:

Checking miscapture.py...
miscapture.py:3 captures 'i' assigned at line 2
1 suspiciously captured variables in 1 out of 1 file(s).

Changing the loop body to "a[i] = lambda x, i=i: x+i" fixes the problem, and check-miscaptures will not report a warning for the fixed code (it rarely gives false positives, and when it does they are easy to suppress).

The same problem can happen with named functions rather than lambdas, and with other kinds of loop or list comprehensions. More realistic examples are common in programs that define lots of callback functions, for example when using asynchronous libraries such as Twisted.

This ticket is to integrate the checking done by this script into pyflakes.

Ngôn ngữ chính
Python
Star
1.5k
Fork
207
Merge trung bình
4 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

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 PyCQA/pyflakes

Tất cả issue của PyCQA/pyflakes

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.