Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_hypothesis.py: 56%
16 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
5"""
6Astroid hook for the Hypothesis library.
8Without this hook pylint reports no-value-for-parameter for use of strategies
9defined using the `@hypothesis.strategies.composite` decorator. For example:
11 from hypothesis import strategies as st
13 @st.composite
14 def a_strategy(draw):
15 return draw(st.integers())
17 a_strategy()
18"""
19from astroid.manager import AstroidManager
20from astroid.nodes.scoped_nodes import FunctionDef
22COMPOSITE_NAMES = (
23 "composite",
24 "st.composite",
25 "strategies.composite",
26 "hypothesis.strategies.composite",
27)
30def is_decorated_with_st_composite(node) -> bool:
31 """Return whether a decorated node has @st.composite applied."""
32 if node.decorators and node.args.args and node.args.args[0].name == "draw":
33 for decorator_attribute in node.decorators.nodes:
34 if decorator_attribute.as_string() in COMPOSITE_NAMES:
35 return True
36 return False
39def remove_draw_parameter_from_composite_strategy(node):
40 """Given that the FunctionDef is decorated with @st.composite, remove the
41 first argument (`draw`) - it's always supplied by Hypothesis so we don't
42 need to emit the no-value-for-parameter lint.
43 """
44 del node.args.args[0]
45 del node.args.annotations[0]
46 del node.args.type_comment_args[0]
47 return node
50AstroidManager().register_transform(
51 node_class=FunctionDef,
52 transform=remove_draw_parameter_from_composite_strategy,
53 predicate=is_decorated_with_st_composite,
54)