Skip to content

Commit

Permalink
chore: Restore code coverage (#855)
Browse files Browse the repository at this point in the history
Mostly get rid of older Py2-only code
  • Loading branch information
rwhogg authored Dec 15, 2022
1 parent bcf6143 commit 15fcea2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 85 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ show_missing = True
exclude_lines =
# Re-enable the standard pragma
pragma: NO COVER
pragma: NO PY${PY_VERSION} COVER
omit =
*/gapic/*.py
*/proto/*.py
Expand Down
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)
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
@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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

def _get_decoder(s):
a = array.array("B")
try:
a.frombytes(s)
except AttributeError: # pragma: NO PY3 COVER
a.fromstring(s)
a.frombytes(s)
d = pb_module.Decoder(a, 0, len(a))
return d

Expand Down

0 comments on commit 15fcea2

Please sign in to comment.