-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Auto dedent -c arguments #103997
Comments
You may be interested in something like https://github.com/hauntsaninja/pyp |
I am currently working to help this out. The clock is ticking. We need more reviewers. |
Thanks for the feature suggestion and tagging me on the PR! I think it's unlikely that this makes 3.12. Looking at the ideas thread: https://mail.python.org/archives/list/python-ideas@python.org/thread/6SYN2MQIP5DTF6INZ4SP2YKLF6P4VJOW/#6SYN2MQIP5DTF6INZ4SP2YKLF6P4VJOW I don't see any participation from core devs, so probably needs a little more consensus. My unorganised personal thoughts:
Also, a little off topic, but I've been appreciating your contributions sunmy2019! Thanks for helping make Python better! |
Let's target this to 3.13. This indeed might be worth going through the PEP process. https://peps.python.org/pep-0001/ We will need a core-dev sponsor then. |
Does this need a PEP? If so, should I start on a draft? If not, what if anything is on my plate to move this forward? @hauntsaninja pyp is a nifty tool, and something I'll likely add to my tool stack, but it would be nice to write readable Python scripts in instances where installing extra packages isn't an option. And unfortunately I found an issue with the |
It seems we cannot find a core dev willing to merge this.
We need to find a core dev as required by PEP 1. I think we should find one first, or we may end up with nothing. |
This doesn’t seem to be PEP-sized, but definitely discuss it on the forum. Compiler dedent was added recently in #106411, str.dedent is being discussed, this should all be discussed together. |
Sorry for chiming in late, but reading the comments about handling tabs vs. spaces, I realized there's another possible way to achieve this: add a new top-level grammar rule rather than preprocessing the string. We already have:
and could add:
I haven't tried this. I assume it won't be trivial, but the complexity is likely to be in adding this to |
I like the idea of a new grammar rule. It makes sense to me that a Python program should "just work" as long as indentation is consistent. In my mind there is no reason it has to start at level 0. However, that is a much bigger change and I'm sure it would require a PEP. I would be inclined to merge this first as it provides immediate benefit for an existing use case. While a grammar rule does seem more elegant to me, I'm not sure if the use-case is there. I don't see .py scripts benefiting from non-zero indentation. |
After a discussion with friends, we'd say the main usage would still be embedding codes inside other codes. just like def foo():
x = """
xxx
xxx
"""
exec(x) Also in other languages, such as https://pyo3.rs/v0.20.2/python_from_rust#want-to-run-statements-then-use-run |
Python ideas discussions happen on Discuss, not Github – please post there about the feature request, its rationale and use cases, examples in other languages, etc. |
Indeed. And for the discussion, you'll need to write a very PEP-like document anyway, so I recommend using the PEP structure. If you can't find the time to start the discussion, let me know. I like the feature :) |
Please let me know if you're writing something up. If not, I plan to take this back to discuss.python.org next week. |
I am not. |
Discussion here: https://discuss.python.org/t/44122 |
Feature or enhancement
The Python CLI should automatically dedent the argument given to "-c".
I raised this issue on the Python-Ideas mailing list and it got some positive feedback, so I'm moving forward with it here and in a proof-of-concept PR.
Pitch
I have what I think is a fairly low impact quality of life improvement to suggest for the python CLI.
When I'm not working in Python I tend to be working in bash. But often I want to break out and do something quick in Python. I find the
python -c
CLI very useful for this. For one liners it's perfect. E.g.NEW_VAR=$(python -c "import pathlib; print(pathlib.Path('$MYVAR').parent.parent)")
And even if I want to do something multi-line it's pretty easy
But the problem is when I'm writing bash inside a function or some other nested code, I would like to have nice indentation in my bash file, but if I write something like this:
I get
IndentationError: unexpected indent
.This means I have to write the function ugly like this:
Or use a helper function like this:
Or more recently I found that this is a low-impact workaround:
But as a certain Python dev may say: "There must be a better way."
Would there be any downside to the Python CLI automatically dedenting the input string given to -c? I can't think of any case off the top of my head where it would make a previously valid program invalid. Unless I'm missing something this would strictly make previously invalid strings valid.
Thoughts?
Previous discussion
On the mailing list there were these responses:
Lucas Wiman said:
There was also positive feedback from @cameron-simpson and @barry-scott suggested a PR with an implementation might move this forward. I have a proof-of-concept PR and this is the corresponding issue for it.
Linked PRs
The text was updated successfully, but these errors were encountered: