pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.25k stars 1.12k forks source link

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

Open divineunited opened 3 years ago

divineunited commented 3 years ago

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 commented 3 years ago

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

divineunited commented 3 years ago

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

jaehoonhwang commented 2 years ago

Mind if i take this one?

clavedeluna commented 1 year ago

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 commented 1 year ago

As the feature is quite interesting I am willing to contribute with following PR https://github.com/pylint-dev/pylint/pull/8553