Skip to content

SourceFile

A Python source file component that represents a Python file in the source directory. It provides a scope for the file, which is a PythonModuleScope that contains all the symbols defined in the file, such as functions, classes, and variables.

import { SourceFile } from "@alloy-js/python";
<SourceFile
doc={Children}
futureImports={Children[]}
header={Children}
headerComment="string"
insertFinalNewLine
path="string"
printWidth={number}
tabWidth={number}
useTabs
>
{children}
</SourceFile>
childrenoptional ChildrenContent to add to the file, such as function definitions, class definitions, and variable declarations.
docoptional ChildrenDocumentation for this module, which will be rendered as a module-level docstring.
futureImportsoptional Children[]future imports to render after the docstring but before regular imports.
headeroptional ChildrenContent to render at the very top of the file, before everything else. Use this for shebang lines, encoding declarations, or license headers.
headerCommentoptional stringComment to add at the top of the file, rendered as a Python comment block. This is a convenience prop for adding copyright notices or other comments.
insertFinalNewLineoptional booleanIf files should end with a final new line.
pathstringThe path to the file relative to the source directory.
printWidthoptional numberThe number of characters the printer will wrap on. Defaults to 100 characters.
tabWidthoptional numberThe number of spaces to use for indentation. Defaults to 2 spaces.
useTabsoptional booleanWhether to use tabs instead of spaces for indentation. Defaults to false.
<SourceFile path="test.py">
  <FunctionDeclaration name="test" />
</SourceFile>

renders to

def test():
  pass

With module documentation:

<SourceFile
  path="utils.py"
  doc={<ModuleDoc description={[<Prose>Utility functions for data processing.</Prose>]} />}
>
  <FunctionDeclaration name="process_data" />
</SourceFile>

renders to

"""
Utility functions for data processing.
"""

def process_data():
  pass