Typer options unexpectedly being propagated to pytest #817
Replies: 2 comments
-
Here, I fixed it for you.
This works by tricking PyTest into thinking it got no arguments, so Typer can react to them. |
Beta Was this translation helpful? Give feedback.
-
I found a much better approach. Since import typer
import pytest
@app.command(context_settings={"allow_extra_args": True, "ignore_unknown_options": True})
def main(ctx: typer.Context, some_opt: str = typer.Option(default="default")):
# do stuff with some_opt
...
pytest.main(ctx.args)
if __name__ == "__main__":
typer.run(main) So now if I call python issue.py -s tests/test_things.py --some-opt X it will do the thing with the option, and then actually call pytest -s tests/test_things.py NB For typer arguments (as opposed to options), the ordering becomes important. But this isn't an issue for me. |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
Above code saved to
issue.py
If - and only if - the option is specified, it is somehow propagated to pytest. This works as expected (runs pytest):but this fails:
which is a pytest error message with
pytest
being replaced withissue.py
, which you can confirm by runningOperating System
Linux
Operating System Details
No response
Typer Version
0.12.3
Python Version
3.12.2
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions