python / python/mypy

request: Statically evaluating module-level hasattr checks

Đang mở
#9,042 3 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

feature priority-2-low
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ả

This is a feature request, for mypy to enhance its static evaluation for boolean expressions involving sys.platform/sys.version_info to also handle expressions like hasattr(module_object, "constant string").

background

A common idiom in the standard library is to have functions/constants/etc. that are only exposed on certain platforms. For example, select.epoll only exists on Linux and illumos, socket.fromshare only exists on Windows, and os.preadv only exists on "Linux 2.6.30 and newer, FreeBSD 6.0 and newer, OpenBSD 2.7 and newer".

Currently, the way mypy handles these cases is:

  • typeshed contains if sys.platform == ... checks that try to approximate the runtime availability. Example
  • then mypy statically evaluates these checks, so it knows which functions/constants/etc. are available

So that works about as well as anything could. The static approximations aren't always exactly correct (e.g. the epoll example linked above gives the wrong answer on illumos), but they're "good enough" and can be improved over time if they cause problems.

the problem

Say we have a library which wants to use or expose certain features in its API depending on whether or not the standard library exposes those features. For example, in Trio we define our IOManager class differently depending on whether select.epoll and select.kqueue exist, and we export a trio.socket.fromshare function iff there's a socket.fromshare.

Now we're trying to figure out how to add type annotations, and it's super awkward. So far our best attempt for the basic platform differences looks like:

if sys.platform == "win32":
    ...
elif sys.platform == "linux" or (not TYPE_CHECKING and hasattr(select, "epoll")):
    ...
# At type-checking time, assume that all platforms that aren't win32 or linux are some kind of BSD
elif TYPE_CHECKING or (not TYPE_CHECKING and hasattr(select, "kqueue")):
    ...
else:
    raise NotImplementedError

This has a few problems:

  • it's complex and awkward
  • it requires manually duplicating information that's already in typeshed
  • it requires manually propagating any typeshed changes into our project (which seems plausible, since typeshed often contains rough approximations for platform-specific stuff)
  • it requires copy/pasting this whole complex construct in multiple places around our code-base

This is a bit frustrating. It would be nice if mypy could just understand if hasattr(select, "epoll") and do the right thing. It already has all the information it needs to do that.

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 xác định phần đánh giá tĩnh hiện có của mypy cho sys.platform và sys.version_info, cùng với các bài kiểm thử của nó. Theo dõi cách các thuộc tính module của typeshed được biểu diễn và đánh giá cho các phép kiểm tra hasattr bằng chuỗi hằng. Công việc được xem là hoàn tất khi các biểu thức hasattr ở cấp module có thể được đánh giá nhất quán dựa trên thông tin typeshed hiện có, với các bài kiểm thử bao quát các thuộc tính phụ thuộc nền tảng.

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, devtools
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
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
35/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.