Skip to content

Commit 3b76f60

Browse files
martindemellorchen152
authored andcommitted
BEGIN_PUBLIC
Add some metadata fields to the serialized ast. Added fields: src_path: Full path to the original source file. metadata: List of arbitrary string-encoded metadata END_PUBLIC The new fields are mainly for kythe, so that cross references to generated code link to the correct place. Also includes a pytype release, so that kythe stays in sync. PiperOrigin-RevId: 457845537
1 parent f4279fe commit 3b76f60

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pytype/pytd/serialize_ast.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class SerializableTupleClass(NamedTuple):
6363
dependencies: List[Tuple[str, Set[str]]]
6464
late_dependencies: List[Tuple[str, Set[str]]]
6565
class_type_nodes: Optional[List[pytd.ClassType]]
66-
is_package: bool
66+
is_package: bool # TODO(rechen): remove this, can be computed from src_path
67+
src_path: Optional[str]
68+
metadata: List[str]
6769

6870

6971
class SerializableAst(SerializableTupleClass):
@@ -83,11 +85,15 @@ class SerializableAst(SerializableTupleClass):
8385
visited and have their .cls set. If this attribute is None the whole AST
8486
will be visited and all found ClassType instances will have their .cls
8587
set.
88+
is_package: True if the original source file was a package __init__.
89+
src_path: Optionally, the filepath of the original source file.
90+
metadata: A list of arbitrary string-encoded metadata.
8691
"""
8792
Replace = SerializableTupleClass._replace # pylint: disable=no-member,invalid-name
8893

8994

90-
def StoreAst(ast, filename=None, open_function=open, is_package=False):
95+
def StoreAst(ast, filename=None, open_function=open, is_package=False,
96+
src_path=None, metadata=None):
9197
"""Loads and stores an ast to disk.
9298
9399
Args:
@@ -96,6 +102,8 @@ def StoreAst(ast, filename=None, open_function=open, is_package=False):
96102
function instead returns the pickled string.
97103
open_function: A custom file opening function.
98104
is_package: Whether the module with the given ast is a package.
105+
src_path: Optionally, the filepath of the original source file.
106+
metadata: A list of arbitrary string-encoded metadata.
99107
100108
Returns:
101109
The pickled string, if no filename was given. (None otherwise.)
@@ -116,10 +124,15 @@ def StoreAst(ast, filename=None, open_function=open, is_package=False):
116124
indexer = FindClassTypesVisitor()
117125
ast.Visit(indexer)
118126
ast = ast.Visit(visitors.CanonicalOrderingVisitor())
127+
128+
metadata = metadata or []
129+
119130
return pytd_utils.SavePickle(
120131
SerializableAst(
121132
ast, sorted(dependencies.items()), sorted(late_dependencies.items()),
122-
sorted(indexer.class_type_nodes), is_package=is_package),
133+
sorted(indexer.class_type_nodes), is_package=is_package,
134+
src_path=src_path, metadata=metadata,
135+
),
123136
filename, open_function=open_function)
124137

125138

0 commit comments

Comments
 (0)