Skip to content

Update FormattedValue.postinit and its brain #2029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ What's New in astroid 2.15.0?
=============================
Release date: TBA

* ``Formattedvalue.postinit`` is now keyword only. This is to allow correct typing of the
``Formattedvalue`` class.

Refs #1516

* ``Astroid`` now supports custom import hooks.

Refs PyCQA/pylint#7306
Expand Down
25 changes: 19 additions & 6 deletions astroid/brain/brain_fstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

from __future__ import annotations

import collections.abc
from typing import TypeVar

from astroid import nodes
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import FormattedValue

_NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)


def _clone_node_with_lineno(node, parent, lineno):
def _clone_node_with_lineno(
node: _NodeT, parent: nodes.NodeNG, lineno: int | None
) -> _NodeT:
cls = node.__class__
other_fields = node._other_fields
_astroid_fields = node._astroid_fields
Expand All @@ -28,21 +35,27 @@ def _clone_node_with_lineno(node, parent, lineno):
return new_node


def _transform_formatted_value(node): # pylint: disable=inconsistent-return-statements
def _transform_formatted_value( # pylint: disable=inconsistent-return-statements
node: nodes.FormattedValue,
) -> nodes.FormattedValue | None:
if node.value and node.value.lineno == 1:
if node.lineno != node.value.lineno:
new_node = FormattedValue(
new_node = nodes.FormattedValue(
lineno=node.lineno, col_offset=node.col_offset, parent=node.parent
)
new_value = _clone_node_with_lineno(
node=node.value, lineno=node.lineno, parent=new_node
)
new_node.postinit(value=new_value, format_spec=node.format_spec)
new_node.postinit(
value=new_value,
conversion=node.conversion,
format_spec=node.format_spec,
)
return new_node


# TODO: this fix tries to *patch* http://bugs.python.org/issue29051
# The problem is that FormattedValue.value, which is a Name node,
# has wrong line numbers, usually 1. This creates problems for pylint,
# which expects correct line numbers for things such as message control.
AstroidManager().register_transform(FormattedValue, _transform_formatted_value)
AstroidManager().register_transform(nodes.FormattedValue, _transform_formatted_value)
11 changes: 5 additions & 6 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4696,20 +4696,18 @@ def __init__(
self.value: NodeNG
"""The value to be formatted into the string."""

self.conversion: int | None = None # can be None
self.conversion: int
"""The type of formatting to be applied to the value.

.. seealso::
:class:`ast.FormattedValue`
"""

self.format_spec: NodeNG | None = None # can be None
self.format_spec: JoinedStr | None = None
"""The formatting to be applied to the value.

.. seealso::
:class:`ast.FormattedValue`

:type: JoinedStr or None
"""

super().__init__(
Expand All @@ -4722,9 +4720,10 @@ def __init__(

def postinit(
self,
*,
value: NodeNG,
conversion: int | None = None,
format_spec: NodeNG | None = None,
conversion: int,
format_spec: JoinedStr | None = None,
) -> None:
"""Do some setup after initialisation.

Expand Down
6 changes: 3 additions & 3 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,9 +1442,9 @@ def visit_formattedvalue(
parent=parent,
)
newnode.postinit(
self.visit(node.value, newnode),
node.conversion,
self.visit(node.format_spec, newnode),
value=self.visit(node.value, newnode),
conversion=node.conversion,
format_spec=self.visit(node.format_spec, newnode),
)
return newnode

Expand Down