@@ -153,7 +153,7 @@ def create_vector(
153153
154154
155155def create_matrix (
156- a : Form | Sequence [Sequence [Form ]],
156+ a : Form | Sequence [Sequence [Form | None ]],
157157 kind : str | Sequence [Sequence [str ]] | None = None ,
158158) -> PETSc .Mat :
159159 """Create a matrix compatible with a sequence of bilinear forms.
@@ -381,7 +381,7 @@ def _assemble_vector_petsc(
381381# -- Matrix assembly ------------------------------------------------------
382382@overload
383383def assemble_matrix (
384- a : Form | Sequence [Sequence [Form ]],
384+ a : Form | Sequence [Sequence [Form | None ]],
385385 bcs : Sequence [DirichletBC ] | None = None ,
386386 diag : float = 1.0 ,
387387 constants : npt .NDArray | Sequence [Sequence [npt .NDArray ]] | None = None ,
@@ -395,7 +395,7 @@ def assemble_matrix(
395395@overload
396396def assemble_matrix (
397397 A : PETSc .Mat ,
398- a : Form | Sequence [Sequence [Form ]],
398+ a : Form | Sequence [Sequence [Form | None ]],
399399 bcs : Sequence [DirichletBC ] | None = None ,
400400 diag : float = 1.0 ,
401401 constants : npt .NDArray | Sequence [Sequence [npt .NDArray ]] | None = None ,
@@ -409,7 +409,7 @@ def assemble_matrix(
409409
410410@functools .singledispatch
411411def assemble_matrix (
412- a : Form | Sequence [Sequence [Form ]],
412+ a : Form | Sequence [Sequence [Form | None ]],
413413 bcs : Sequence [DirichletBC ] | None = None ,
414414 diag : float = 1 ,
415415 constants : npt .NDArray | Sequence [Sequence [npt .NDArray ]] | None = None ,
@@ -478,7 +478,7 @@ def assemble_matrix(
478478@assemble_matrix .register # type: ignore[attr-defined]
479479def _assemble_matrix_petsc (
480480 A : PETSc .Mat ,
481- a : Form | Sequence [Sequence [Form ]],
481+ a : Form | Sequence [Sequence [Form | None ]],
482482 bcs : Sequence [DirichletBC ] | None = None ,
483483 diag : float = 1 ,
484484 constants : npt .NDArray | Sequence [Sequence [npt .NDArray ]] | None = None ,
@@ -513,12 +513,12 @@ def _assemble_matrix_petsc(
513513 if a_block is not None :
514514 Asub = A .getNestSubMatrix (i , j )
515515 _assemble_matrix_petsc (Asub , a_block , bcs , diag , const , coeff )
516- elif i == j :
516+ elif i == j and bcs is not None :
517517 for bc in bcs :
518518 row_forms = [row_form for row_form in a_row if row_form is not None ]
519519 if len (row_forms ) == 0 :
520520 raise ValueError (f"Row { i } of forms is entirely 'None'." )
521- if row_forms [0 ].function_spaces [0 ].contains (bc .function_space ._cpp_object ):
521+ if row_forms [0 ].function_spaces [0 ].contains (bc .function_space ._cpp_object ): # type: ignore
522522 raise RuntimeError (
523523 f"Diagonal sub-block ({ i } , { j } ) cannot be 'None'"
524524 " and have DirichletBC applied."
@@ -559,7 +559,7 @@ def _assemble_matrix_petsc(
559559 )
560560 A .restoreLocalSubMatrix (is0 [i ], is1 [j ], Asub )
561561 elif i == j :
562- for bc in _bcs :
562+ for bc in _bcs : # type: ignore
563563 row_forms = [row_form for row_form in a_row if row_form is not None ]
564564 if len (row_forms ) == 0 :
565565 raise ValueError (f"Row { i } of forms is entirely 'None'." )
@@ -599,7 +599,7 @@ def _assemble_matrix_petsc(
599599
600600def apply_lifting (
601601 b : PETSc .Vec ,
602- a : Sequence [Form ] | Sequence [Sequence [Form ]],
602+ a : Sequence [Form | None ] | Sequence [Sequence [Form | None ]],
603603 bcs : Sequence [DirichletBC ] | Sequence [Sequence [DirichletBC ]] | None ,
604604 x0 : Sequence [PETSc .Vec ] | None = None ,
605605 alpha : float = 1 ,
@@ -665,12 +665,16 @@ def apply_lifting(
665665 """
666666 if b .getType () == PETSc .Vec .Type .NEST :
667667 x0 = [] if x0 is None else x0 .getNestSubVecs () # type: ignore[attr-defined]
668- constants = [pack_constants (forms ) for forms in a ] if constants is None else constants # type: ignore[assignment]
669- coeffs = [pack_coefficients (forms ) for forms in a ] if coeffs is None else coeffs # type: ignore[misc]
668+ if constants is None :
669+ constants = [pack_constants (forms ) for forms in a ] # type: ignore
670+ if coeffs is None :
671+ coeffs = [pack_coefficients (forms ) for forms in a ] # type: ignore
672+ assert coeffs is not None
673+ assert constants is not None
670674 for b_sub , a_sub , const , coeff in zip (
671675 b .getNestSubVecs (),
672676 a ,
673- constants , # type: ignore[arg-type]
677+ constants ,
674678 coeffs ,
675679 strict = True ,
676680 ):
@@ -697,8 +701,8 @@ def apply_lifting(
697701 for i , (a_ , off0 , off1 , offg0 , offg1 ) in enumerate (
698702 zip (a , offset0 [:- 1 ], offset0 [1 :], offset1 [:- 1 ], offset1 [1 :], strict = True )
699703 ):
700- const = pack_constants (a_ ) if constants is None else constants [i ] # type: ignore[call-overload]
701- coeff = pack_coefficients (a_ ) if coeffs is None else coeffs [i ] # type: ignore[index, call-overload, assignment]
704+ const = pack_constants (a_ ) if constants is None else constants [i ] # type: ignore
705+ coeff = pack_coefficients (a_ ) if coeffs is None else coeffs [i ] # type: ignore
702706 const_ = [
703707 np .empty (0 , dtype = PETSc .ScalarType ) if val is None else val
704708 for val in const
@@ -1085,7 +1089,7 @@ def a(self) -> Form | Sequence[Sequence[Form]]:
10851089 return typing .cast (Form | Sequence [Sequence [Form ]], self ._a )
10861090
10871091 @property
1088- def preconditioner (self ) -> Form | Sequence [Sequence [Form ]] | None :
1092+ def preconditioner (self ) -> Form | Sequence [Sequence [Form | None ]] | None :
10891093 """The compiled bilinear form representing the preconditioner."""
10901094 return self ._preconditioner
10911095
@@ -1280,7 +1284,7 @@ class NonlinearProblem(typing.Generic[_U]):
12801284 """ # noqa: D301
12811285
12821286 _P_mat : PETSc .Mat | None
1283- _preconditioner : Form | Sequence [Sequence [Form ]] | None
1287+ _preconditioner : Form | Sequence [Sequence [Form | None ]] | None
12841288
12851289 @typing .overload
12861290 def __init__ (
@@ -1539,7 +1543,7 @@ def J(self) -> Form | Sequence[Sequence[Form]]:
15391543 return typing .cast (Form | Sequence [Sequence [Form ]], self ._J )
15401544
15411545 @property
1542- def preconditioner (self ) -> Form | Sequence [Sequence [Form ]] | None :
1546+ def preconditioner (self ) -> Form | Sequence [Sequence [Form | None ]] | None :
15431547 """The compiled preconditioner."""
15441548 return self ._preconditioner
15451549
0 commit comments