Avoid calling __init__ for classmethod/staticmethod
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
- 45/100
Hướng nghiên cứu
Bắt đầu với điểm vào Fire được minh họa trong ví dụ, fire.Fire(SomethingLikeGit), và theo dõi cách init, __init__, các classmethod và staticmethod được dispatch. Tái hiện python something_like_git.py init khi chưa có thư mục .something_like_git; hoàn thành khi init có thể chạy mà không gọi __init__, trong khi các lệnh instance thông thường vẫn giữ nguyên hành vi khởi tạo hiện tại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Suppose we want a CLI that is similar to git in the sense that we must first call init before interacting with the repo. For this, we aim for the following (simplified/partially implemented) class, which is intentionally similar to GitPythons Repo class (see here):
from pathlib import Path
import fire
class SomethingLikeGit:
dirname = ".something_like_git"
@classmethod
def init(cls, path=None):
path = Path(path or Path.cwd())
path = path / cls.dirname
print("Initializing at", path)
path.mkdir()
...
print("Done.")
def __init__(self, path=None):
path = Path(path or Path.cwd())
for parent in (path, *path.parents):
self.path = parent / self.dirname
if self.path.exists():
break
else:
raise FileNotFoundError(path)
...
if __name__ == "__main__":
fire.Fire(SomethingLikeGit)
When we run the command python something_like_git.py init, it first tries to instantiate the class via __init__, which fails (FileNotFoundError) since it must first create the directory via init.
I think it makes generally more sense if Fire would call classmethods/staticmethods directly on the class without trying to instantiate it. This is usually also how class/static methods are used in a programmatic way (e.g. for git.Repo).
Note that the fix for #113 does not resolve this issue. This is because the path argument for __init__ is (intentionally) optional, so Fire thinks it can call __init__ first. If path wasn't optional, it would behave as wanted: init would be called without first calling __init__.
Anyway, thanks for your great work!
- Ngôn ngữ chính
- Python
- Star
- 28.2k
- Fork
- 1.5k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
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.
Issue khác của google/python-fire
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
google/python-fire#693 ·
-
Release 0.7.2? Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
google/python-fire#698 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
google/python-fire#672 · 5 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
google/python-fire#665 · 2 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
google/python-fire#659 · 1 bình luận ·
Tất cả issue của google/python-fire
Issue tương tự
-
triage/confirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
apache/cloudstack#14222 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100