how to use typer on class method __init__ with self argument, got this error: Error: Missing argument 'SELF'.
fbenavides69 opened this issue · 2 comments
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the Typer documentation, with the integrated search.
- I already searched in Google "How to X in Typer" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to Typer but to Click.
Commit to Help
- I commit to help with one of those options 👆
Example Code
import typer
app = typer.Typer()
class some:
@app.command()
def __init__(self, start_date: Optional[str] = None, stop_date: Optional[str] = None) -> None:
self.start = start_date
self.stop = stop_date
def run():
print(f"{self.start} - {self.stop}")
if __name__ == "__main__":
dc = app.run(some())
dc.run()
Description
Typer deals with the "self" argument in the class constructor "init" and does not know how to deal with that, please help!
Operating System
Windows
Operating System Details
No response
Typer Version
0.7.0
Python Version
Python 3.8.5
Additional Context
No response
I'm not sure using typer like that on an init method is actually supported. I think having your CLI be a simple method is really to be preferred. Try putting the CLI into its own method, and then call whatever program logic you have from there, like this for example:
import typer
from typing import Optional
from datetime import datetime
class Some:
# your class impl
pass
def main(start_date: Optional[datetime] = None, stop_date: Optional[datetime] = None):
Some(start_date, stop_date).run()
if __name__ == "__main__":
typer.run(main)
You can totally use a constructor as a typer command. However, it's pretty confusing! I would recommend @jonaslb's approach above instead. But if you really want to... the trick is to decorate the class instead of the initializer:
from typing import Optional
import typer
app = typer.Typer()
@app.command()
class Some:
def __init__(self, start_date: Optional[str] = None, stop_date: Optional[str] = None) -> None:
self.start = start_date
self.stop = stop_date
self.run()
def run(self):
print(f"{self.start} - {self.stop}")
if __name__ == "__main__":
app()
Then
$ /usr/bin/python3 main.py --start-date 2023-01-01 --stop-date 2023-12-31
2023-01-01 - 2023-12-31
However, your code as written has a few other problems. The biggest one to me is trying to use the return value of the typer entry point. You shouldn't run any code after calling app()
. Make sure all the code you want to execute is inside your command.