Skip to content

Commit 632305d

Browse files
authored
Remove dead code in get_context. (#4100)
Fix for issue #4098. Co-authored-by: Robert P. Goldman <[email protected]>
1 parent 6a5b758 commit 632305d

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

pymc3/model.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,15 @@ def get_context(cls, error_if_none=True) -> Optional[T]:
312312
"""Return the most recently pushed context object of type ``cls``
313313
on the stack, or ``None``. If ``error_if_none`` is True (default),
314314
raise a ``TypeError`` instead of returning ``None``."""
315-
idx = -1
316-
while True:
317-
try:
318-
candidate = cls.get_contexts()[idx] # type: Optional[T]
319-
except IndexError as e:
320-
# Calling code expects to get a TypeError if the entity
321-
# is unfound, and there's too much to fix.
322-
if error_if_none:
323-
raise TypeError("No %s on context stack" % str(cls))
324-
return None
325-
return candidate
326-
idx = idx - 1
315+
try:
316+
candidate = cls.get_contexts()[-1] # type: Optional[T]
317+
except IndexError as e:
318+
# Calling code expects to get a TypeError if the entity
319+
# is unfound, and there's too much to fix.
320+
if error_if_none:
321+
raise TypeError("No %s on context stack" % str(cls))
322+
return None
323+
return candidate
327324

328325
def get_contexts(cls) -> List[T]:
329326
"""Return a stack of context instances for the ``context_class``

0 commit comments

Comments
 (0)