-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: improve error messages when add_field received a malformed funct…
…ion argument
- Loading branch information
1 parent
c1433df
commit c08c55d
Showing
8 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from functools import partial | ||
|
||
import pytest | ||
|
||
from yt.testing import fake_random_ds | ||
|
||
|
||
def test_add_field_lambda(): | ||
ds = fake_random_ds(16) | ||
|
||
ds.add_field( | ||
("bacon", "spam"), | ||
lambda field, data: data["gas", "density"], | ||
sampling_type="cell", | ||
) | ||
|
||
|
||
def test_add_field_partial(): | ||
ds = fake_random_ds(16) | ||
|
||
def _spam(field, data, factor): | ||
return factor * data["gas", "density"] | ||
|
||
ds.add_field( | ||
("bacon", "spam"), | ||
partial(_spam, factor=1), | ||
sampling_type="cell", | ||
) | ||
|
||
|
||
def test_add_field_arbitrary_callable(): | ||
ds = fake_random_ds(16) | ||
|
||
class Spam: | ||
def __call__(self, field, data): | ||
return data["gas", "density"] | ||
|
||
ds.add_field(("bacon", "spam"), Spam(), sampling_type="cell") | ||
|
||
|
||
def test_add_field_uncallable(): | ||
ds = fake_random_ds(16) | ||
|
||
class Spam: | ||
pass | ||
|
||
with pytest.raises( | ||
TypeError, match=r"Expected a callable object, got .* with type .*" | ||
): | ||
ds.add_field(("bacon", "spam"), Spam(), sampling_type="cell") | ||
|
||
|
||
def test_add_field_wrong_signature(): | ||
ds = fake_random_ds(16) | ||
|
||
def _spam(data, field): | ||
return data["gas", "density"] | ||
|
||
with pytest.raises( | ||
TypeError, | ||
match=( | ||
r"Received field function <function .*> with invalid signature\. " | ||
r"Expected exactly 2 positional parameters \('field', 'data'\), got \('data', 'field'\)" | ||
), | ||
): | ||
ds.add_field(("bacon", "spam"), _spam, sampling_type="cell") | ||
|
||
|
||
def test_add_field_wrong_signature_lambda(): | ||
ds = fake_random_ds(16) | ||
|
||
with pytest.raises( | ||
TypeError, | ||
match=( | ||
r"Received field function <function .*> with invalid signature\. " | ||
r"Expected exactly 2 positional parameters \('field', 'data'\), got \('data', 'field'\)" | ||
), | ||
): | ||
ds.add_field( | ||
("bacon", "spam"), | ||
lambda data, field: data["gas", "density"], | ||
sampling_type="cell", | ||
) | ||
|
||
|
||
def test_add_field_keyword_only(): | ||
ds = fake_random_ds(16) | ||
|
||
def _spam(field, *, data): | ||
return data["gas", "density"] | ||
|
||
with pytest.raises( | ||
TypeError, | ||
match=( | ||
r"Received field function .* with invalid signature\. " | ||
r"field and data parameters must accept positional values \(they cannot be keyword-only\)" | ||
), | ||
): | ||
ds.add_field( | ||
("bacon", "spam"), | ||
_spam, | ||
sampling_type="cell", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters