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

Python compiler creates classes with mutable default values

Agile BoardAttach filesAttach ScreenshotAdd voteVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    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

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            elesel Louie Larson

            Dates

              Created:
              Updated:

              Slack

                Issue deployment