Skip to content

Commit

Permalink
drop custom flag and refactor MISSING_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Vergnaud committed Nov 5, 2024
1 parent 132cfb1 commit c8da26f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
13 changes: 5 additions & 8 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4715,7 +4715,7 @@ def _infer(
continue


MISSING_VALUE = "{MISSING_VALUE}"
UNINFERABLE_VALUE = "{Uninferable}"


class JoinedStr(NodeNG):
Expand All @@ -4729,9 +4729,6 @@ class JoinedStr(NodeNG):

_astroid_fields = ("values",)

"""Customer-defined behavior on what to return when _infer encounters an Uninferable element"""
FAIL_ON_UNINFERABLE = True

def __init__(
self,
lineno: int | None = None,
Expand Down Expand Up @@ -4797,9 +4794,9 @@ def _infer_with_values(
failed = (
inferred is util.Uninferable
or isinstance(inferred, Const)
and MISSING_VALUE in inferred.value
and UNINFERABLE_VALUE in inferred.value
)
if failed and self.FAIL_ON_UNINFERABLE:
if failed:
if not uninferable_already_generated:
uninferable_already_generated = True
yield util.Uninferable
Expand All @@ -4817,7 +4814,7 @@ def _infer_from_values(
if isinstance(node, Const):
yield node
continue
yield Const(MISSING_VALUE)
yield Const(UNINFERABLE_VALUE)
return
for prefix in cls._safe_infer_from_node(nodes[0], context, **kwargs):
for suffix in cls._infer_from_values(nodes[1:], context, **kwargs):
Expand All @@ -4826,7 +4823,7 @@ def _infer_from_values(
if isinstance(node, Const):
result += str(node.value)
continue
result += MISSING_VALUE
result += UNINFERABLE_VALUE
yield Const(result)

@classmethod
Expand Down
14 changes: 5 additions & 9 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7389,7 +7389,7 @@ def test_empty_format_spec() -> None:


@pytest.mark.parametrize(
"source, expected, fail",
"source, expected",
[
(
"""
Expand All @@ -7402,18 +7402,14 @@ class Cls:
s1 = f'{c_obj!r}' #@
""",
"<__main__.Cls",
False,
),
("s1 = f'{5}' #@", "5", False),
("s1 = f'{missing}'", None, True),
("s1 = f'{missing}'", "{MISSING_VALUE}", False),
("s1 = f'a/{missing}/b'", None, True),
("s1 = f'a/{missing}/b'", "a/{MISSING_VALUE}/b", False),
("s1 = f'{5}' #@", "5",),
("s1 = f'{missing}'", None),
("s1 = f'a/{missing}/b'", None),
],
)
def test_joined_str_returns_string(source, expected, fail) -> None:
def test_joined_str_returns_string(source, expected) -> None:
"""Regression test for https://github.com/pylint-dev/pylint/issues/9947."""
JoinedStr.FAIL_ON_UNINFERABLE = fail
node = extract_node(source)
assert isinstance(node, Assign)
target = node.targets[0]
Expand Down

0 comments on commit c8da26f

Please sign in to comment.