Skip to content

Commit

Permalink
FunctionMetadata: Catch conversion exception
Browse files Browse the repository at this point in the history
If we can't provide the desired type, let's provide the raw data so the
bear or whatever can at least try by itself.

Fixes: #194
  • Loading branch information
sils committed Feb 16, 2015
1 parent 8cdec65 commit 919a6e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
24 changes: 19 additions & 5 deletions coalib/settings/FunctionMetadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from inspect import isfunction, ismethod, getfullargspec

from coalib.settings.DocumentationComment import DocumentationComment
from coalib.output.printers.ConsolePrinter import ConsolePrinter
from coalib.misc.i18n import _


Expand Down Expand Up @@ -49,7 +50,9 @@ def __init__(self,
self.non_optional_params = non_optional_params
self.optional_params = optional_params

def create_params_from_section(self, section):
def create_params_from_section(self,
section,
log_printer=ConsolePrinter()):
"""
Create a params dictionary for this function that holds all values the
function needs plus optional ones that are available.
Expand All @@ -68,20 +71,31 @@ def create_params_from_section(self, section):

for param in self.non_optional_params:
desc, annotation = self.non_optional_params[param]
params[param] = self.get_param(param, section, annotation)
params[param] = self._get_param(param,
section,
annotation,
log_printer)

for param in self.optional_params:
if param in section:
desc, annotation, default = self.optional_params[param]
params[param] = self.get_param(param, section, annotation)
params[param] = self._get_param(param,
section,
annotation,
log_printer)

return params

@staticmethod
def get_param(param, section, annotation):
def _get_param(param, section, annotation, log_printer):
if annotation is None:
annotation = lambda x: x
return annotation(section[param])
try:
return annotation(section[param])
except:
log_printer.warn(_("Unable to convert {param} to the desired "
"data type.").format(param=param))
return section[param]

@classmethod
def from_function(cls, func, omit=[]):
Expand Down
7 changes: 5 additions & 2 deletions coalib/tests/bearlib/spacing/SpacingHelperTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ def test_construction(self):
self.assertEqual(self.uut.tab_width, self.uut.from_section(section).tab_width)

section.append(Setting("tab_width", "invalid"))
self.assertRaises(ValueError, self.uut.from_section, section)
# Setting won't be converted since it's not possible, SpacingHelper
# will then complain with TypeError
self.assertRaises(TypeError, self.uut.from_section, section)

# This is assumed in some tests. If you want to change this value, be sure to change the tests too
# This is assumed in some tests. If you want to change this value, be
# sure to change the tests too
self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

Expand Down
28 changes: 24 additions & 4 deletions coalib/tests/settings/FunctionMetadataTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
sys.path.insert(0, ".")
import unittest
from coalib.settings.FunctionMetadata import FunctionMetadata
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting


class TestClass:
Expand All @@ -15,16 +17,28 @@ def __init__(self, param1, param2, param3=5, param4: int=6):
:return: ret
"""

def bad_function(self, bad_param: "no function"):
pass


class FunctionMetadataTestCase(unittest.TestCase):
def test_construction(self):
self.assertRaises(TypeError, FunctionMetadata, 5)
self.assertRaises(TypeError, FunctionMetadata, "name", desc=5)
self.assertRaises(TypeError, FunctionMetadata, "name", retval_desc=5)
self.assertRaises(TypeError, FunctionMetadata, "name", non_optional_params=5)
self.assertRaises(TypeError, FunctionMetadata, "name", optional_params=5)
self.assertRaises(TypeError,
FunctionMetadata,
"name",
non_optional_params=5)
self.assertRaises(TypeError,
FunctionMetadata,
"name",
optional_params=5)
self.assertRaises(TypeError, FunctionMetadata.from_function, 5)
self.assertRaises(TypeError, FunctionMetadata.from_function, self.test_construction, 5)
self.assertRaises(TypeError,
FunctionMetadata.from_function,
self.test_construction,
5)
self.check_function_metadata_data_set(FunctionMetadata("name"), "name")

def test_from_function(self):
Expand Down Expand Up @@ -58,6 +72,13 @@ def test_from_function(self):
"param4": ("p4 desc (" + uut.str_optional.format("6") + ")", int, 6)
})

def test_create_params_from_section(self):
section = Section("name")
section.append(Setting("bad_param", "value"))
uut = FunctionMetadata.from_function(TestClass(5, 5).bad_function)
params = uut.create_params_from_section(section)
self.assertIsInstance(params["bad_param"], Setting)

def check_function_metadata_data_set(self,
metadata,
name,
Expand All @@ -72,6 +93,5 @@ def check_function_metadata_data_set(self,
self.assertEqual(metadata.optional_params, optional_params)



if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 919a6e4

Please sign in to comment.