protobuf.plugin
A protoc plugin framework for Python.
File
Bases: Protocol
A Python file that will have content generated based on the operations on this object.
Add a line to the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
object
|
Elements to print on the line. Each argument is converted to a string element:
|
()
|
ident
scope
Open an indented scope.
Prints args as the scope header and indents all subsequent
print() calls inside the context manager by one level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
object
|
Elements to print as the scope header line. |
()
|
Examples:
type_checking
Open an if TYPE_CHECKING: scope.
Emits if TYPE_CHECKING: as a scope header and marks all
identifiers printed inside the context manager as type-only
imports.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If already inside a type-checking context. |
Examples:
preamble
Add a DO NOT EDIT preamble derived from desc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desc
|
DescFile
|
The file descriptor whose proto name is used in the preamble. |
required |
doc
Open a Python docstring.
Prints opening """ (with args on the same line if
provided) and closing """ on exit. If args are
provided and nothing is printed inside the context manager, the
docstring collapses to a single line. While the context is
open, print() escapes \ and """ in string
elements so they cannot break the docstring. scope() works
normally inside for indented sections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
object
|
Optional elements to print on the opening line after the triple-quote. |
()
|
Examples:
Ident
dataclass
A Python identifier with its owning module.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The symbol name. |
module |
Module
|
The module this identifier is imported from. |
type_only |
bool
|
If true, the import is only emitted inside an
|
for_desc
classmethod
Derive an importable identifier from a descriptor.
For DescFile, the returned name is the module for the file
within its Module, (e.g. baz_pb in .foo.bar). For message, enum, and
extension descriptors the name is the generated symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desc
|
DescEnum | DescMessage | DescExtension | DescFile
|
A file, message, enum, or extension descriptor. |
required |
type_only
|
bool
|
If true, the import is only emitted inside an
|
False
|
escape_module_with_hash
|
bool
|
If true, the module name is escaped with a hash suffix to avoid potential collisions with other artifacts. |
False
|
Returns:
| Type | Description |
|---|---|
Ident
|
An |
Module
dataclass
A Python module path used for import resolution.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
The import path of the module. If it starts with a |
for_desc
classmethod
Derive a relative module path from a file descriptor.
Intermediate path components are sanitized (Python keywords and
the _pb suffix are escaped). The final component gets
suffix appended.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desc
|
DescFile
|
The file descriptor. |
required |
suffix
|
str
|
Appended to the final path component, e.g.
|
required |
escape_with_hash
|
bool
|
If true, the module name is escaped with a hash suffix to avoid potential collisions with other artifacts. |
False
|
Returns:
| Type | Description |
|---|---|
Module
|
A relative |
Examples:
google/protobuf/any.proto with suffix "_pb" produces
.google.protobuf.any_pb.
ident
Schema
Bases: Protocol[T_co]
Schema describes the files and types that the plugin is requested to generate.
generate_file
Create a generated file.
Two calling conventions are supported:
generate_file(path)
Create a file at an arbitrary output path relative to the
generation root.
generate_file(desc, suffix)
Create a file with a path derived from the descriptor:
<proto path without .proto><suffix>.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_desc
|
str | DescFile
|
Either a string path (e.g.
|
''
|
suffix
|
str
|
When |
''
|
Returns:
| Type | Description |
|---|---|
File
|
A new |
escape_identifier
escape_public_identifier
Escapes an identifier such as a class or attribute name.
We escape for two points:
- Reserved words which can cause conflicts with other code. We suffix with
_. Identifiers that are already a reserved word with_suffixes also get an extra_to ensure the mapping is injective. - Starting with
_which can cause visibility issues, especially if it results in name mangling from double underscores. We prefix withpb_. Identifiers that already start withpb_are also prefixed withpb_to ensure the mapping is injective.
python_keywords
Return the default set of Python keywords.
These are the hard keywords that must be escaped to generate usable Python code. This list is a the collection of all keywords across Python versions supported by protobuf-py.
run
run(
name: str,
version: str,
generate: Callable[[Schema[None]], None],
/,
*,
minimum_edition: int = minimum_supported_edition,
maximum_edition: int = maximum_supported_edition,
args: list[str] | None = None,
stdin: IO[bytes] | None = None,
stdout: IO[bytes] | None = None,
) -> None
run(
name: str,
version: str,
options: type[D],
generate: Callable[[Schema[D]], None],
/,
*,
minimum_edition: int = minimum_supported_edition,
maximum_edition: int = maximum_supported_edition,
args: list[str] | None = None,
stdin: IO[bytes] | None = None,
stdout: IO[bytes] | None = None,
) -> None
run(
name: str,
version: str,
options: Callable[[str], T],
generate: Callable[[Schema[T]], None],
/,
*,
minimum_edition: int = minimum_supported_edition,
maximum_edition: int = maximum_supported_edition,
args: list[str] | None = None,
stdin: IO[bytes] | None = None,
stdout: IO[bytes] | None = None,
) -> None
Run a protoc plugin.
This is the single entry point for any protoc plugin. It handles the full lifecycle: reading the request, parsing options, building descriptors, calling the generate callback, and writing the response.
Two calling conventions are supported:
run(name, version, generate)
run(name, version, options, generate)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Plugin name, used for |
required |
version
|
str
|
Plugin version, used for |
required |
options_or_generate
|
type[DataclassInstance] | Callable[..., Any]
|
If a dataclass type, parse key=value pairs into it.
If a callable and |
required |
generate
|
Callable[[Schema[Any]], None] | None
|
Callback invoked with the schema. |
None
|
minimum_edition
|
int
|
Minimum edition supported by this plugin. Defaults to the runtime's minimum_supported_edition. |
minimum_supported_edition
|
maximum_edition
|
int
|
Maximum edition supported by this plugin. Defaults to the runtime's maximum_supported_edition. |
maximum_supported_edition
|
args
|
list[str] | None
|
The command line arguments. Defaults to sys.argv. |
None
|
stdin
|
IO[bytes] | None
|
The input stream to read the request from. Defaults to sys.stdin.buffer. |
None
|
stdout
|
IO[bytes] | None
|
The output stream to write the response to. Defaults to sys.stdout.buffer. |
None
|
get_package_comments
Get comments on the package element in the protobuf source.