Fix incorrect deprecation warnings on defaults (#348)

This change ensures that deprecation warnings are only raised when
either a deprecated field is explicitly set or a deprecated message is
initialised.

Resolves: #347
This commit is contained in:
Arun Babu Neelicattu
2022-03-12 00:36:14 +01:00
committed by GitHub
parent 9c1bf25304
commit 69f4192341
7 changed files with 65 additions and 39 deletions

View File

@@ -1,16 +1,15 @@
import dataclasses
import enum
import inspect
import json
import math
import struct
import sys
import typing
import warnings
from abc import ABC
from base64 import b64decode, b64encode
from copy import deepcopy
from datetime import datetime, timedelta, timezone
from dateutil.parser import isoparse
from typing import (
Any,
Callable,
@@ -26,12 +25,13 @@ from typing import (
get_type_hints,
)
from dateutil.parser import isoparse
from ._types import T
from ._version import __version__
from .casing import camel_case, safe_snake_case, snake_case
from .grpc.grpclib_client import ServiceStub
# Proto 3 data types
TYPE_ENUM = "enum"
TYPE_BOOL = "bool"
@@ -867,7 +867,10 @@ class Message(ABC):
return field_cls
def _get_field_default(self, field_name: str) -> Any:
return self._betterproto.default_gen[field_name]()
with warnings.catch_warnings():
# ignore warnings when initialising deprecated field defaults
warnings.filterwarnings("ignore", category=DeprecationWarning)
return self._betterproto.default_gen[field_name]()
@classmethod
def _get_field_default_gen(cls, field: dataclasses.Field) -> Any:
@@ -1288,6 +1291,22 @@ class Message(ABC):
"""
return self.from_dict(json.loads(value))
def is_set(self, name: str) -> bool:
"""
Check if field with the given name has been set.
Parameters
-----------
name: :class:`str`
The name of the field to check for.
Returns
--------
:class:`bool`
`True` if field has been set, otherwise `False`.
"""
return self.__raw_get(name) is not PLACEHOLDER
def serialized_on_wire(message: Message) -> bool:
"""