{"record":{"id":"85b8ded91ffac21a","repo":"puppetlabs/puppet","slug":"odd-number-of-arguments-for-hash","errorCode":null,"errorMessage":"odd number of arguments for Hash","messagePattern":"odd number of arguments for Hash","errorType":"exception","errorClass":"TypeConversionError","httpStatus":null,"severity":"error","filePath":"lib/puppet/pops/types/types.rb","lineNumber":2849,"sourceCode":"            else\n              result.merge!(value)\n            end\n          else\n            r = path[0..-2].reduce(result) { |memo, idx| (memo.is_a?(Array) || memo.has_key?(idx)) ? memo[idx] : memo[idx] = {} }\n            r[path[-1]] = (all_hashes ? PHashType.array_as_hash(value) : value)\n          end\n        end\n        result\n      end\n\n      def from_array(from)\n        case from\n        when Array\n          if from.size == 0\n            {}\n          else\n            unless from.size.even?\n              raise TypeConversionError, _('odd number of arguments for Hash')\n            end\n\n            Hash[*from]\n          end\n        when Hash\n          from\n        else\n          if PIterableType::DEFAULT.instance?(from)\n            Hash[*Iterable.on(from).to_a]\n          else\n            t = TypeCalculator.singleton.infer(from).generalize\n            raise TypeConversionError, _(\"Value of type %{type} cannot be converted to Hash\") % { type: t }\n          end\n        end\n      end\n    end\n  end\n","sourceCodeStart":2831,"sourceCodeEnd":2867,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/pops/types/types.rb#L2831-L2867","documentation":"The new() function on Puppet's Hash type can build a hash from a flat array of alternating keys and values (implemented with Hash[*from]). Ruby's Hash[] semantics require an even number of elements, so an odd-length array raises TypeConversionError with Ruby's classic 'odd number of arguments for Hash' wording. An empty array yields {} and a Hash passes through unchanged; only the flat array/iterable path with an odd count fails.","triggerScenarios":"Hash.new(['a', 1, 'b']) in a manifest; Hash.new($flat) where $flat came from split() and has an odd element count; converting any iterable whose element count is odd via Hash.new.","commonSituations":"Parsing key/value tokens out of a config line where a trailing element or malformed input makes the count odd; module parameters accepting flat pair lists from Hiera; glue code that flattens pairs from another data source and loses one element.","solutions":["Fix the source data so every key has a value.","Check the count before converting and fail with a message that shows the array: if size($flat) % 2 != 0 { fail(...) }.","Truncate deliberately when a trailing element is known garbage: $flat = $flat[0, (size($flat) / 2) * 2].","Build the hash from real 2-element pairs instead of a flat list, e.g. reduce over each_slice(2)."],"exampleFix":"# before (Puppet DSL)\n$flat = split($content, ' ')   # may end up odd after bad input\n$h = Hash.new($flat)           # TypeConversionError when odd\n\n# after\nif size($flat) % 2 != 0 {\n  fail(\"expected key/value pairs, got odd array: ${flat}\")\n}\n$h = Hash.new($flat)","handlingStrategy":"validation","validationCode":"# Ruby\nraise ArgumentError, 'odd pair array' unless flat.size.even?\n\n# Puppet DSL\nif size($flat) % 2 != 0 { fail('odd number of elements; expected key/value pairs') }","typeGuard":"def even_pair_array?(a)\n  a.is_a?(Array) && a.size.even?\nend","tryCatchPattern":"begin\n  h = Hash.new($flat)\nrescue Puppet::Error\n  # repair by dropping the dangling element, then retry\n  h = Hash.new($flat[0, (size($flat) / 2) * 2])\nend","preventionTips":["Validate size % 2 == 0 before any flat-array-to-hash conversion.","Prefer arrays of 2-element pairs (or real hashes) in module interfaces instead of flat key/value lists.","Log the offending array when validation fails so bad input is visible."],"tags":["puppet","hash","type-conversion","puppet-dsl"],"backgroundTag":"key-value-count-mismatch","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}