-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bias less towards classes over functions
- Loading branch information
Carl Meyer
committed
May 18, 2023
1 parent
fec3184
commit 1f98dce
Showing
2 changed files
with
15 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,18 +49,22 @@ def record_targets(tree: ast.Module) -> ast.Module: | |
num_lambdas = 0 | ||
num_listcomps = 0 | ||
num_classes = 0 | ||
num_functions = 0 | ||
for n in nodes: | ||
if isinstance(n, ast.Lambda): | ||
num_lambdas += 1 | ||
elif isinstance(n, ast.ListComp): | ||
num_listcomps += 1 | ||
elif isinstance(n, ast.ClassDef): | ||
num_classes += 100 | ||
num_classes += 1 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
carljm
Owner
|
||
elif isinstance(n, ast.FunctionDef): | ||
num_functions += 1 | ||
# hypothesis will aim for samples that maximize these metrics | ||
for value, label in [ | ||
(num_lambdas, "(modules) number of lambdas"), | ||
(num_listcomps, "(modules) number of listcomps"), | ||
(num_classes, "(modules) number of classes"), | ||
(num_functions, "(modules) number of functions"), | ||
]: | ||
target(float(value), label=label) | ||
return to_source(tree) | ||
|
@@ -259,7 +263,12 @@ def functions(draw): | |
|
||
|
||
def module_level_statements(): | ||
return st.from_type(ast.ClassDef) | ||
# require top-level class or function; plain module-level comprehensions are | ||
# not very interesting in terms of finding scoping bugs | ||
return st.one_of( | ||
st.from_type(ast.ClassDef), | ||
st.from_type(ast.FunctionDef) | ||
) | ||
|
||
|
||
st.register_type_strategy( | ||
|
FYI my experience was that this didn't make much difference; when i ran it with both top-level functions and classes, but with this 100-fold boost, I still got a lot of functions. The hypothesis docs suggest that the boost takes a long time (on the order of 10k attempts) to take effect, so maybe that's why.