How should a custom configuration entity define a dependency on other configuration? In a custom module I am defining a defaults Configuration entity that references a role.
I would like to define a dependency on the role. This is my first attempt.
public function calculateDependencies() {
  $this-&gt;dependencies = parent::calculateDependencies();
  $this-&gt;addDependency('module', 'user');
  if (isset($this-&gt;role_id)) {
    $this-&gt;addDependency('config', &quot;user.role.{$this-&gt;role_id}&quot;);
  }
  return $this; // Also tried return $this-&gt;dependencies;
}

The Drupal documentation on dependencies references the Block configuration class that looks very similar:
  public function calculateDependencies() {
    parent::calculateDependencies();
    $this-&gt;addDependency('theme', $this-&gt;theme);
    return $this;
  }

However I am seeing an error shown below when trying to save the entity.The backtrace indicates it is coming from the attempt to add a dependency on the user module. I chose to add that dependency by taking an example from system.action.user_add_role_action.administrator.

PHP Error:  Cannot use object of type Drupal/my_module/Entity/MyAccessDefaults as array in /app/web/core/lib/Drupal/Core/Entity/DependencyTrait.php on line 32

For example I am trying to test with this code:
$values = [
  'id' =&gt; 'administrator', 'label' =&gt; 'Administrator role grants', 
  'grant_type' =&gt; 'role', 'role_id' =&gt; &quot;administrator&quot;, 
  'grants' =&gt; ['grant_view' =&gt; 1]
];
$default_admin_config = \Drupal::entityTypeManager()-&gt;getStorage('my_entity')-&gt;create($values);
$default_admin_config-&gt;save();
ConfigEntityInterface::calculateDependencies returns $this, so this line:
$this-&gt;dependencies = parent::calculateDependencies();

Is causing problems (replacing the dependencies array with the instance of the current class, which is presumably a MyAccessDefaults).
Looking at the code, and other core classes which implement ConfigEntityInterface, your code should be:
public function calculateDependencies() {
  parent::calculateDependencies();
  $this-&gt;addDependency('module', 'user');
  if (isset($this-&gt;role_id)) {
    $this-&gt;addDependency('config', &quot;user.role.{$this-&gt;role_id}&quot;);
  }
  return $this;
}