<s> from abc import abstractmethod , ABCMeta <EOL> from unittest import TestCase <EOL> from six import with_metaclass <EOL> from zipline . utils . final import ( <EOL> FinalMeta , <EOL> final , <EOL> ) <EOL> from zipline . utils . metautils import compose_types <EOL> class FinalMetaTestCase ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> class ClassWithFinal ( with_metaclass ( FinalMeta , object ) ) : <EOL> a = final ( '<STR_LIT>' ) <EOL> b = '<STR_LIT>' <EOL> @ final <EOL> def f ( self ) : <EOL> return '<STR_LIT>' <EOL> def g ( self ) : <EOL> return '<STR_LIT>' <EOL> cls . class_ = ClassWithFinal <EOL> def test_subclass_no_override ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> pass <EOL> def test_subclass_no_final_override ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> b = '<STR_LIT>' <EOL> def g ( self ) : <EOL> return '<STR_LIT>' <EOL> def test_override_final_no_decorator ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class SubClass ( self . class_ ) : <EOL> def f ( self ) : <EOL> return '<STR_LIT>' <EOL> def test_override_final_attribute ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class SubClass ( self . class_ ) : <EOL> a = '<STR_LIT>' <EOL> def test_override_final_with_decorator ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class SubClass ( self . class_ ) : <EOL> @ final <EOL> def f ( self ) : <EOL> return '<STR_LIT>' <EOL> def test_override_final_attribute_with_final ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class SubClass ( self . class_ ) : <EOL> a = final ( '<STR_LIT>' ) <EOL> def test_override_on_class_object ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> pass <EOL> with self . assertRaises ( TypeError ) : <EOL> SubClass . f = lambda self : '<STR_LIT>' <EOL> with self . assertRaises ( TypeError ) : <EOL> SubClass . a = '<STR_LIT>' <EOL> def test_override_on_instance ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> def h ( self ) : <EOL> pass <EOL> s = SubClass ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> s . f = lambda self : '<STR_LIT>' <EOL> with self . assertRaises ( TypeError ) : <EOL> s . a = lambda self : '<STR_LIT>' <EOL> def test_override_on_super ( self ) : <EOL> """<STR_LIT>""" <EOL> old_a = self . class_ . a <EOL> old_f = self . class_ . f <EOL> try : <EOL> with self . assertRaises ( TypeError ) : <EOL> self . class_ . f = lambda * args : None <EOL> except Exception : <EOL> self . class_ . f = old_f <EOL> raise <EOL> try : <EOL> with self . assertRaises ( TypeError ) : <EOL> self . class_ . a = '<STR_LIT>' <EOL> except Exception : <EOL> self . class_ . a = old_a <EOL> raise <EOL> def test_override___setattr___on_subclass ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class SubClass ( self . class_ ) : <EOL> def __setattr__ ( self , name , value ) : <EOL> object . __setattr__ ( self , name , value ) <EOL> def test_override___setattr___on_instance ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> pass <EOL> s = SubClass ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> s . __setattr__ = lambda a , b : None <EOL> class FinalABCMetaTestCase ( FinalMetaTestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> FinalABCMeta = compose_types ( FinalMeta , ABCMeta ) <EOL> class ABCWithFinal ( with_metaclass ( FinalABCMeta , object ) ) : <EOL> a = final ( '<STR_LIT>' ) <EOL> b = '<STR_LIT>' <EOL> @ final <EOL> def f ( self ) : <EOL> return '<STR_LIT>' <EOL> def g ( self ) : <EOL> return '<STR_LIT>' <EOL> @ abstractmethod <EOL> def h ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT:h>' ) <EOL> cls . class_ = ABCWithFinal <EOL> def test_cannot_instantiate_subclass ( self ) : <EOL> """<STR_LIT>""" <EOL> class AbstractSubClass ( self . class_ ) : <EOL> pass <EOL> with self . assertRaises ( TypeError ) : <EOL> AbstractSubClass ( ) <EOL> def test_override_on_instance ( self ) : <EOL> class SubClass ( self . class_ ) : <EOL> def h ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> s = SubClass ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> s . f = lambda self : '<STR_LIT>' <EOL> def test_override___setattr___on_instance ( self ) : <EOL> """<STR_LIT>""" <EOL> class SubClass ( self . class_ ) : <EOL> def h ( self ) : <EOL> pass <EOL> s = SubClass ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> s . __setattr__ = lambda a , b : None <EOL> def test_subclass_setattr ( self ) : <EOL> """<STR_LIT>""" <EOL> class ClassWithFinal ( with_metaclass ( FinalMeta , object ) ) : <EOL> @ final <EOL> def f ( self ) : <EOL> return '<STR_LIT>' <EOL> class SubClass ( ClassWithFinal ) : <EOL> def __init__ ( self ) : <EOL> self . a = '<STR_LIT:a>' <EOL> SubClass ( ) <EOL> self . assertEqual ( SubClass ( ) . a , '<STR_LIT:a>' ) <EOL> self . assertEqual ( SubClass ( ) . f ( ) , '<STR_LIT>' ) <EOL> def test_final_classmethod ( self ) : <EOL> class ClassWithClassMethod ( with_metaclass ( FinalMeta , object ) ) : <EOL> count = <NUM_LIT:0> <EOL> @ final <EOL> @ classmethod <EOL> def f ( cls ) : <EOL> cls . count += <NUM_LIT:1> <EOL> return cls . count <EOL> with self . assertRaises ( TypeError ) : <EOL> class ClassOverridingClassMethod ( ClassWithClassMethod ) : <EOL> @ classmethod <EOL> def f ( cls ) : <EOL> return "<STR_LIT>" <EOL> with self . assertRaises ( TypeError ) : <EOL> ClassWithClassMethod . f = lambda cls : <NUM_LIT:0> <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:3> ) <EOL> instance = ClassWithClassMethod ( ) <EOL> with self . assertRaises ( TypeError ) : <EOL> instance . f = lambda cls : <NUM_LIT:0> <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( ClassWithClassMethod . f ( ) , <NUM_LIT:6> ) </s>
