crates/ty_python_semantic/resources/mdtest/scripts.md
Scripts with PEP 723 metadata are considered single-file projects. They can configure rules,
analysis, and their Python environment independently of the enclosing project. Dependencies are
resolved from existing Python environments; ty does not install them.
[environment]
python-version = "3.12"
[rules]
unresolved-reference = "error"
[analysis]
respect-type-ignore-comments = false
A script can change its rules, analysis, and environment settings. A script does not inherit
the enclosing project's configuration or Python environment, but can use an activated or explicitly
configured environment. First-party imports require explicitly configured source roots or extra
search paths.
# /// script
# [tool.ty.rules]
# all = "ignore"
# unresolved-reference = "error"
# [tool.ty.analysis]
# respect-type-ignore-comments = false
# ///
# error: [unresolved-reference]
print(missing) # type: ignore
tool.tyScripts with a valid metadata block are considered as their own project, even if the metadata block
does not contain any tool.ty section.
# /// script
# dependencies = []
# ///
value: int = "not an int" # error: [invalid-assignment]
suppressed: int = "not an int" # type: ignore
Script metadata is also recognized in stubs and extensionless Python files.
# /// script
# dependencies = []
# ///
value: Missing # error: [unresolved-reference]
script:
# /// script
# dependencies = []
# ///
# error: [unresolved-reference]
print(missing)
Invalid blocks do not establish script isolation, so the project configuration continues to apply.
if True:
# /// script
# [tool.ty.rules]
# unresolved-reference = "ignore"
# ///
pass
# error: [unresolved-reference]
print(missing)
value = 1 # /// script
# [tool.ty.rules]
# unresolved-reference = "ignore"
# ///
# error: [unresolved-reference]
print(missing)
# /// script
# [tool.ty.rules]
# unresolved-reference = "ignore"
# error: [unresolved-reference]
print(missing)
# /// script
# [tool.ty.rules
# unresolved-reference = "ignore"
# ///
# error: [unresolved-reference]
print(missing)
Invalid opening tags do not prevent a later valid metadata block from being recognized.
value = 1 # /// script
# [tool.ty.rules]
# unresolved-reference = "error"
# ///
# /// script invalid
# [tool.ty.rules]
# unresolved-reference = "error"
# ///
# /// script
# [tool.ty.rules]
# unresolved-reference = "ignore"
# ///
print(missing)
An earlier unclosed block does not prevent a later valid metadata block from being recognized.
# /// script
# [tool.ty.rules]
# unresolved-reference = "error"
value = 1
# /// script
# [tool.ty.rules]
# unresolved-reference = "ignore"
# ///
print(missing)