Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use typer on class method __init__ with self argument, got this error: Error: Missing argument 'SELF'. #539

Open
7 tasks done
fbenavides69 opened this issue Jan 25, 2023 · 2 comments
Labels
question Question or problem

Comments

@fbenavides69
Copy link

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

@fbenavides69 fbenavides69 added the question Question or problem label Jan 25, 2023
@jonaslb
Copy link
Contributor

jonaslb commented Feb 6, 2023

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)

@kuykendall-ben
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

3 participants