Skip to content

Commit

Permalink
Refactor required args
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Mar 11, 2021
1 parent 4961225 commit f2e80fa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/internal/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def required_args(target, args=(), kwargs=()):
else target
)
except TypeError: # pragma: no cover
return None
return set()
# self appears in the argspec of __init__ and bound methods, but it's an
# error to explicitly supply it - so we might skip the first argument.
skip_self = int(inspect.isclass(target) or inspect.ismethod(target))
Expand Down
13 changes: 5 additions & 8 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ def builds(
"infer was passed as a positional argument to "
"builds(), but is only allowed as a keyword arg"
)
required = required_args(target, args, kwargs) or set()
required = required_args(target, args, kwargs)
to_infer = {k for k, v in kwargs.items() if v is infer}
if required or to_infer:
if isinstance(target, type) and attr.has(target):
Expand Down Expand Up @@ -1054,13 +1054,10 @@ def as_strategy(strat_or_callable, thing, final=True):
return sampled_from(thing)
# If we know that builds(thing) will fail, give a better error message
required = required_args(thing)
if required and not any(
[
required.issubset(get_type_hints(thing)),
attr.has(thing),
# NamedTuples are weird enough that we need a specific check for them.
is_typed_named_tuple(thing),
]
if required and not (
required.issubset(get_type_hints(thing))
or attr.has(thing)
or is_typed_named_tuple(thing) # weird enough that we have a specific check
):
raise ResolutionFailed(
"Could not resolve %r to a strategy; consider "
Expand Down

0 comments on commit f2e80fa

Please sign in to comment.