Support deprecated message and fields (#126)

This commit is contained in:
Arun Babu Neelicattu
2020-07-30 14:47:01 +02:00
committed by GitHub
parent beafc812ff
commit 0cd9510b54
7 changed files with 82 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ import re
from dataclasses import dataclass
from dataclasses import field
from typing import (
Iterator,
Union,
Type,
List,
@@ -249,6 +250,13 @@ class OutputTemplate:
"""
return [f.name for f in self.input_files]
@property
def python_module_imports(self) -> Set[str]:
imports = set()
if any(x for x in self.messages if any(x.deprecated_fields)):
imports.add("warnings")
return imports
@dataclass
class MessageCompiler(ProtoContentBase):
@@ -261,6 +269,7 @@ class MessageCompiler(ProtoContentBase):
fields: List[Union["FieldCompiler", "MessageCompiler"]] = field(
default_factory=list
)
deprecated: bool = field(default=False, init=False)
def __post_init__(self):
# Add message to output file
@@ -269,6 +278,7 @@ class MessageCompiler(ProtoContentBase):
self.output_file.enums.append(self)
else:
self.output_file.messages.append(self)
self.deprecated = self.proto_obj.options.deprecated
super().__post_init__()
@property
@@ -285,6 +295,12 @@ class MessageCompiler(ProtoContentBase):
return f"List[{self.py_name}]"
return self.py_name
@property
def deprecated_fields(self) -> Iterator[str]:
for f in self.fields:
if f.deprecated:
yield f.py_name
def is_map(
proto_field_obj: FieldDescriptorProto, parent_message: DescriptorProto

View File

@@ -1,6 +1,9 @@
# Generated by the protocol buffer compiler. DO NOT EDIT!
# sources: {{ ', '.join(description.input_filenames) }}
# plugin: python-betterproto
{% for i in description.python_module_imports|sort %}
import {{ i }}
{% endfor %}
from dataclasses import dataclass
{% if description.datetime_imports %}
from datetime import {% for i in description.datetime_imports|sort %}{{ i }}{% if not loop.last %}, {% endif %}{% endfor %}
@@ -50,6 +53,18 @@ class {{ message.py_name }}(betterproto.Message):
pass
{% endif %}
{% if message.deprecated or message.deprecated_fields %}
def __post_init__(self) -> None:
{% if message.deprecated %}
warnings.warn("{{ message.py_name }} is deprecated", DeprecationWarning)
{% endif %}
super().__post_init__()
{% for field in message.deprecated_fields %}
if self.{{ field }}:
warnings.warn("{{ message.py_name }}.{{ field }} is deprecated", DeprecationWarning)
{% endfor %}
{% endif %}
{% endfor %}
{% for service in description.services %}