crates/ty_python_semantic/resources/mdtest/scripts.md
Scripts with PEP 723 metadata are considered single-file projects. For now, they can configure
rules and analysis, but we plan to also support dependencies and changing environment
settings.
[environment]
python-version = "3.12"
[rules]
unresolved-reference = "error"
[analysis]
respect-type-ignore-comments = false
A script can change its rules and analysis settings. In the future, it can also change its
environment settings. A script is standalone, it does not inherit any settings from the project
(that's not entirely true today, because scripts still inherit environment settings but it's our
end goal).
# /// 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)