From 5ff1de84909dd65c44b9aa48000b9e73a7a93716 Mon Sep 17 00:00:00 2001 From: Stanislav Laznicka Date: Jul 27 2017 08:28:58 +0000 Subject: 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 Reviewed-By: Rob Crittenden Reviewed-By: Martin Basti --- diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 107cc90..7f19642 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -467,17 +467,14 @@ 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) - ): + if kind is callable and not callable(value): raise TypeError( - TYPE_ERROR % (key, kind, value, type(value)) + CALLABLE_ERROR % (key, value, type(value)) ) - elif kind is callable and not callable(value): + elif (isinstance(kind, (type, tuple)) and + not isinstance(value, kind)): raise TypeError( - CALLABLE_ERROR % (key, value, type(value)) + TYPE_ERROR % (key, kind, value, type(value)) ) kw[key] = value elif key not in ('required', 'multivalue'): @@ -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(