[mlir][python] Add __{bool,float,int,str}__ to bindings of attributes.
This allows to use Python's `bool(.)`, `float(.)`, `int(.)`, and `str(.)` to convert pybound attributes to the corresponding native Python types. In particular, pybind11 uses these functions to automatically cast objects to the corresponding primitive types wherever they are required by pybound functions, e.g., arguments are converted to Python's `int` if the C++ signature requires a C++ `int`. With this patch, pybound attributes can by used wherever the corresponding native types are expected. New tests show-case this behavior in the constructors of `Dense*ArrayAttr`. Note that this changes the output of Python's `str` on `StringAttr` from `"hello"` to `hello`. Arguably, this is still in line with `str`s goal of producing a readable interpretation of the value, even if it is now not unambiously a string anymore (`print(ir.Attribute.parse('"42"'))` now outputs `42`). However, this is consistent with instances of Python's `str` (`print("42")` outputs `42`), and `repr` still provides an unambigous representation if one is required. Reviewed By: springerm Differential Revision: https://reviews.llvm.org/D158974
Loading
Please sign in to comment