Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4869

Python compiler creates classes with mutable default values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 0.12.0
    • None
    • Python - Compiler
    • None

    Description

      Given this IDL:

      struct Basic {
          1: required list<string> strings = []
      }

      the following Python code is generated:

      class Basic(object):
          """
          Attributes:
          - strings
          """
      
      
          def __init__(self, strings=[
          ]):
              if identifiers is self.thrift_spec[1][4]:
                  identifiers = [
                  ]
              self.identifiers = identifiers
      
          <remainder truncated>

      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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            elesel Louie Larson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: