Avoid calling __init__ for classmethod/staticmethod
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Inizia dal punto di ingresso di Fire mostrato nell’esempio, fire.Fire(SomethingLikeGit), e segui come vengono sottoposti a dispatch init, __init__, i metodi di classe e i metodi statici. Riproduci python something_like_git.py init senza una directory .something_like_git esistente; il lavoro è completato quando init può essere eseguito senza invocare __init__, mentre i normali comandi di istanza mantengono il comportamento attuale di inizializzazione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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!
- Lingua principale
- Python
- Stelle
- 28.2k
- Fork
- 1.5k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di google/python-fire
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
google/python-fire#693 ·
-
Release 0.7.2? Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 38/100
google/python-fire#698 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
google/python-fire#672 · 5 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
google/python-fire#665 · 2 commenti ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
google/python-fire#659 · 1 commento ·
Tutte le issue di google/python-fire
Issue simili
-
Add: hunch Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
DiamondLightSource/dodal#2211 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
openml/openml-python#1749 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
sipyourdrink-ltd/bernstein#6191 ·