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

PR: Remove mutable defaults for keyword arguments #23293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

gb119
Copy link

@gb119 gb119 commented Dec 15, 2024

Description of Changes

  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)
  • Included a screenshot or animation (if affecting the UI, see Licecap)

Whilst tracking down an odd race condition in locating conda, I spotted several instances where a mutable type
was being used as a default for a keyword argument. This PR simple replaces the mutable constant (typically [] and {})
with a None and then does an x = [] if x is None else x or equivalent on the first line of each affected function.

It is possible that functionality was depending on the default mutable value being changed by a function call, but
I'd argue that it is sufficiently non-obvious that doing that should be considered a bug :-)

Issue(s) Resolved

Partially Fixes #23061

Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the Developer Certificate of Origin
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: gb119 (https://github.com/gb119)

@pep8speaks
Copy link

pep8speaks commented Dec 15, 2024

Hello @gb119! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2024-12-16 18:57:18 UTC

@ccordoba12 ccordoba12 changed the title Remove mutable defaults for keyword arguments. Partially addresses #23061 PR: Remove mutable defaults for keyword arguments Dec 16, 2024
Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this @gb119!

external-deps/qtconsole/qtconsole/ansi_code_processor.py Outdated Show resolved Hide resolved
Comment on lines +19 to 20
providers = [] if providers is None else providers
super().__init__(plugin, parent)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
providers = [] if providers is None else providers
super().__init__(plugin, parent)
super().__init__(plugin, parent)
providers = [] if providers is None else providers

Our custom is not add code before calling super unless it's really necessary.

max_line_count=300, exitfunc=None, profile=False,
multithreaded=True):
commands = [] if commands is None else commands
Copy link
Member

Choose a reason for hiding this comment

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

Please move this to be at line 171 below.

Comment on lines +310 to +312
file_associations = (
{} if file_associations is None else file_associations
)
Copy link
Member

Choose a reason for hiding this comment

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

Please move these to be at line 321 below.

Comment on lines +48 to +50
external_path_history = (
[] if external_path_history is None else external_path_history
)
Copy link
Member

Choose a reason for hiding this comment

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

Please move these to be at line 77 below.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, this can just be removed as there is already a test for an empty list that works equally well for None

Comment on lines +271 to 272
data = [] if data is None else data
QAbstractTableModel.__init__(self, parent)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data = [] if data is None else data
QAbstractTableModel.__init__(self, parent)
QAbstractTableModel.__init__(self, parent)
data = [] if data is None else data

"""
Run Python script in a separate process
package=None -> module is in sys.path (standard library modules)
"""
args = [] if args is None else args
p_args = [] if p_args is None else p_args
assert module is not None
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert module is not None
assert module is not None

For code clarity.

"""Create action to run a GUI based Python script"""
args = [] if args else args
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
args = [] if args else args
args = [] if args is None else args

It seems missing.

@@ -116,7 +116,8 @@ def __init__(self, parent, handle_links=True, class_parent=None):
self.setPage(web_page)
self.source_text = ''

def setup(self, options={}):
def setup(self, options=None):
options = {} if options is None else options
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
options = {} if options is None else options
options = {} if options is None else options

For clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Internal Error in util/conda.py if conda channel not found
3 participants