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

Dangerous default argument should include "Stateful" default arguments as well #4659

Open
divineunited opened this issue Jul 2, 2021 · 5 comments · May be fixed by #8553
Open

Dangerous default argument should include "Stateful" default arguments as well #4659

divineunited opened this issue Jul 2, 2021 · 5 comments · May be fixed by #8553
Assignees
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@divineunited
Copy link

divineunited commented Jul 2, 2021

Is your feature request related to a problem? Please describe

A clear and concise description of what the problem is.

When a function is defined, a function will maintain the state of the default argument. This can be dangerous if the default argument is something like datetime.date.today(). In this article, it states that:

Python’s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby).

Thus, if a production environment has a pod that evaluates a function once and the pod doesn't rotate for a couple days, it will continue to define that default argument incorrectly.

Describe the solution you'd like

It would be nice if pylint warns us about these dangerous default "stateful" arguments, similar to the warning about dangerous "mutable" default arguments, e.g. http://pylint-messages.wikidot.com/messages:w0102

A clear and concise description of what you want to happen.

Additional context

Add any other context about the feature request here.

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Help wanted 🙏 Outside help would be appreciated, good for new contributors Good first issue Friendly and approachable by new contributors labels Jul 4, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, that would be a really useful enhancement !

@divineunited
Copy link
Author

Cool! - Thanks for adding the tags @Pierre-Sassoulas

@jaehoonhwang
Copy link
Contributor

Mind if i take this one?

@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jun 24, 2022
@clavedeluna
Copy link
Collaborator

Wondering what other test cases we should add to dangerous-default-value in addition to this one:

def give_date(date=datetime.date.today()): # [dangerous-default-value]
    return date

@alblasco
Copy link

alblasco commented Apr 8, 2023

As the feature is quite interesting I am willing to contribute with following PR
#8553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants