Skip to content

Commit 5619e69

Browse files
committed
Typing improvements.
1 parent d0250c8 commit 5619e69

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

pymc3/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Dict, List, Any
12
from copy import copy
23
import io
34
import os
@@ -232,7 +233,7 @@ class Minibatch(tt.TensorVariable):
232233
>>> assert x.eval().shape == (2, 20, 20, 40, 10)
233234
"""
234235

235-
RNG = collections.defaultdict(list)
236+
RNG = collections.defaultdict(list) # type: Dict[str, List[Any]]
236237

237238
@theano.configparser.change_flags(compute_test_value='raise')
238239
def __init__(self, data, batch_size=128, dtype=None, broadcastable=None, name='Minibatch',

pymc3/model.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import itertools
44
import threading
55
import warnings
6-
from typing import Optional, Tuple, TypeVar, Type, List, Union
6+
from typing import Optional, TypeVar, Type, List, Union, TYPE_CHECKING, Any, cast
77
from sys import modules
88

99
import numpy as np
@@ -180,7 +180,7 @@ def __new__(cls, name, bases, dct, **kargs):
180180

181181
# FIXME: is there a more elegant way to automatically add methods to the class that
182182
# are instance methods instead of class methods?
183-
def __init__(cls, name, bases, nmspc, context_class: Optional[Type]=None, **kwargs):
183+
def __init__(cls, name, bases, nmspc, context_class: Optional[Type]=None, **kwargs): # pylint: disable=unused-variable
184184
"""Add ``__enter__`` and ``__exit__`` methods to the new class automatically."""
185185
if context_class is not None:
186186
cls._context_class = context_class
@@ -193,7 +193,7 @@ def __enter__(self):
193193
self._old_theano_config = set_theano_conf(self._theano_config)
194194
return self
195195

196-
def __exit__(self, typ, value, traceback):
196+
def __exit__(self, typ, value, traceback): # pylint: disable=unused-variable
197197
self.__class__.context_class.get_contexts().pop()
198198
# self._theano_config is set in Model.__new__
199199
if hasattr(self, '_old_theano_config'):
@@ -277,7 +277,7 @@ def __call__(cls, *args, **kwargs):
277277
return instance
278278

279279

280-
def modelcontext(model: Optional['Model']) -> Optional['Model']:
280+
def modelcontext(model: Optional['Model']) -> 'Model':
281281
"""
282282
Return the given model or, if none was supplied, try to find one in
283283
the context stack.
@@ -418,11 +418,18 @@ def __setitem__(self, key, value):
418418
' able to determine '
419419
'appropriate logic for it')
420420

421-
def __imul__(self, other):
421+
# Added this because mypy didn't like having __imul__ without __mul__
422+
# This is my best guess about what this should do. I might be happier
423+
# to kill both of these if they are not used.
424+
def __mul__ (self, other) -> 'treelist':
425+
return cast('treelist', list.__mul__(self, other))
426+
427+
def __imul__(self, other) -> 'treelist':
422428
t0 = len(self)
423429
list.__imul__(self, other)
424430
if self.parent is not None:
425431
self.parent.extend(self[t0:])
432+
return self # python spec says should return the result.
426433

427434

428435
class treedict(dict):
@@ -715,6 +722,11 @@ def __init__(self, mean=0, sigma=1, name='', model=None):
715722
CustomModel(mean=1, name='first')
716723
CustomModel(mean=2, name='second')
717724
"""
725+
726+
if TYPE_CHECKING:
727+
def __enter__(self: 'Model') -> 'Model': ...
728+
def __exit__(self: 'Model', *exc: Any) -> bool: ...
729+
718730
def __new__(cls, *args, **kwargs):
719731
# resolves the parent instance
720732
instance = super().__new__(cls)
@@ -764,7 +776,7 @@ def root(self):
764776
def isroot(self):
765777
return self.parent is None
766778

767-
@property
779+
@property # type: ignore -- mypy can't handle decorated types.
768780
@memoize(bound=True)
769781
def bijection(self):
770782
vars = inputvars(self.vars)

0 commit comments

Comments
 (0)