Adapt TemplatizableDict to python3 hasattr
In python 3, hasattr() expects __getattr__ to raise an AttributeError if the attribute does not exist. This updates TemplatizableDict accordingly. Change-Id: I989b5c5d8888bce47fa8c8d7157097d3ea48e0b5
This commit is contained in:
parent
1f3e1180d1
commit
fb168515d9
|
@ -0,0 +1,28 @@
|
|||
# Copyright 2019 Objectif Libre
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
#
|
||||
import unittest
|
||||
|
||||
from cloudkittydashboard import utils
|
||||
|
||||
|
||||
class TemplatizableDictTest(unittest.TestCase):
|
||||
|
||||
def test_hasattr_attr_exists(self):
|
||||
obj = utils.TemplatizableDict(a=1, b=2)
|
||||
self.assertTrue(hasattr(obj, 'a'))
|
||||
|
||||
def test_hasattr_attr_does_not_exist(self):
|
||||
obj = utils.TemplatizableDict(a=1, b=2)
|
||||
self.assertFalse(hasattr(obj, 'c'))
|
|
@ -19,7 +19,9 @@ class TemplatizableDict(dict):
|
|||
"""Class allowing to pass a dict to horizon templates"""
|
||||
|
||||
def __getattr__(self, key):
|
||||
return self[key]
|
||||
if key in self.keys():
|
||||
return self[key]
|
||||
raise AttributeError("Object has no {} attribute".format(key))
|
||||
|
||||
def __setattr__(self, key, val):
|
||||
self[key] = val
|
||||
|
|
Loading…
Reference in New Issue