Skip to content

Commit be2d39e

Browse files
committed
Update FormattedValue.postinit and its brain
1 parent d57e34f commit be2d39e

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ What's New in astroid 2.15.0?
66
=============================
77
Release date: TBA
88

9+
* ``Formattedvalue.postinit`` is now keyword only. This is to allow correct typing of the
10+
``Formattedvalue`` class.
11+
12+
Refs #1516
13+
914
* ``Astroid`` now supports custom import hooks.
1015

1116
Refs PyCQA/pylint#7306

astroid/brain/brain_fstrings.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
33
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
44

5+
from __future__ import annotations
6+
57
import collections.abc
8+
from typing import TypeVar
69

10+
from astroid import nodes
711
from astroid.manager import AstroidManager
8-
from astroid.nodes.node_classes import FormattedValue
12+
13+
_NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)
914

1015

11-
def _clone_node_with_lineno(node, parent, lineno):
16+
def _clone_node_with_lineno(
17+
node: _NodeT, parent: nodes.NodeNG, lineno: int | None
18+
) -> _NodeT:
1219
cls = node.__class__
1320
other_fields = node._other_fields
1421
_astroid_fields = node._astroid_fields
@@ -28,21 +35,27 @@ def _clone_node_with_lineno(node, parent, lineno):
2835
return new_node
2936

3037

31-
def _transform_formatted_value(node): # pylint: disable=inconsistent-return-statements
38+
def _transform_formatted_value( # pylint: disable=inconsistent-return-statements
39+
node: nodes.FormattedValue,
40+
) -> nodes.FormattedValue | None:
3241
if node.value and node.value.lineno == 1:
3342
if node.lineno != node.value.lineno:
34-
new_node = FormattedValue(
43+
new_node = nodes.FormattedValue(
3544
lineno=node.lineno, col_offset=node.col_offset, parent=node.parent
3645
)
3746
new_value = _clone_node_with_lineno(
3847
node=node.value, lineno=node.lineno, parent=new_node
3948
)
40-
new_node.postinit(value=new_value, format_spec=node.format_spec)
49+
new_node.postinit(
50+
value=new_value,
51+
conversion=node.conversion,
52+
format_spec=node.format_spec,
53+
)
4154
return new_node
4255

4356

4457
# TODO: this fix tries to *patch* http://bugs.python.org/issue29051
4558
# The problem is that FormattedValue.value, which is a Name node,
4659
# has wrong line numbers, usually 1. This creates problems for pylint,
4760
# which expects correct line numbers for things such as message control.
48-
AstroidManager().register_transform(FormattedValue, _transform_formatted_value)
61+
AstroidManager().register_transform(nodes.FormattedValue, _transform_formatted_value)

astroid/nodes/node_classes.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4696,20 +4696,18 @@ def __init__(
46964696
self.value: NodeNG
46974697
"""The value to be formatted into the string."""
46984698

4699-
self.conversion: int | None = None # can be None
4699+
self.conversion: int
47004700
"""The type of formatting to be applied to the value.
47014701
47024702
.. seealso::
47034703
:class:`ast.FormattedValue`
47044704
"""
47054705

4706-
self.format_spec: NodeNG | None = None # can be None
4706+
self.format_spec: JoinedStr | None = None
47074707
"""The formatting to be applied to the value.
47084708
47094709
.. seealso::
47104710
:class:`ast.FormattedValue`
4711-
4712-
:type: JoinedStr or None
47134711
"""
47144712

47154713
super().__init__(
@@ -4722,9 +4720,10 @@ def __init__(
47224720

47234721
def postinit(
47244722
self,
4723+
*,
47254724
value: NodeNG,
4726-
conversion: int | None = None,
4727-
format_spec: NodeNG | None = None,
4725+
conversion: int,
4726+
format_spec: JoinedStr | None = None,
47284727
) -> None:
47294728
"""Do some setup after initialisation.
47304729

astroid/rebuilder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,9 +1442,9 @@ def visit_formattedvalue(
14421442
parent=parent,
14431443
)
14441444
newnode.postinit(
1445-
self.visit(node.value, newnode),
1446-
node.conversion,
1447-
self.visit(node.format_spec, newnode),
1445+
value=self.visit(node.value, newnode),
1446+
conversion=node.conversion,
1447+
format_spec=self.visit(node.format_spec, newnode),
14481448
)
14491449
return newnode
14501450

0 commit comments

Comments
 (0)