ranged_int = type('ranged_int', (int,), {}) # # define a type alias # type RangedInt = int class RangeObj: def __new__(self, floor, ceil): if ceil <= floor or floor >= ceil: raise ValueError( "Invalid format for type :" " must specify correct `ceil` and `floor` values" " ceil cannot be less than or equal to floor and vice versa" ) return type('IntRange', (), {'ceil': ceil, 'floor': floor}) class RangedInt(int): def __new__(cls, range_object: RangeObj, value: int) -> RangeObj: if value < range_object.floor or value >= range_object.ceil: raise ValueError( "Invalid number specified, value cannot be greater" " than or equal to or less" " than " ) return super().__new__(cls, value) # test R1 = RangeObj(5, 10) x1 = RangedInt(R1, 9) print(x1) # prints the value as expected x2 = RangedInt(R1, 12) # raises an ValueError exception