Convert the validity functions to class functions to allow them to be used in __new__ methods.

This commit is contained in:
Mike Auty
2016-12-15 23:03:48 +00:00
parent bf61357ed2
commit 7719aaa960
+6 -4
View File
@@ -8,7 +8,8 @@ Created on 4 May 2013
class ValidityRoutines(object):
"""Class to hold all validation routines, such as type checking"""
def _check_type(self, value, valid_type):
@classmethod
def _check_type(cls, value, valid_type):
"""Checks that value is an instance of valid_type, and returns value if it is, or throws a TypeError otherwise
:param value: The value of which to validate the type
@@ -17,11 +18,12 @@ class ValidityRoutines(object):
:type valid_type: type
"""
assert isinstance(value,
valid_type), self.__class__.__name__ + " expected " + valid_type.__name__ + ", not " + type(
valid_type), cls.__name__ + " expected " + valid_type.__name__ + ", not " + type(
value).__name__
return value
def _check_class(self, klass, valid_class):
@classmethod
def _check_class(cls, klass, valid_class):
"""Checks that class is an instance of valid_class, and returns klass if it is, or throws a TypeError otherwise
:param klass: Class to validate
@@ -30,5 +32,5 @@ class ValidityRoutines(object):
:type valid_class: class
"""
assert issubclass(klass,
valid_class), self.__class__.__name__ + " expected " + valid_class.__name__ + ", not " + klass.__name__
valid_class), cls.__name__ + " expected " + valid_class.__name__ + ", not " + klass.__name__
return klass