Louie Larson created THRIFT-4869:
------------------------------------
Summary: Python compiler creates classes with mutable default
values
Key: THRIFT-4869
URL: https://issues.apache.org/jira/browse/THRIFT-4869
Project: Thrift
Issue Type: Bug
Components: Python - Compiler
Affects Versions: 0.12.0
Reporter: Louie Larson
Given this IDL:
{code:java}
struct Basic {
1: required list<string> strings = []
}{code}
the following Python code is generated:
{code:python}
class Basic(object):
"""
Attributes:
- strings
"""
def __init__(self, strings=[
]):
if identifiers is self.thrift_spec[1][4]:
identifiers = [
]
self.identifiers = identifiers
<remainder truncated>{code}
If Basic's constructor is called without specifying a value for {{strings}},
such as inĀ {{b = Basic()}}, then every {{Basic}} object created this way will
share the same list that's created for the default value. Any attempts to
append to the list would yield unexpected results. See
[https://docs.quantifiedcode.com/python-anti-patterns/correctness/mutable_default_value_as_argument.html].
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)