-
-
Notifications
You must be signed in to change notification settings - Fork 329
Wrong inference for the str(const)
#2995
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
Open
jkmnt
wants to merge
6
commits into
pylint-dev:main
Choose a base branch
from
jkmnt:bug-2994
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39aa612
tests first
jkmnt 1228762
ok
jkmnt 492f7d5
ok
jkmnt e9757ce
handles the possible exception in str() call
jkmnt 40fdf2e
inferring all string values, not only the first one
jkmnt d6e686b
bump the patch test coverage
jkmnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -842,15 +842,35 @@ def infer_str(node, context: InferenceContext | None = None) -> nodes.Const: | |
|
|
||
| :param nodes.Call node: str() call to infer | ||
| :param context.InferenceContext: node context | ||
| :rtype nodes.Const: a Const containing an empty string | ||
| :rtype nodes.Const: | ||
| a Const containing a stringified value of str() call if possible, else an empty string | ||
| """ | ||
| call = arguments.CallSite.from_call(node, context=context) | ||
| if call.keyword_arguments: | ||
| raise UseInferenceDefault("TypeError: str() must take no keyword arguments") | ||
|
|
||
| fallback = nodes.Const("") | ||
|
|
||
| if not call.positional_arguments: | ||
| return fallback | ||
|
|
||
| # Accept only if all inferred values resolve to the same string | ||
| candidates: set[str] = set() | ||
| try: | ||
| return nodes.Const("") | ||
| except (AstroidTypeError, InferenceError) as exc: | ||
| raise UseInferenceDefault(str(exc)) from exc | ||
| for inferred in call.positional_arguments[0].infer(context=context): | ||
| if not isinstance(inferred, nodes.Const): | ||
| return fallback | ||
|
|
||
| try: | ||
| candidates.add(str(inferred.value)) | ||
| except ValueError: | ||
| return fallback | ||
| except InferenceError: | ||
| return fallback | ||
|
|
||
| if len(candidates) == 1: | ||
| return nodes.Const(candidates.pop()) | ||
|
Comment on lines
+871
to
+872
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like an unnecessary source of indeterminism. |
||
| return fallback | ||
|
|
||
|
|
||
| def infer_int(node, context: InferenceContext | None = None): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1407,6 +1407,7 @@ def test_infer_str() -> None: | |
| str(s) #@ | ||
| str('a') #@ | ||
| str(some_object()) #@ | ||
| str(7**10000) #@ | ||
| """) | ||
| for node in ast_nodes: | ||
| inferred = next(node.infer()) | ||
|
|
@@ -1420,6 +1421,37 @@ def test_infer_str() -> None: | |
| assert inferred.qname() == "builtins.str" | ||
|
|
||
|
|
||
| def test_infer_str_const() -> None: | ||
| ast_nodes = astroid.extract_node(""" | ||
| str('') #@ | ||
| str('a') #@ | ||
| str(1) #@ | ||
| str(True) #@ | ||
| str(False) #@ | ||
| str(None) #@ | ||
| str(4.33) #@ | ||
| str(...) #@ | ||
| str(2 + 2) #@ | ||
| str() #@ | ||
| str(int) #@ | ||
| str(2 if unknown() else 3) #@ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add a test ensuring user class StrWillFail:
def __str__(self):
raise RuntimeError
str(StrWillFail()) #@ |
||
| """) | ||
|
|
||
| inferred = list(node.inferred()[0].value for node in ast_nodes) | ||
| assert inferred[0] == "" | ||
| assert inferred[1] == "a" | ||
| assert inferred[2] == "1" | ||
| assert inferred[3] == "True" | ||
| assert inferred[4] == "False" | ||
| assert inferred[5] == "None" | ||
| assert inferred[6] == "4.33" | ||
| assert inferred[7] == "Ellipsis" | ||
| assert inferred[8] == "4" | ||
| assert inferred[9] == "" | ||
| assert inferred[10] == "" | ||
| assert inferred[11] == "" | ||
|
|
||
|
|
||
| def test_infer_int() -> None: | ||
| ast_nodes = astroid.extract_node(""" | ||
| int(0) #@ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we only run str() if the value is a builtin? I wouldn't want a more involved
str()implementation from user code to run (we should keep pylint static).