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

poetry_hooks with switch tasks by named argument #193

Open
StripedMiha opened this issue Jan 11, 2024 · 3 comments
Open

poetry_hooks with switch tasks by named argument #193

StripedMiha opened this issue Jan 11, 2024 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@StripedMiha
Copy link

Hi! I was very interested in your tool and decided to implement it into the project.

In my project, I want to add a hook with a pre-commit install setup along with executing the poetry install --with dev command.
Here is my puproject.toml with dependencies divided into groups:

[tool.poetry.dependencies]
python = "^3.10"
...
poethepoet = "^0.24.4"

[tool.poetry.group.dev.dependencies]
...
pre-commit = "^3.2.0"


[tool.poetry.group.test.dependencies]
...

I have a working solution for this purpose, which is always executed when entering the poetry install command.

[tool.poe.poetry_hooks]
post_install = "pre_commit_install"

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
cmd = "pre-commit install"

However, I need the hook to be executed only when entering the poetry install command with the --with dev argument.

Here is my vision of pyproject.toml, for such functionality:

[tool.poe.poetry_hooks]
post_install = "pre_commit_install"

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
default = "pass"
control.expr = "with"
args = ["with"]

    [[tool.poe.tasks.pre_commit_install.switch]]
    case = "dev"
    cmd = "pre-commit install"

When I enter the command poetry install --with dev, I get the following error:

image

Please help me figure out what went wrong.

Thanks!

@nat-n nat-n added the enhancement New feature or request label Jan 15, 2024
@nat-n
Copy link
Owner

nat-n commented Jan 15, 2024

Hi @StripedMiha,

However, I need the hook to be executed only when entering the poetry install command with the --with dev argument.

I'm afraid limiting poetry hooks to invocations including specific arguments is not currently supported. Your workaround using a switch task is interesting, and might even work.

I believe the error you're getting from the expr task is because an expr task content is actually interpreted as subset of python, and as it happens with is a keyword in python, so the validation fails.

You should be able to avoid this particular error by mapping the argument to a different name with something like:

control.expr = "deps_group"
args = [{ name = "deps_group", options = ["--with"] }]

Please let me know if you get it working :)

@nat-n nat-n removed the enhancement New feature or request label Jan 15, 2024
@StripedMiha
Copy link
Author

@nat-n
Thanks for the advice.

But I'm afraid I don't quite understand how this should work, but this option does not catch the argument correctly and gives a default response.

[tool.poe.tasks.pre_commit_install]
help = "pre-commit pre install"
control.expr = "deps_group"
args = [{ name = "deps_group", options = ["--with"] }]

    [[tool.poe.tasks.pre_commit_install.switch]]
    case = "dev"
    cmd = "pre-commit install"

    [[tool.poe.tasks.pre_commit_install.switch]]
    cmd = "echo abc"

The response from such a command now does not fall with an error, but the switcher does not catch the argument
image

@nat-n
Copy link
Owner

nat-n commented Jan 18, 2024

Hi @StripedMiha ,

Thanks for getting back. In that case I think what you want is not possible today, because the poetry plugin only passes the task name to poe:

task_status = PoeCommand.get_poe(application, event.io)(

However in principle I suspect it could be supported because that linked function in the plugin could inspect the arguments from the present ConsoleCommandEvent and pass them to the poe task. It would also require a tweak to the logic around there to support templating in the poetry task's args if the user configures the hook with something like:
post_install = "_my_task --with=${POETRY_ARGS_WITH}".

Maybe this wouldn't too difficult to implement. I'll leave this issue open as an enhancement request.

@nat-n nat-n added enhancement New feature or request good first issue Good for newcomers labels Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants