Skip to content
Open
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
8 changes: 3 additions & 5 deletions src/pptx/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,12 @@ def __get__(self, obj: Any, type: Any = None) -> _T:

# --- when accessed on instance, start by checking instance __dict__ for
# --- item with key matching the wrapped function's name
value = obj.__dict__.get(self._name)
if value is None:
if self._name not in obj.__dict__:
# --- on first access, the __dict__ item will be absent. Evaluate fget()
# --- and store that value in the (otherwise unused) host-object
# --- __dict__ value of same name ('fget' nominally)
value = self._fget(obj)
obj.__dict__[self._name] = value
return cast(_T, value)
obj.__dict__[self._name] = self._fget(obj)
return cast(_T, obj.__dict__[self._name])

def __set__(self, obj: Any, value: Any) -> None:
"""Raises unconditionally, to preserve read-only behavior.
Expand Down
36 changes: 35 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,41 @@

import pytest

from pptx.util import Centipoints, Cm, Emu, Inches, Length, Mm, Pt
from pptx.util import Centipoints, Cm, Emu, Inches, Length, Mm, Pt, lazyproperty


class DescribeLazyproperty(object):
def it_only_calls_the_decorated_method_once(self):
class Obj(object):
def __init__(self):
self.call_count = 0

@lazyproperty
def fget(self):
self.call_count += 1
return self.call_count

obj = Obj()

assert obj.fget == 1
assert obj.fget == 1
assert obj.call_count == 1

def it_caches_a_None_return_value_too(self):
class Obj(object):
def __init__(self):
self.call_count = 0

@lazyproperty
def fget(self):
self.call_count += 1
return None

obj = Obj()

assert obj.fget is None
assert obj.fget is None
assert obj.call_count == 1


class DescribeLength(object):
Expand Down