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"""
19from astroid.manager import AstroidManager
20from astroid.nodes.scoped_nodes import FunctionDef
21
22COMPOSITE_NAMES = (
23 "composite",
24 "st.composite",
25 "strategies.composite",
26 "hypothesis.strategies.composite",
27)
28
29
30def is_decorated_with_st_composite(node: FunctionDef) -> 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
37
38
39def remove_draw_parameter_from_composite_strategy(node: FunctionDef) -> FunctionDef:
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 assert isinstance(node.args.args, list)
45 del node.args.args[0]
46 del node.args.annotations[0]
47 del node.args.type_comment_args[0]
48 return node
49
50
51def register(manager: AstroidManager) -> None:
52 manager.register_transform(
53 node_class=FunctionDef,
54 transform=remove_draw_parameter_from_composite_strategy,
55 predicate=is_decorated_with_st_composite,
56 )