-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
uv wrongly detects PEP723 style magic comments in strings #12303
Comments
Alas. Fwiw, this matches the regex in the specification https://regex101.com/r/LNoSNF/1 — though I'm sure this isn't the intent. I presume we can use a heuristic to avoid this? I don't want to bring in a parser.
cc @ofek |
Presumably the magic comment should appear at the start of the file, perhaps allowing for blank lines? If so, that should be pretty easy to detect. I agree the original regex here would match on this, and I'm pretty sure my code in |
I think it needs to be able to appear after a shebang, which could be fairly complicated (e.g., #12193 (comment) — which is not quite a perfect example but does capture the possible complexity) |
I need to look back at the comments, but I seem to recall that this came up when the PEP was being discussed… I’ll look when I’m back at my computer. |
The magic comment is allowed anywhere in the file because an anticipated use case is modification of the comment such as storing the contents of a lock file/dependency resolution. I don't think anything can be done here. In order to get your example working start immediately after the triple quotes i.e. don't have a leading new line character. |
This pre-emptively fixes the issue reported here: astral-sh/uv#12303
I very much dislike solutions like the one that was opened in response to this issue: pex-tool/pex#2722 There's nothing preventing such liberties but I think we're in a bad situation if/when providers start getting requests to support parsing Python syntax. @samuelcolvin What's your use case for running scripts that happen to embed magic comments in multiline literals? |
This seems like a "don't shoot yourself in the face" problem. I'd advocate that this is noted as an issue with the spec and if the spec changes to fix it then great. |
I don't have a specific use case, as mentioned above I discovered this while debugging our MCP server for writing python that uses PEP 723 style comments for dependencies, so I had some code in a string within a python script. I was reporting it to be a good citizen, not particularly because I need a fix. |
This pre-emptively fixes the issue reported here: astral-sh/uv#12303
Personally, I think we should probably avoid changing anything here. The spec was designed with the intent of supporting non-Python consumers, and deviating from the spec puts pressure on other tools to support those deviations. |
Accepting the magic comment in any location in the file matches the spec and is obvious to the user what happened after the fact, if sometimes surprising to the user before the fact. Therefore if a tool rejects a magic comment based on the Python syntax, or some other measure, I would prefer it do it non-silently, give a big warning that a magic comment was detected but rejected due to reasons. As I recall, there was a long discussion about not having to parse Python, and the construction of this comment was made such that it was unlikely to accidentally result in false positives, but obviously giving an example of the spec itself would be one of those cases impossible to avoid. |
Germane to the discussion: is there a plan to embed the lock file within scripts rather than generating a file beside the script? It would be nice to have portable reproducibility (without relying on that resolution date limit option). |
I think that's #11064 |
Worth noting that I see some hiccups with the specification for this purpose too #6318 (comment) |
If PEP 751 lands maybe we can have a |
I agree with the folks suggesting that nothing needs to change in I filed pypa/packaging.python.org#1835 to cover adding a note about this to the "Recommendations" section on the PEP 723 page (I'm open to bikeshedding on what we think the nicest "escaping" mechanism to recommend would be) |
Summary
code:
Running this prints:
Platform
MacOS 15 ARM
Version
uv 0.6.6 (c1a0bb8 2025-03-12)
Python version
Python 3.12.3
The text was updated successfully, but these errors were encountered: