Repository: wemake-services/wemake-python-styleguide

Issue:
Method `given_function_called` should only take the function name into account.
# Bug report

## What's wrong

The method `given_function_called(node: ast.Call, to_check: Container[str]) -> str` in `logic.tree.functions` is described as a method that returns the name of the function being called in `node`, in case it is included in `to_check`. For example:
 ```python
# Let's imagine we are visiting the Call node in `print(123, 456)` stored in `node`
called_function = given_function_called(node, ['print'])
print(called_function)
# Prints `print`
# But, if we are visiting `datetime.timedelta(days=1)`
called_function = given_function_called(node, ['timedelta'])
print(called_function)
# Prints an empty string, as if `timedelta` had not been called. The way for it to be shown is:
called_function = given_function_called(node, ['datetime.timedelta'])
print(called_function)
# Prints `datetime.timedelta`
```

This is related to https://github.com/wemake-services/wemake-python-styleguide/pull/1676#discussion_r508471791

## How is that should be

```python
# If we are visiting `datetime.timedelta(days=1)`
called_function = given_function_called(node, ['timedelta'])
print(called_function)
# Prints `timedelta`
```



Discussion:
Ofc I will be doing this since it is blocking my own PR. 

Resolving patch:
diff --git a/wemake_python_styleguide/logic/tree/functions.py b/wemake_python_styleguide/logic/tree/functions.py
--- a/wemake_python_styleguide/logic/tree/functions.py
+++ b/wemake_python_styleguide/logic/tree/functions.py
@@ -10,20 +10,21 @@
 )
 
 
-def given_function_called(node: Call, to_check: Container[str]) -> str:
+def given_function_called(
+    node: Call,
+    to_check: Container[str],
+    *,
+    split_modules: bool = False,
+) -> str:
     """
     Returns function name if it is called and contained in the container.
 
-    >>> import ast
-    >>> module = ast.parse('print(123, 456)')
-    >>> given_function_called(module.body[0].value, ['print'])
-    'print'
-
-    >>> given_function_called(module.body[0].value, ['adjust'])
-    ''
-
+    If `split_modules`, takes the modules or objects into account. Otherwise,
+    it only cares about the function's name.
     """
     function_name = source.node_to_string(node.func)
+    if split_modules:
+        function_name = function_name.split('.')[-1]
     if function_name in to_check:
         return function_name
     return ''


Test patch:
diff --git a/tests/test_logic/test_tree/test_functions.py b/tests/test_logic/test_tree/test_functions.py
new file mode 100644
--- /dev/null
+++ b/tests/test_logic/test_tree/test_functions.py
@@ -0,0 +1,59 @@
+import pytest
+
+from wemake_python_styleguide.logic.tree import functions
+
+
+@pytest.mark.parametrize(('function_call', 'function_name'), [
+    # Simple builtin functions
+    ('print("Hello world!")', 'print'),
+    ('int("10")', 'int'),
+    ('bool(1)', 'bool'),
+    ('open("/tmp/file.txt", "r")', 'open'),
+    ('str(10)', 'str'),
+
+    # Functions in modules
+    ('datetime.timedelta(days=1)', 'datetime.timedelta'),
+    ('cmath.sqrt(100)', 'cmath.sqrt'),
+
+    # Functions in (made up) objects
+    ('dt.strftime("%H:%M")', 'dt.strftime'),
+    ('obj.funct()', 'obj.funct'),
+])
+def test_given_function_called_no_split(
+    parse_ast_tree, function_call: str, function_name: str,
+) -> None:
+    """Test given_function_called without splitting the modules."""
+    tree = parse_ast_tree(function_call)
+    node = tree.body[0].value
+    called_function = functions.given_function_called(node, [function_name])
+    assert called_function == function_name
+
+
+@pytest.mark.parametrize(('function_call', 'function_name'), [
+    # Simple builtin functions
+    ('print("Hello world!")', 'print'),
+    ('int("10")', 'int'),
+    ('bool(1)', 'bool'),
+    ('open("/tmp/file.txt", "r")', 'open'),
+    ('str(10)', 'str'),
+
+    # Functions in modules
+    ('datetime.timedelta(days=1)', 'timedelta'),
+    ('cmath.sqrt(100)', 'sqrt'),
+
+    # Functions in (made up) objects
+    ('dt.strftime("%H:%M")', 'strftime'),
+    ('obj.funct()', 'funct'),
+])
+def test_given_function_called_with_split(
+    parse_ast_tree, function_call: str, function_name: str,
+) -> None:
+    """Test given_function_called splitting the modules."""
+    tree = parse_ast_tree(function_call)
+    node = tree.body[0].value
+    called_function = functions.given_function_called(
+        node,
+        [function_name],
+        split_modules=True,
+    )
+    assert called_function == function_name