AttributeError on `with super()`
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Writing with super() raises an AttributeError. I ran into this implementing context managers inspired by contextlib.contextmnager but which had extra functionality. I can see no reason why with super() should be disallowed, and in fact, super().__enter__() and super().__exit__() work just fine.
Attempting to use an ExitStack as a work around was unsuccessful.
import sys
from contextlib import ExitStack
class Base:
def __enter__(self):
print("Entering base context manager")
def __exit__(self, type, value, traceback):
print("Exiting base context manager")
def __derived_enter__(self):
self.context = self.contextlib_style_context()
next(self.context)
return self.context
def __derived_exit__(self, type, value, traceback):
try:
next(self.context)
except StopIteration:
pass
class DerivedUsingEnterExit(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
super().__enter__()
try:
yield
finally:
super().__exit__(*sys.exc_info())
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
class DerivedUsingWith(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
with super():
yield
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
class DerivedUsingExitStack(Base):
def contextlib_style_context(self):
print("Entering contextlib style context manager")
with ExitStack() as stack:
stack.enter_context(super())
yield
print("Exiting contextlib style context manager")
__enter__ = __derived_enter__
__exit__ = __derived_exit__
# Runs successfully
with DerivedUsingEnterExit() as d:
pass
# Raises AttributeError
with DerivedUsingWith() as d:
pass
# Raises AttributeError
with DerivedUsingExitStack() as d:
pass
CPython versions tested on:
3.13, 3.12
Operating systems tested on:
Windows
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Trước tiên, hãy tái hiện các ví dụ DerivedUsingWith và DerivedUsingExitStack được cung cấp trên CPython 3.13 hoặc 3.12, sau đó lần theo các điểm vào của super() và trình quản lý ngữ cảnh có liên quan. So sánh chúng với trường hợp super().enter() và super().exit() hoạt động đúng; hoàn tất có nghĩa là hành vi dự kiến đã được quyết định và AttributeError được báo cáo đã được bao phủ bởi một bài kiểm thử hồi quy.
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
- 35/100