uv: uv wrongly detects PEP723 style magic comments in strings

Summary

code:

print("""
# /// script
# dependencies = ["pydantic", "email-validator"]
# ///
import pydantic

class Model(pydantic.BaseModel):
    email: pydantic.EmailStr

print(Model(email='hello@pydantic.dev'))
""")

Running this prints:

error: failed to create directory `/Users/samuel/Library/Caches/uv/environments-v2/check-da89a400ed637ca7`: Permission denied (os error 13)

Platform

MacOS 15 ARM

Version

uv 0.6.6 (c1a0bb85e 2025-03-12)

Python version

Python 3.12.3

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@samuelcolvin What’s your use case for running scripts that happen to embed magic comments in multiline literals?

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.

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.

I agree with the folks suggesting that nothing needs to change in uv here - the naive parsing in the spec is intentional, not accidental, and Python’s string quoting is flexible enough that it isn’t necessary to format strings in a way that triggers a misfire when generating code that happens to include PEP 723 comments.

I filed https://github.com/pypa/packaging.python.org/issues/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)

If PEP 751 lands maybe we can have a # /// lock block 😃