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

rethink default values #8

Open
rue-a opened this issue Nov 24, 2022 · 0 comments
Open

rethink default values #8

rue-a opened this issue Nov 24, 2022 · 0 comments
Labels
help wanted Extra attention is needed invalid This doesn't seem right possible problem May result in a problem

Comments

@rue-a
Copy link
Owner

rue-a commented Nov 24, 2022

Currently, many default values are set to None by proxy (e.g., "", [], {}) to be compliant with the given type hint, e.g.

def method(in: str = "", in_2: list = []) -> str:
    <...>
    if <something>:
        return "some string"
    else:
        return ""

instead of:

def method(in: str = None, in_2: list = None) -> str:
    <...>
    if <something>:
        return "some string"
    else:
        return None

According to PyLint some of these default values may be harmful:

dangerous-default-value / W0102

Message emitted:

Dangerous default value %s as argument

Description:

Used when a mutable value as list or dictionary is detected in a default value for an argument.

Problematic code:

def whats\_on\_the\_telly(penguin\=\[\]):  \# \[dangerous-default-value\]
   penguin.append("property of the zoo")
   return penguin

Correct code:

def whats\_on\_the\_telly(penguin\=None):
   if penguin is None:
       penguin \= \[\]
   penguin.append("property of the zoo")
   return penguin

Additional details:

With a mutable default value, with each call the default value is modified, i.e.:

whats\_on\_the\_telly() \# \["property of the zoo"\]
whats\_on\_the\_telly() \# \["property of the zoo", "property of the zoo"\]
whats\_on\_the\_telly() \# \["property of the zoo", "property of the zoo", "property of the zoo"\]
@rue-a rue-a added help wanted Extra attention is needed invalid This doesn't seem right possible problem May result in a problem labels Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right possible problem May result in a problem
Projects
None yet
Development

No branches or pull requests

1 participant