Path 1: 326 calls (0.42)

For (216) Comprehension (110)

None (326)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 2: 194 calls (0.25)

For (123) Comprehension (71)

None (194)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 3: 117 calls (0.15)

For (86) Comprehension (31)

None (117)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 4: 81 calls (0.1)

For (55) Comprehension (26)

None (81)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 5: 34 calls (0.04)

For (23) Comprehension (11)

'Foo.c_dict' (9) 'b_dict' (7) 'my_dict' (5) 'd' (4) 'a_dict' (2) 'out_of_scope_dict' (2) 'another_dict' (1) 'd_tuple' (1) 'dict1' (1) 'self.attribute'...

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 6: 21 calls (0.03)

Comprehension (15) For (6)

'{}' (9) 'DICT' (6) 'CustomClass()' (1) 'b_dict' (1) 'Foo.c_dict' (1) 'd' (1) 'Animal.__members__' (1) '{1: 2}' (1)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None
            

Path 7: 3 calls (0.0)

For (3)

None (3)

1def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
2    """Get the name of the dictionary which keys are being iterated over on
3    a ``nodes.For`` or ``nodes.Comprehension`` node.
4
5    If the iterating object is not either the keys method of a dictionary
6    or a dictionary itself, this returns None.
7    """
8    # Is it a proper keys call?
9    if (
10        isinstance(node.iter, nodes.Call)
11        and isinstance(node.iter.func, nodes.Attribute)
12        and node.iter.func.attrname == "keys"
13    ):
14        inferred = safe_infer(node.iter.func)
15        if not isinstance(inferred, astroid.BoundMethod):
16            return None
17        return node.iter.as_string().rpartition(".keys")[0]  # type: ignore[no-any-return]
18
19    # Is it a dictionary?
20    if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
21        inferred = safe_infer(node.iter)
22        if not isinstance(inferred, nodes.Dict):
23            return None
24        return node.iter.as_string()  # type: ignore[no-any-return]
25
26    return None