Skip to content

Commit 92485a3

Browse files
Fix a crash when calling copy.copy() without args (#8784)
1 parent 507dfc5 commit 92485a3

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

doc/user_guide/installation/ide_integration/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Below you can find tutorials for some of the most common ones.
4040
.. _Sublime Text: https://packagecontrol.io/packages/SublimeLinter-pylint
4141
.. _Vim: https://www.vim.org/scripts/script.php?script_id=891
4242
.. _Visual Studio: https://docs.microsoft.com/visualstudio/python/code-pylint
43-
.. _Visual Studio Code: https://code.visualstudio.com/docs/python/linting#_pylint
43+
.. _Visual Studio Code: https://code.visualstudio.com/docs/python/linting
4444
.. _Visual Studio Code Pylint Extension: https://marketplace.visualstudio.com/items?itemName=ms-python.pylint
4545
.. _WingIDE: https://wingware.com/doc/warnings/external-checkers
4646

doc/whatsnew/fragments/8774.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash when calling ``copy.copy()`` without arguments.
2+
3+
Closes #8774

pylint/checkers/stdlib.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,23 @@ def _check_for_check_kw_in_run(self, node: nodes.Call) -> None:
511511
self.add_message("subprocess-run-check", node=node, confidence=INFERENCE)
512512

513513
def _check_shallow_copy_environ(self, node: nodes.Call) -> None:
514-
arg = utils.get_argument_from_call(node, position=0)
514+
confidence = HIGH
515+
try:
516+
arg = utils.get_argument_from_call(node, position=0, keyword="x")
517+
except utils.NoSuchArgumentError:
518+
arg = utils.infer_kwarg_from_call(node, keyword="x")
519+
if not arg:
520+
return
521+
confidence = INFERENCE
515522
try:
516523
inferred_args = arg.inferred()
517524
except astroid.InferenceError:
518525
return
519526
for inferred in inferred_args:
520527
if inferred.qname() == OS_ENVIRON:
521-
self.add_message("shallow-copy-environ", node=node)
528+
self.add_message(
529+
"shallow-copy-environ", node=node, confidence=confidence
530+
)
522531
break
523532

524533
@utils.only_required_for_messages(

tests/functional/s/shallow_copy_environ.py

+8
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@
2727
import os
2828

2929
copy.deepcopy(os.environ)
30+
31+
# Edge cases
32+
copy.copy() # [no-value-for-parameter]
33+
copy.copy(x=test_dict)
34+
copy.copy(x=os.environ) # [shallow-copy-environ]
35+
copy.copy(**{"x": os.environ}) # [shallow-copy-environ]
36+
copy.copy(**{"y": os.environ}) # [unexpected-keyword-arg]
37+
copy.copy(y=os.environ) # [no-value-for-parameter, unexpected-keyword-arg]
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
shallow-copy-environ:7:0:7:21::Using copy.copy(os.environ). Use os.environ.copy() instead.:UNDEFINED
2-
shallow-copy-environ:17:0:17:18::Using copy.copy(os.environ). Use os.environ.copy() instead.:UNDEFINED
1+
shallow-copy-environ:7:0:7:21::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
2+
shallow-copy-environ:17:0:17:18::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
3+
no-value-for-parameter:32:0:32:11::No value for argument 'x' in function call:UNDEFINED
4+
shallow-copy-environ:34:0:34:23::Using copy.copy(os.environ). Use os.environ.copy() instead.:HIGH
5+
shallow-copy-environ:35:0:35:30::Using copy.copy(os.environ). Use os.environ.copy() instead.:INFERENCE
6+
unexpected-keyword-arg:36:0:36:30::Unexpected keyword argument 'y' in function call:UNDEFINED
7+
no-value-for-parameter:37:0:37:23::No value for argument 'x' in function call:UNDEFINED
8+
unexpected-keyword-arg:37:0:37:23::Unexpected keyword argument 'y' in function call:UNDEFINED

0 commit comments

Comments
 (0)