Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_fstrings.py: 37%
30 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
5from __future__ import annotations
7import collections.abc
8from typing import TypeVar
10from astroid import nodes
11from astroid.manager import AstroidManager
13_NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)
16def _clone_node_with_lineno(
17 node: _NodeT, parent: nodes.NodeNG, lineno: int | None
18) -> _NodeT:
19 cls = node.__class__
20 other_fields = node._other_fields
21 _astroid_fields = node._astroid_fields
22 init_params = {
23 "lineno": lineno,
24 "col_offset": node.col_offset,
25 "parent": parent,
26 "end_lineno": node.end_lineno,
27 "end_col_offset": node.end_col_offset,
28 }
29 postinit_params = {param: getattr(node, param) for param in _astroid_fields}
30 if other_fields:
31 init_params.update({param: getattr(node, param) for param in other_fields})
32 new_node = cls(**init_params)
33 if hasattr(node, "postinit") and _astroid_fields:
34 for param, child in postinit_params.items():
35 if child and not isinstance(child, collections.abc.Sequence):
36 cloned_child = _clone_node_with_lineno(
37 node=child, lineno=new_node.lineno, parent=new_node
38 )
39 postinit_params[param] = cloned_child
40 new_node.postinit(**postinit_params)
41 return new_node
44def _transform_formatted_value( # pylint: disable=inconsistent-return-statements
45 node: nodes.FormattedValue,
46) -> nodes.FormattedValue | None:
47 if node.value and node.value.lineno == 1:
48 if node.lineno != node.value.lineno:
49 new_node = nodes.FormattedValue(
50 lineno=node.lineno,
51 col_offset=node.col_offset,
52 parent=node.parent,
53 end_lineno=node.end_lineno,
54 end_col_offset=node.end_col_offset,
55 )
56 new_value = _clone_node_with_lineno(
57 node=node.value, lineno=node.lineno, parent=new_node
58 )
59 new_node.postinit(
60 value=new_value,
61 conversion=node.conversion,
62 format_spec=node.format_spec,
63 )
64 return new_node
67# TODO: this fix tries to *patch* http://bugs.python.org/issue29051
68# The problem is that FormattedValue.value, which is a Name node,
69# has wrong line numbers, usually 1. This creates problems for pylint,
70# which expects correct line numbers for things such as message control.
71AstroidManager().register_transform(nodes.FormattedValue, _transform_formatted_value)