3
3
import itertools
4
4
import threading
5
5
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
7
7
from sys import modules
8
8
9
9
import numpy as np
@@ -180,7 +180,7 @@ def __new__(cls, name, bases, dct, **kargs):
180
180
181
181
# FIXME: is there a more elegant way to automatically add methods to the class that
182
182
# 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
184
184
"""Add ``__enter__`` and ``__exit__`` methods to the new class automatically."""
185
185
if context_class is not None :
186
186
cls ._context_class = context_class
@@ -193,7 +193,7 @@ def __enter__(self):
193
193
self ._old_theano_config = set_theano_conf (self ._theano_config )
194
194
return self
195
195
196
- def __exit__ (self , typ , value , traceback ):
196
+ def __exit__ (self , typ , value , traceback ): # pylint: disable=unused-variable
197
197
self .__class__ .context_class .get_contexts ().pop ()
198
198
# self._theano_config is set in Model.__new__
199
199
if hasattr (self , '_old_theano_config' ):
@@ -277,7 +277,7 @@ def __call__(cls, *args, **kwargs):
277
277
return instance
278
278
279
279
280
- def modelcontext (model : Optional ['Model' ]) -> Optional [ 'Model' ] :
280
+ def modelcontext (model : Optional ['Model' ]) -> 'Model' :
281
281
"""
282
282
Return the given model or, if none was supplied, try to find one in
283
283
the context stack.
@@ -418,11 +418,18 @@ def __setitem__(self, key, value):
418
418
' able to determine '
419
419
'appropriate logic for it' )
420
420
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' :
422
428
t0 = len (self )
423
429
list .__imul__ (self , other )
424
430
if self .parent is not None :
425
431
self .parent .extend (self [t0 :])
432
+ return self # python spec says should return the result.
426
433
427
434
428
435
class treedict (dict ):
@@ -715,6 +722,11 @@ def __init__(self, mean=0, sigma=1, name='', model=None):
715
722
CustomModel(mean=1, name='first')
716
723
CustomModel(mean=2, name='second')
717
724
"""
725
+
726
+ if TYPE_CHECKING :
727
+ def __enter__ (self : 'Model' ) -> 'Model' : ...
728
+ def __exit__ (self : 'Model' , * exc : Any ) -> bool : ...
729
+
718
730
def __new__ (cls , * args , ** kwargs ):
719
731
# resolves the parent instance
720
732
instance = super ().__new__ (cls )
@@ -764,7 +776,7 @@ def root(self):
764
776
def isroot (self ):
765
777
return self .parent is None
766
778
767
- @property
779
+ @property # type: ignore -- mypy can't handle decorated types.
768
780
@memoize (bound = True )
769
781
def bijection (self ):
770
782
vars = inputvars (self .vars )
0 commit comments