diff --git a/kwant/_system.pyx b/kwant/_system.pyx index 257377ac555388ce6b490946e347d7da8200217b..fcc3987c33834db8cf8ba52348234ec62e452d0e 100644 --- a/kwant/_system.pyx +++ b/kwant/_system.pyx @@ -242,6 +242,17 @@ def make_dense_full(ham, args, params, CGraph gr, diag, return h_sub +def _check_parameters_match(expected_parameters, params): + if params is None: + params = {} + missing = set(expected_parameters) - set(params) + + if missing: + msg = ('System is missing required parameters: ', + ', '.join(map('"{}"'.format, missing))) + raise TypeError(''.join(msg)) + + @cython.embedsignature(True) def hamiltonian_submatrix(self, args=(), to_sites=None, from_sites=None, sparse=False, return_norb=False, *, params=None): @@ -287,6 +298,9 @@ def hamiltonian_submatrix(self, args=(), to_sites=None, from_sites=None, n = self.graph.num_nodes matrix = ta.matrix + if not args: # Then perhaps parameters + _check_parameters_match(self.parameters, params) + if from_sites is None: diag = n * [None] from_norb = np.empty(n, gint_dtype) diff --git a/kwant/tests/test_builder.py b/kwant/tests/test_builder.py index 2d35df203e143f9b4bfd3acf39ffb20c6d2ef50a..cb2cb9c7f0d5dbe968b8f4e65fc22ffbbc0e5751 100644 --- a/kwant/tests/test_builder.py +++ b/kwant/tests/test_builder.py @@ -1225,9 +1225,11 @@ def test_argument_passing(): with raises(TypeError): inf_syst.hamiltonian(0, 0, *(2, 1), params=dict(p1=2, p2=1)) - # test that missing any parameters raises TypeError + # Missing parameters raises TypeError with raises(TypeError): - syst.hamiltonian(0, 0, params=dict(fake=10)) + syst.hamiltonian(0, 0, params=dict(p1=2)) + with raises(TypeError): + syst.hamiltonian_submatrix(params=dict(p1=2)) # test that passing parameters without default values works, and that # passing parameters with default values fails