Python @properties raising an error
Question: I am trying to write a class to pass the following unittest:

    
    
    import unittest
    from property_address import *
    
    class TestAddresses(unittest.TestCase): 
    
        def setUp(self): 
            self.home = Address( name='Steve Holden', street_address='1972 Flying Circus', city='Arlington', state='VA', zip_code='12345' )
    
        def test_name(self): 
            self.assertEqual(self.home.name, 'Steve Holden') 
            self.assertRaises(AttributeError, setattr, self.home, 'name', 'Daniel Greenfeld')  
    
        def test_state(self): 
            self.assertEqual(self.home.state, 'VA') 
            self.assertRaises(StateError, setattr, self.home, 'state', 'Not a state')  
            self.home.state = 'CO' 
            self.assertEqual(self.home.state, 'CO')  
    

The part I am having issues with is the self.assertRaises(StateError, setattr,
self.home, 'state', 'Not a state')

I can't seem how to get a StatError to be raised.

The code I am using is:

    
    
    class Address(object):
    
        states = ['IA', 'KS', 'UT', 'VA', 'NC', 'NE', 'SD', 'AL', 'ID', 'FM', 'DE', 'AK', 'CT', 'PR', 'NM', 'MS', 'PW', 'CO', 'NJ', 'FL', 'MN', 
                  'VI', 'NV', 'AZ', 'WI', 'ND', 'PA', 'OK', 'KY', 'RI', 'NH', 'MO', 'ME', 'VT', 'GA', 'GU', 'AS', 'NY', 'CA', 'HI', 'IL', 'TN', 
                  'MA', 'OH', 'MD', 'MI', 'WY', 'WA', 'OR', 'MH', 'SC', 'IN', 'LA', 'MP', 'DC', 'MT', 'AR', 'WV', 'TX']
    
    
        def __init__(self,name, street_address, city, state, zip_code):
            self._name = name
            self._street_address = street_address
            self._city = city
            self._state = state
            self._zip_code = zip_code
    
    
        @property
        def name(self):
            return self._name.title()
    
        @property
        def state(self):
            return self._state
    
        @state.setter
        def state(self,value):
            if value in self.states:
                self._state = value
            else:
                raise ### This is where I am stuck
    

do I need to create a new @property for StateError, or should I work it into
state def somehow.


Answer: You need to raise a `StateError` exception; that is all:

    
    
    @state.setter
    def state(self,value):
        if value not in self.states:
            raise StateError(value)
        self._state = value
    

This does require you to have defined the exception class first, of course:

    
    
    class StateError(Exception):
        """Invalid state value used"""
    

Demo:

    
    
    >>> class StateError(Exception): pass
    ... 
    >>> class Address(object):
    ...     states = ['IA', 'KS', 'UT', 'VA', 'NC', 'NE', 'SD', 'AL', 'ID', 'FM', 'DE', 'AK', 'CT', 'PR', 'NM', 'MS', 'PW', 'CO', 'NJ', 'FL', 'MN', 
    ...               'VI', 'NV', 'AZ', 'WI', 'ND', 'PA', 'OK', 'KY', 'RI', 'NH', 'MO', 'ME', 'VT', 'GA', 'GU', 'AS', 'NY', 'CA', 'HI', 'IL', 'TN', 
    ...               'MA', 'OH', 'MD', 'MI', 'WY', 'WA', 'OR', 'MH', 'SC', 'IN', 'LA', 'MP', 'DC', 'MT', 'AR', 'WV', 'TX']...     @property
    ...     def state(self):
    ...         return self._state
    ...     @state.setter
    ...     def state(self,value):
    ...         if value not in self.states:
    ...             raise StateError(value)
    ...         self._state = value
    ...
    >>> a = Address()
    >>> a.state = 'VA'
    >>> a.state = 'Nonesuch'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 11, in state
    __main__.StateError: Nonesuch
    

