Skip to content
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

Infer string interpolation with % #151

Closed
pylint-bot opened this issue Jul 3, 2015 · 1 comment · Fixed by #1617
Closed

Infer string interpolation with % #151

pylint-bot opened this issue Jul 3, 2015 · 1 comment · Fixed by #1617
Assignees
Labels
Enhancement ✨ Improvement to a component
Milestone

Comments

@pylint-bot
Copy link

Originally reported by: Claudiu Popa (BitBucket: PCManticore, GitHub: @PCManticore)


Not sure how important this is, but we could understand string interpolation, both msg % something, as well as msg.format(something). Currently, for the second form we are returning an empty string, which is wrong, while the first form we're returning an YES object.


@pylint-bot pylint-bot added the Enhancement ✨ Improvement to a component label Dec 9, 2015
@DanielNoord DanielNoord self-assigned this Jun 9, 2022
@DanielNoord
Copy link
Collaborator

Let's merge #1602 first and rescope this to inference of msg % something.

I have a WIP diff for this PR that fixes the most simple cases:

diff --git a/astroid/inference.py b/astroid/inference.py
index 61b46e10d..cd0108c87 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -562,12 +562,24 @@ def _is_not_implemented(const):
     return isinstance(const, nodes.Const) and const.value is NotImplemented
 
 
-def _invoke_binop_inference(instance, opnode, op, other, context, method_name):
+def _invoke_binop_inference(
+    instance, opnode, op, other, context: InferenceContext, method_name
+):
     """Invoke binary operation inference on the given instance."""
     methods = dunder_lookup.lookup(instance, method_name)
     context = bind_context_to_node(context, instance)
     method = methods[0]
     context.callcontext.callee = method
+
+    if isinstance(instance, nodes.Const) and op == "%":
+        if isinstance(other, nodes.Tuple):
+            inferred_positional = [helpers.safe_infer(i, context) for i in other.elts]
+            if all(isinstance(i, nodes.Const) for i in inferred_positional):
+                values_positional = [i.value for i in inferred_positional]
+                return iter(
+                    [nodes.const_factory(instance.value % tuple(values_positional))]
+                )
+
     try:
         inferred = next(method.infer(context=context))
     except StopIteration as e:

@DanielNoord DanielNoord changed the title Implement string interpolation? Infer string interpolation with % Jun 9, 2022
@DanielNoord DanielNoord changed the title Infer string interpolation with % Infer string interpolation with % Jun 9, 2022
@DanielNoord DanielNoord added this to the 2.12.0 milestone Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants