Use of None datatype and __new__ method for subclass instantiation
Let us have this example:
use kB from ase.units
use exp from numpy
kB_ = 1 [ eV/K ] * kB
ene = (v: 0.1, 0.05) [eV]
temperature = 273.15 [K]
beta = 1.0/(kB_*temperature)
print(sum(map((e: exp(-e*beta)), ene)))
The first error I get is
Traceback (most recent call last):
File "/home/vanjo/work/vre-language/examples/../scripts/run_model.py", line 34, in <module>
main()
File "/home/vanjo/work/vre-language/src/virtmat/language/utilities/errors.py", line 70, in wrapper
process_error(err)
File "/home/vanjo/work/vre-language/src/virtmat/language/utilities/errors.py", line 52, in process_error
raise err.__cause__
File "/home/vanjo/work/vre-language/src/virtmat/language/utilities/errors.py", line 22, in wrapper
return func(*args, **kwargs)
File "/home/vanjo/work/vre-language/src/virtmat/language/interpreter/instant_executor.py", line 373, in func_reduce_value
return self.type_(ret)
File "/usr/lib/python3.10/functools.py", line 981, in __get__
val = self.func(instance)
File "/home/vanjo/work/vre-language/src/virtmat/language/constraints/typechecks.py", line 466, in sum_type
assert issubclass(self.parameter.type_.datatype, (int, float))
TypeError: issubclass() arg 1 must be a class
After fixing this by adding if datatype is not None:
before the block calling issubclass
I get this result:
0.13381515446825232 dimensionless
It is correct by dimensionless
should not be printed due to the formatter. This becomes more obvious after adding
s = sum(map((e: exp(-e*beta)), ene))
print(s*kB_)
with the output:
1.1531293901786782e-05 dimensionless [electron_volt / kelvin]
After debugging the types I found out that the class <class 'virtmat.language.constraints.typechecks.Number'> produced by DType
metaclass is used for casting the output after evaluation of sum
taking a pint.Quantity
as and returns an object containing a pint.Quantity
as a magnitude! This is not due to the special metaclass but the behavior of all pint.Quantity
subclasses - this can be reproduced by simply subclassing pint.Quantity to MyQuantity and then taking MyQuantity(pint.Quantity(1)).magnitude
. The way to solve this is to write __new__
method of MyQuantity to override the default __new__
method.