chore: Restore code coverage by bw-yuhogg · Pull Request #855 · googleapis/python-ndb · GitHub
Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .coveragerc
6 changes: 1 addition & 5 deletions google/cloud/ndb/_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import uuid
import time

# Python 2.7 module name change
try:
import queue
except ImportError: # pragma: NO PY3 COVER
import Queue as queue
import queue

from google.cloud.ndb import utils

Expand Down
16 changes: 3 additions & 13 deletions google/cloud/ndb/_legacy_protocol_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@


# Python 3 doesn't have "long" anymore
try:
long(42)
except NameError: # pragma: NO PY2 COVER
long = int
long = int


class ProtocolBufferDecodeError(Exception):
Expand All @@ -31,10 +28,7 @@ class ProtocolBufferDecodeError(Exception):
class ProtocolMessage:
def MergePartialFromString(self, s):
a = array.array("B")
try:
a.frombytes(s)
except AttributeError: # pragma: NO PY3 COVER
a.fromstring(s)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromstring is a deprecated alias for frombytes. Ditto for tostring vs. tobytes.

The string versions date from Py2 and were renamed and aliased in Py3 due to the bytes/string distinction. The aliases were removed in Python 3.9.

a.frombytes(s)
d = Decoder(a, 0, len(a))
self.TryMerge(d)

Expand Down Expand Up @@ -204,11 +198,7 @@ def getPrefixedString(self):
raise ProtocolBufferDecodeError("truncated")
r = self.buf[self.idx : self.idx + length] # noqa: E203
self.idx += length
try:
prefixed = r.tobytes()
except AttributeError: # pragma: NO PY3 COVER
prefixed = r.tostring()
return prefixed
return r.tobytes()


__all__ = [
Expand Down
52 changes: 20 additions & 32 deletions google/cloud/ndb/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import collections
import contextlib
import contextvars
import itertools
import os
import six
Expand Down Expand Up @@ -60,43 +61,30 @@ def __next__(self):
_context_ids = _ContextIds()


try: # pragma: NO PY2 COVER
import contextvars
class _LocalState:
"""Thread local state."""

class _LocalState:
"""Thread local state."""

def __init__(self):
self._toplevel_context = contextvars.ContextVar(
"_toplevel_context", default=None
)
self._context = contextvars.ContextVar("_context", default=None)

@property
def context(self):
return self._context.get()

@context.setter
def context(self, value):
self._context.set(value)

@property
def toplevel_context(self):
return self._toplevel_context.get()

@toplevel_context.setter
def toplevel_context(self, value):
self._toplevel_context.set(value)
def __init__(self):
self._toplevel_context = contextvars.ContextVar(
"_toplevel_context", default=None
)
self._context = contextvars.ContextVar("_context", default=None)

@property
def context(self):
return self._context.get()

except ImportError: # pragma: NO PY3 COVER

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: it's easier to see that this change is correct if you view it with split-diffs and while ignoring whitespace changes

@context.setter
def context(self, value):
self._context.set(value)

class _LocalState(threading.local):
"""Thread local state."""
@property
def toplevel_context(self):
return self._toplevel_context.get()

def __init__(self):
self.context = None
self.toplevel_context = None
@toplevel_context.setter
def toplevel_context(self, value):
self._toplevel_context.set(value)


_state = _LocalState()
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/ndb/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def __getnewargs__(self):
state to pickle. The dictionary has three keys ``pairs``, ``app``
and ``namespace``.
"""
return ( # pragma: NO PY2 COVER
return (
{
"pairs": self.pairs(),
"app": self.app(),
Expand Down
16 changes: 6 additions & 10 deletions google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,7 @@ class Person(Model):
GeoPt = helpers.GeoPoint
Rollback = exceptions.Rollback


try:
_getfullargspec = inspect.getfullargspec
except AttributeError: # pragma: NO PY3 COVER
_getfullargspec = inspect.getargspec
_getfullargspec = inspect.getfullargspec


class KindError(exceptions.BadValueError):
Expand Down Expand Up @@ -3064,9 +3060,9 @@ def _from_base_type(self, value):
Returns:
Any: The unpickled ``value``.
"""
if six.PY3 and type(value) is bytes: # pragma: NO BRANCH
return pickle.loads(value, encoding="bytes") # pragma: NO PY2 COVER
return pickle.loads(value) # pragma: NO PY3 COVER
if type(value) is bytes: # pragma: NO BRANCH
return pickle.loads(value, encoding="bytes")
return pickle.loads(value) # pragma: NO COVER


class JsonProperty(BlobProperty):
Expand Down Expand Up @@ -3313,7 +3309,7 @@ def __eq__(self, other):
return self._email == other._email and self._auth_domain == other._auth_domain

def __lt__(self, other):
if not isinstance(other, User): # pragma: NO PY2 COVER
if not isinstance(other, User):
return NotImplemented

return (self._email, self._auth_domain) < (
Expand Down Expand Up @@ -4907,7 +4903,7 @@ def __init__(_self, **kwargs):

def _get_property_for(self, p, indexed=True, depth=0):
"""Internal helper to get the Property for a protobuf-level property."""
if isinstance(p.name(), six.text_type): # pragma: NO PY2 COVER
if isinstance(p.name(), six.text_type):
p.set_name(bytes(p.name(), encoding="utf-8"))
parts = p.name().decode().split(".")
if len(parts) <= depth:
Expand Down
15 changes: 2 additions & 13 deletions google/cloud/ndb/tasklets.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,7 @@ def get_traceback(self):
Union[types.TracebackType, None]: The traceback, or None.
"""
if self._exception:
try:
traceback = self._exception.__traceback__
except AttributeError: # pragma: NO PY3 COVER # pragma: NO BRANCH
# Python 2 does not have the helpful traceback attribute, and
# since the exception is not being handled, it appears that
# sys.exec_info can't give us the traceback either.
traceback = None
return traceback
return self._exception.__traceback__

def add_done_callback(self, callback):
"""Add a callback function to be run upon task completion. Will run
Expand Down Expand Up @@ -322,11 +315,7 @@ def _advance_tasklet(self, send_value=None, error=None):
with self.context.use():
# Send the next value or exception into the generator
if error:
try:
traceback = error.__traceback__
except AttributeError: # pragma: NO PY3 COVER # pragma: NO BRANCH # noqa: E501
traceback = None

traceback = error.__traceback__
yielded = self.generator.throw(type(error), error, traceback)

else:
Expand Down
5 changes: 1 addition & 4 deletions google/cloud/ndb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import os
import threading

try:
_getfullargspec = inspect.getfullargspec
except AttributeError: # pragma: NO PY3 COVER
_getfullargspec = inspect.getargspec
_getfullargspec = inspect.getfullargspec

TRUTHY_STRINGS = {"t", "true", "y", "yes", "on", "1"}

Expand Down
3 changes: 1 addition & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def cover(session):
# Install all dependencies.
session.install("coverage")
# Run coverage report.
# TODO return to 100% coverage
session.run("coverage", "report", "--fail-under=99", "--show-missing")
session.run("coverage", "report", "--fail-under=100", "--show-missing")
# Erase cached coverage data.
session.run("coverage", "erase")

Expand Down
5 changes: 1 addition & 4 deletions tests/unit/test__legacy_entity_pb.py