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

Fix a crash when calling copy.copy() without args #8784

Merged
merged 2 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8774.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a crash when calling ``copy.copy()`` without arguments.

Closes #8774
13 changes: 11 additions & 2 deletions pylint/checkers/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,23 @@ def _check_for_check_kw_in_run(self, node: nodes.Call) -> None:
self.add_message("subprocess-run-check", node=node, confidence=INFERENCE)

def _check_shallow_copy_environ(self, node: nodes.Call) -> None:
arg = utils.get_argument_from_call(node, position=0)
confidence = HIGH
try:
arg = utils.get_argument_from_call(node, position=0, keyword="x")
except utils.NoSuchArgumentError:
arg = utils.infer_kwarg_from_call(node, keyword="x")
if not arg:
return
confidence = INFERENCE
try:
inferred_args = arg.inferred()
except astroid.InferenceError:
return
for inferred in inferred_args:
if inferred.qname() == OS_ENVIRON:
self.add_message("shallow-copy-environ", node=node)
self.add_message(
"shallow-copy-environ", node=node, confidence=confidence
)
break

@utils.only_required_for_messages(
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/s/shallow_copy_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@
import os

copy.deepcopy(os.environ)

# Edge cases
copy.copy() # [no-value-for-parameter]
copy.copy(x=test_dict)
copy.copy(x=os.environ) # [shallow-copy-environ]
copy.copy(**{"x": os.environ}) # [shallow-copy-environ]
copy.copy(**{"y": os.environ}) # [unexpected-keyword-arg]
copy.copy(y=os.environ) # [no-value-for-parameter, unexpected-keyword-arg]
Comment on lines +35 to +37
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the last two lines to raise the same set of messages. We don't use inference to analyze whether **kwargs contains the missing named parameter x. We could.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Captured in #8785

10 changes: 8 additions & 2 deletions tests/functional/s/shallow_copy_environ.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
shallow-copy-environ:7:0:7:21::Using copy.copy(os.environ). Use os.environ.copy() instead.:UNDEFINED
shallow-copy-environ:17:0:17:18::Using copy.copy(os.environ). Use os.environ.copy() instead.:UNDEFINED
shallow-copy-environ:7:0:7:21::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
shallow-copy-environ:17:0:17:18::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
no-value-for-parameter:32:0:32:11::No value for argument 'x' in function call:UNDEFINED
shallow-copy-environ:34:0:34:23::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
shallow-copy-environ:35:0:35:30::Using copy.copy(os.environ). Use os.environ.copy() instead.:INFERENCE
unexpected-keyword-arg:36:0:36:30::Unexpected keyword argument 'y' in function call:UNDEFINED
no-value-for-parameter:37:0:37:23::No value for argument 'x' in function call:UNDEFINED
unexpected-keyword-arg:37:0:37:23::Unexpected keyword argument 'y' in function call:UNDEFINED