OpcUaVariant
OpcUaVariant It is OPC UA a generic value container that covers all OPC UA built-in data types. Both SDK the read and write interfaces use it to pass values.
- Package ID + Timestamp + Status—please see OpcUaDataValue.
- For standard node constants, see WellKnownNodes.
Public Attributes
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Status | is_null | bool | R | Is null |
is_scalar | bool | R | Whether it is a scalar (= not is_null) | |
is_array | bool | R | Whether it is an array (currently always SDK False; arrays are detected via Read(ArrayDimensions)) | |
| Type | data_type | BuiltinType | R | Enumeration of built-in types (Boolean / Int32 / Double / String / DateTime / ...) |
handle | int | R | Resource handle; generally not used directly | |
| Value | value | Any | R | Pythonic decoding (auto-converts to bool/int/float/str/datetime based on data_type) |
Structure
OpcUaVariant() Create an empty container, then use set_* chained assignment. User-constructed objects hold their own OpcUaVariant resource handles, Must close() or use with.
from darra_opcua import OpcUaVariant # Module Name darra_opcua (pip package darra-opcua)
import datetime as dt
# Scalar Constructor (Streaming setter, always returns `self`)
OpcUaVariant().set_boolean(True) # Boolean
OpcUaVariant().set_sbyte(-1) # SByte
OpcUaVariant().set_byte(255) # Byte
OpcUaVariant().set_int16(42) # Int16
OpcUaVariant().set_uint16(42) # UInt16
OpcUaVariant().set_int32(42) # Int32
OpcUaVariant().set_uint32(42) # UInt32
OpcUaVariant().set_int64(42) # Int64
OpcUaVariant().set_float(3.14) # Float
OpcUaVariant().set_double(3.14) # Double
OpcUaVariant().set_string("hello") # String
OpcUaVariant().set_datetime(dt.datetime.now(dt.timezone.utc)) # DateTime
Array construction currently has no setters—all set_* operations are scalar-only (see “Implementation Limitations”).
Implementation Constraints
- No array setter:
set_*The series only contains scalars;is_arrayis constantFalse. To determine if a value is an array, read the node'sAttributeId.ArrayDimensionsproperty (do not determine it fromOpcUaVariant). set_sbyte/set_uint16take the compatibility route: there is no separate SByte / UInt16 storage;set_sbytestores values as Int16 andset_uint16stores values as UInt32. Useas_int32/as_uint32when retrieving values.- Composite types are not decoded:
valuereturnsNonefor Guid / QualifiedName / LocalizedText / ExtensionObject and so on. Custom structures go through Structure Decoding.
Read (Strongly Typed)
v.as_boolean
v.as_int32
v.as_uint32
v.as_int64
v.as_double # Float / Double (Universal)
v.as_string
v.as_datetime
v.value # Pythonic Auto-Decoding (Press data_type)
Returns a type mismatch None (without throwing an exception). Also returns a type mismatch when the value retrieval fails None.
ToString / repr
str(v) Click data_type Smart Formatting:
| data_type | Output Example |
|---|---|
| Double | 3.14 |
| String | hello |
| DateTime | 2026-04-25T12:00:00+00:00 (ISO 8601) |
| Null | <null> |
Other Types (value Unsolvable) | <TypeName>, for example <Guid> |
repr(v): Variant(Double=3.14). This is what came __repr__ out.Text, not a type name — a type name is OpcUaVariant.
Memory Ownership
OpcUaVariant Holds a resource handle. The source determines whether _owns:
| Source | _owns | Who is responsible for the release? |
|---|---|---|
| OpcUaVariant() User Structure | True | User (close / with / dispose) |
| ua.call(...) returns | True | User (close / with / dispose) |
| dv.variant Borrow | False | No need to release (followed by "release OpcUaDataValue ") |
# User-defined structures: Must be disposed of
v = OpcUaVariant().set_int32(42)
ua.write("ns=2;s=X", v)
v.close()
# Recommended: with (abbreviation)
with OpcUaVariant().set_int32(42) as v:
ua.write("ns=2;s=X", v)
# call Return: Must also call `dispose` (inputs may be omitted)
outputs = ua.call(obj, mtd)
for v in outputs:
with v:
print(v.as_int32)
# OpcUaDataValue Borrowed from: Don't close
with ua.read("ns=2;s=X") as dv:
v = dv.variant # Borrow (_owns=False)
print(v.as_int32)
# Do not use v.close() — the data is released along with the dv
BuiltinType Complete List
from darra_opcua import BuiltinType
BuiltinType.Null # 0
BuiltinType.Boolean # 1
BuiltinType.SByte # 2
BuiltinType.Byte # 3
BuiltinType.Int16 # 4
BuiltinType.UInt16 # 5
BuiltinType.Int32 # 6
BuiltinType.UInt32 # 7
BuiltinType.Int64 # 8
BuiltinType.UInt64 # 9
BuiltinType.Float # 10
BuiltinType.Double # 11
BuiltinType.String # 12
BuiltinType.DateTime # 13
BuiltinType.Guid # 14
BuiltinType.ByteString # 15
BuiltinType.XmlElement # 16
BuiltinType.NodeId # 17
BuiltinType.ExpandedNodeId # 18
BuiltinType.StatusCode # 19
BuiltinType.QualifiedName # 20
BuiltinType.LocalizedText # 21
BuiltinType.ExtensionObject # 22
BuiltinType.DataValue # 23
BuiltinType.Variant # 24
BuiltinType.DiagnosticInfo # 25
ExtensionObject (Complex Structure)
OPC UA server-side custom Struct DataTypes are wrapped in ExtensionObjects. BuiltinType.ExtensionObject is itself only a placeholder container: value comes back as None, so the inner fields cannot be read.
Deserializing fields of a custom structure Structure Decoding (StructDecoder) — It retrieves the field table using the server-side DataTypeDefinition property, then UA Binary resolves based on dict. Currently, it cannot retrieve the ExtensionObject TypeId or Body for, so decode() can only return a partial _status result containing (see “Implementation Limitations” on this page).
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| Variant | OpcUaVariant | — | — | — | — |
| DataType | data_type | — | — | — | — |
| Double = 3.14 (set-only property) | set_double(3.14) | — | — | — | — |
AsDouble (double?) | as_double (Optional[float]) | — | — | — | — |
| None | value | — | — | — | — |
Python's value is a convenient decoding feature on the Python side (converting data_type to bool/int/float/str/datetime), while C# has no equivalent—C# uses AsXxx to retrieve values one by one.
Java / C++ / Rust / C: The four columns marked "read-only, unverified" in this entry — indicate that the information has not been verified; it does not mean that it does not exist.