-
Notifications
You must be signed in to change notification settings - Fork 45
Testing keyword argument defaults #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ah this is interesting. How about a pattern where we always generate kwargs for the function tests? @given(..., kw=one_of(just({}), ...))
def test_foo(..., kw):
result = foo(..., **kw)
... If I'm correct in saying, Python interprets unpacking of an empty dict like A helper method therefore could be: @st.composite
def kwargs(draw, **kw):
result = {}
for k, strat in kw.items():
if draw(st.booleans()):
result[k] = draw(strat)
return result So for example: @given(
shape=shapes,
fill_value=xps.from_dtype(shared_dtypes),
kw=kwargs(dtype=one_of(none(), shared_dtypes)),
)
def test_full(shape, fill_value, kw):
a = full(shape, fill_value, **kw)
if "dtype" in kw.keys() and kw["dtype"] is not None:
assert a.dtype == kw["dtype"]
... Personally I prefer using |
Yes, your way is much better. We would also want some way to normalize the keyword arguments with the actual default values. So for instance in my diagonal example, we can just check that def test_diagonal(...):
all_kwargs = add_defaults(kwargs)
offset = all_kwargs['offset'] # Will be offset if provided or 0, the default, if not Or maybe we could make a pseudo-strategy that uses |
Oh I see. I think this pattern should be sufficient for anytime we want to test defaulting assumptions. def test_diagonal(..., kw):
res = diagonal(x, **kw)
offset = 0 if "offset" not in kw else kw["offset"]
... |
Let's just do that for now. If it gets too repetitive, we can see if something more streamlined makes sense, perhaps by extracting the default automatically from the stubs. |
@asmeurer What do you think about the current use of |
Yeah, working through the linalg tests, which have a lot of keyword arguments, I'm also thinking it would be a good idea, especially if we can extract the defaults automatically from the stubs. |
All the primary tests do this well now I think. |
We need to make sure all the function tests test the default values for keyword arguments, that is, calling the function without passing the keyword argument. This explicitly isn't tested in the signature tests because those tests aren't sophisticated enough to check that the correct default value is used.
I think we can abstract this somewhat in the hypothesis helpers. Something like
with a strategy helper
kwarg
that would work likeMaybe we could even abstract this further by generating using the function stubs. I'm not sure. The stubs won't contain enough information to infer what strategy values should be drawn from (even with type hints, it won't contain things like bounds or only specific sets of dtypes), but it does have the default values.
The text was updated successfully, but these errors were encountered: