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
4
5"""
6Astroid hook for the Hypothesis library.
7
8Without this hook pylint reports no-value-for-parameter for use of strategies
9defined using the `@hypothesis.strategies.composite` decorator. For example:
10
11 from hypothesis import strategies as st
12
13 @st.composite
14 def a_strategy(draw):
15 return draw(st.integers())
16
17 a_strategy()
18"""
19
20from astroid.manager import AstroidManager
21from astroid.nodes.scoped_nodes import FunctionDef
22
23COMPOSITE_NAMES = (
24 "composite",
25 "st.composite",
26 "strategies.composite",
27 "hypothesis.strategies.composite",
28)
29
30
31def is_decorated_with_st_composite(node: FunctionDef) -> bool:
32 """Return whether a decorated node has @st.composite applied."""
33 if node.decorators and node.args.args and node.args.args[0].name == "draw":
34 for decorator_attribute in node.decorators.nodes:
35 if decorator_attribute.as_string() in COMPOSITE_NAMES:
36 return True
37 return False
38
39
40def remove_draw_parameter_from_composite_strategy(node: FunctionDef) -> FunctionDef:
41 """Given that the FunctionDef is decorated with @st.composite, remove the
42 first argument (`draw`) - it's always supplied by Hypothesis so we don't
43 need to emit the no-value-for-parameter lint.
44 """
45 assert isinstance(node.args.args, list)
46 del node.args.args[0]
47 del node.args.annotations[0]
48 del node.args.type_comment_args[0]
49 return node
50
51
52def register(manager: AstroidManager) -> None:
53 manager.register_transform(
54 node_class=FunctionDef,
55 transform=remove_draw_parameter_from_composite_strategy,
56 predicate=is_decorated_with_st_composite,
57 )