33import itertools
44import threading
55import warnings
6- from typing import Optional , Tuple , TypeVar , Type , List , Union
6+ from typing import Optional , TypeVar , Type , List , Union , TYPE_CHECKING , Any , cast
77from sys import modules
88
99import 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
428435class 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