Enable use of type_ in isinstance() and instantiation from type_
Currently, for almost every model object a new type is created. In adition, the actual values are not instantiated from this type. This prevents the use in isinstance()
and issubclass()
, for example:
obj1.type_ = DType('mytype', (ureg.Quantity,), {'datatype': float})
obj2.type_ = DType('mytype', (ureg.Quantity,), {'datatype': float})
isinstance(obj1.value, obj1.type_) # true only if obj1.value is instantiated from obj1.type_
isinstance(obj1.value, obj2.type_) # never true though the two types are identical; they are not the same
The different objects should reuse the same type. To implement this, maybe a singleton metaclass can be used. Then the metaclass does not need __hash__
and __eq__
methods as the DType
class has.
Another example. Currently we make checks in the tests in this way:
assert issubclass(energy.type_, typemap['Number'])
assert isinstance(energy.value, typemap['Number'])
In addition, this test should become possible:
assert isinstance(energy.value, energy.type_)
In addition, None
shall not be used to describe unknown types. Instead, a special type shall be used, something like unknown_type = type('UnknownType', (type,), {})
. Even better it would be to return just type
(or DType
) because DType
is derived from type
and all other custom types are instantiated using DType
as metaclass.