-
Couldn't load subscription status.
- Fork 0
Open
Labels
help wantedExtra attention is neededExtra attention is neededinvalidThis doesn't seem rightThis doesn't seem rightpossible problemMay result in a problemMay result in a problem
Description
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 NoneAccording 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 penguinCorrect code:
def whats\_on\_the\_telly(penguin\=None): if penguin is None: penguin \= \[\] penguin.append("property of the zoo") return penguinAdditional 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"\]
Metadata
Metadata
Assignees
Labels
help wantedExtra attention is neededExtra attention is neededinvalidThis doesn't seem rightThis doesn't seem rightpossible problemMay result in a problemMay result in a problem