parameters: relax type checks

Previously, the type check of the Param class did only allow
the parameters to only have a value that's of a direct type.
However, that's nonsensically restrictive. For example, if
there's an interface implemented as an `ABCMeta` class then
the check for type fails since the interface's type is
`ABCMeta` instead of directly a `type`. Among others,
this is the case for cryptography.x509.Certificate.

Being a type is a transitive property of a Python object and we
should respect that in our framework.

https://pagure.io/freeipa/issue/4985

Reviewed-By: Fraser Tweedale <ftweedal@redhat.com>
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
Stanislav Laznicka 2017-07-20 09:55:05 +02:00 committed by Pavel Vomacka
parent bf4dae70e0
commit 5ff1de8490

View File

@ -467,18 +467,15 @@ class Param(ReadOnly):
value = kind(value)
elif type(value) is str:
value = kind([value])
if (
type(kind) is type and not isinstance(value, kind)
or
type(kind) is tuple and not isinstance(value, kind)
):
raise TypeError(
TYPE_ERROR % (key, kind, value, type(value))
)
elif kind is callable and not callable(value):
if kind is callable and not callable(value):
raise TypeError(
CALLABLE_ERROR % (key, value, type(value))
)
elif (isinstance(kind, (type, tuple)) and
not isinstance(value, kind)):
raise TypeError(
TYPE_ERROR % (key, kind, value, type(value))
)
kw[key] = value
elif key not in ('required', 'multivalue'):
kw.pop(key, None)
@ -502,7 +499,7 @@ class Param(ReadOnly):
self.nice = '%s(%r)' % (self.__class__.__name__, self.param_spec)
# Make sure no unknown kw were given:
assert all(type(t) is type for t in self.allowed_types)
assert all(isinstance(t, type) for t in self.allowed_types)
if not set(t[0] for t in self.kwargs).issuperset(self.__kw):
extra = set(kw) - set(t[0] for t in self.kwargs)
raise TypeError(