puppetlabs/puppet · error · ArgumentError

Cannot use an unspecific Class[] Type

Error message

Cannot use an unspecific Class[] Type

What it means

Raised by Scope#transform_and_assert_classnames in the PClassType branch when a Class[] type reference carries no class_name. `Class` by itself (a PClassType with class_name == nil) matches any class as a type, but it is not a concrete class that can be included, contained, or realized, so normalization refuses it.

Source

Thrown at lib/puppet/parser/scope.rb:1096

  # @return [Array<String>] names after transformation
  #
  def transform_and_assert_classnames(names)
    names.map do |name|
      case name
      when NilClass
        raise ArgumentError, _("Cannot use undef as a class name")
      when String
        raise ArgumentError, _("Cannot use empty string as a class name") if name.empty?

        name.sub(/^([^:]{1,2})/, '::\1')

      when Puppet::Resource
        assert_class_and_title(name.type, name.title)
        name.title.sub(/^([^:]{1,2})/, '::\1')

      when Puppet::Pops::Types::PClassType
        # TRANSLATORS "Class" and "Type" are Puppet keywords and should not be translated
        raise ArgumentError, _("Cannot use an unspecific Class[] Type") unless name.class_name

        name.class_name.sub(/^([^:]{1,2})/, '::\1')

      when Puppet::Pops::Types::PResourceType
        assert_class_and_title(name.type_name, name.title)
        name.title.sub(/^([^:]{1,2})/, '::\1')
      end.downcase
    end
  end

  # Calls a 3.x or 4.x function by name with arguments given in an array using the 4.x calling convention
  # and returns the result.
  # Note that it is the caller's responsibility to rescue the given ArgumentError and provide location information
  # to aid the user find the problem. The problem is otherwise reported against the source location that
  # invoked the function that ultimately called this method.
  #
  # @return [Object] the result of the called function
  # @raise ArgumentError if the function does not exist

View on GitHub (pinned to e227c27540)

Solutions

  1. Name the class: `include Class['apache']` or simply `include apache`.
  2. If the intent was a type check, keep Class on the right side of a match: `assert_type(Class, $ref)` — do not pass it to include.
  3. In generic Ruby code, verify `klass.class_name` is non-nil before forwarding a PClassType to include/contain.

Example fix

# before
include Class          # Cannot use an unspecific Class[] Type

# after
include Class['apache'] # or: include apache
Defensive patterns

Strategy: validation

Validate before calling

# DSL: always include a concrete name
include Class['apache']
# Ruby: check specificity before forwarding a type object
raise ArgumentError, 'unspecific Class[]' unless pclass_type.class_name

Type guard

p.is_a?(Puppet::Pops::Types::PClassType) && !p.class_name.nil?

Prevention

When it happens

Trigger: `include Class` (bare, no title), `contain Class`, `Class <| tag == 'x' |>`-adjacent API misuse, or `include Class[$var]` where the interpolation degenerates to an unspecific Class type. Also direct calls like scope.transform_and_assert_classnames([Puppet::Pops::Types::TypeParser.parse('Class')]).

Common situations: Code intended to reference 'the Class metatype' in a type annotation accidentally passed to include; refactoring `Class['name']` references where the ['name'] part was dropped; generic code iterating over type objects and forwarding them to include.

Related errors


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