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

[Backport maintenance/2.17.x] Fix a crash when calling copy.copy() without args #8786

Merged
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
2 changes: 1 addition & 1 deletion doc/user_guide/installation/ide_integration/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Below you can find tutorials for some of the most common ones.
.. _Sublime Text: https://packagecontrol.io/packages/SublimeLinter-pylint
.. _Vim: https://www.vim.org/scripts/script.php?script_id=891
.. _Visual Studio: https://docs.microsoft.com/visualstudio/python/code-pylint
.. _Visual Studio Code: https://code.visualstudio.com/docs/python/linting#_pylint
.. _Visual Studio Code: https://code.visualstudio.com/docs/python/linting
.. _Visual Studio Code Pylint Extension: https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
.. _WingIDE: https://wingware.com/doc/warnings/external-checkers

Expand Down
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]
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