Here is a minimal updated example for the latest version of DOLFINx:
from dolfinx import mesh, fem
from dolfinx.fem import FunctionSpace, Function,Constant
from ufl import TestFunction, dx, inner
import numpy as np
from mpi4py import MPI
class MyExpression:
def __init__(self):
self.t = 0.0
def eval(self, x):
# Added some spatial variation here. Expression is sin(t)*x
return np.full(x.shape[1], np.sin(self.t)*x[0])
# If your Expression is not spatially dependent, use Constant
# f = Constant(mesh, 0)
# L = inner(f, v)*dx
# f.value = np.sin(2)
mesh = mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = FunctionSpace(mesh, ("CG", 1))
f = MyExpression()
f.t = 0
w = Function(V)
v = TestFunction(V)
L = inner(w, v)*dx
w.interpolate(f.eval)
print(fem.petsc.assemble_vector(fem.form(L)))