puppetlabs/puppet · error · Puppet::Pops::Types::TypeAssertionError

#{subject} #{what},

Error message

#{subject} #{what},

What it means

TypeAsserter.report_type_mismatch (lib/puppet/pops/types/type_asserter.rb:43) builds the prefix "#{subject} #{what}," (default what = 'has wrong type') and passes it to TypeMismatchDescriber.describe_mismatch, which appends a detailed expected-vs-actual description (including nested structure mismatches), then raises TypeAssertionError carrying the expected and actual types. It is fired by TypeAsserter.assert_instance_of — the standard Ruby-API gate used by Puppet functions and providers to enforce parameter types — whenever a passed value fails expected_type.instance?(value).

Source

Thrown at lib/puppet/pops/types/type_asserter.rb:43

  # @param subject [String,Array] String to be prepended to the exception message or Array where the first element is
  #                               a format string and the rest are arguments to that format string
  # @param expected_type [PAnyType] Expected type for the value
  # @param value [Object] Value to check
  # @param nil_ok [Boolean] Can be true to allow nil value. Optional and defaults to false
  # @return The value argument
  #
  # @api public
  def self.assert_instance_of(subject, expected_type, value, nil_ok = false, &block)
    unless value.nil? && nil_ok
      report_type_mismatch(subject, expected_type, TypeCalculator.singleton.infer_set(value), &block) unless expected_type.instance?(value)
    end
    value
  end

  def self.report_type_mismatch(subject, expected_type, actual_type, what = 'has wrong type')
    subject = yield(subject) if block_given?
    subject = subject[0] % subject[1..] if subject.is_a?(Array)
    raise TypeAssertionError.new(
      TypeMismatchDescriber.singleton.describe_mismatch("#{subject} #{what},", expected_type, actual_type), expected_type, actual_type
    )
  end
  private_class_method :report_type_mismatch
end
end

View on GitHub (pinned to e227c27540)

Solutions

  1. Pass a value matching the asserted type — coerce first: '42'.to_i, or use the type's cast helpers
  2. If nil is legitimate, pass the nil_ok = true flag: assert_instance_of(subject, type, value, true)
  3. Use a block to customize the subject: assert_instance_of(['parameter %s', name], type, value)
  4. For optional parameters, branch on nil before asserting

Example fix

# before
Puppet::Pops::Types::TypeAsserter.assert_instance_of('The value', Puppet::Pops::Types::PIntegerType::DEFAULT, '42')
# after
Puppet::Pops::Types::TypeAsserter.assert_instance_of('The value', Puppet::Pops::Types::PIntegerType::DEFAULT, 42)
Defensive patterns

Strategy: type-guard

Type guard

# Ruby: narrow the value before asserting, mirroring the asserter's own check
PINT = Puppet::Pops::Types::PIntegerType::DEFAULT
def int_or_nil!(value, subject)
  return nil if value.nil?                       # decide nil policy yourself
  return value if PINT.instance?(value)          # same predicate the asserter uses
  raise TypeError, "#{subject} expected Integer, got #{value.class}"
end

Try / catch

begin
  Puppet::Pops::Types::TypeAsserter.assert_instance_of(subject, expected_type, value)
rescue Puppet::Pops::Types::TypeAssertionError => e
  # e.expected_type / e.actual_type are exposed for structured handling
  raise ArgumentError, "bad input from caller: #{e.message}"
end

Prevention

When it happens

Trigger: Calling Puppet::Pops::Types::TypeAsserter.assert_instance_of('The parameter', PIntegerType::DEFAULT, '42') — a String where Integer is required; dispatchers of Puppet::Functions::Function with typed parameters receiving mismatched arguments; assert_keywords_or_instance style calls in the Ruby API receiving wrong-typed keyword args.

Common situations: Writing custom Ruby functions for Puppet and forgetting that DSL scalars arrive as Ruby types with Puppet semantics (e.g. default/undef); API callers passing strings from JSON where the signature declares Integer/Array; version upgrades that tightened signatures with assert_instance_of where previously untyped args were accepted.

Related errors


AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21). Data as JSON: /api/errors/5565548457735f74. Report an issue: GitHub.