Pewarta Proxy

📋 Server Request & Proxy Logs

Loading active proxy settings...
Developed by Khairul Adha (adhexa.id)
Mengambil log server...

⚙️ Pengaturan Proxy Target

Skip to content

TypeError: switch argument was for hp.choice nodes with numpy >= 1.20 #949

Closed

Description

@jedisomCVNA

Description

Running hyperopt with hp.choice raises a TypeError in pyll/base.py when the sampled index is returned as a numpy.int64 scalar rather than a Python int:

TypeError: ('switch argument was', 0)
  File "hyperopt/pyll/base.py", line 863, in rec_eval
    raise TypeError("switch argument was", switch_i)

Root Cause

In hyperopt/pyll/base.py around line 859, the switch node handler calls .item() on the sampled value only when it is an np.ndarray:

if isinstance(switch_i, np.ndarray):
    switch_i = switch_i.item()
if isinstance(switch_i, int):   # fails for np.int64 bare scalars
    ...
else:
    raise TypeError("switch argument was", switch_i)

Prior to numpy 1.20, numpy.int64 inherited from Python's int, so the isinstance(switch_i, int) check passed silently. As of numpy 1.20, that inheritance was removed (numpy 1.20 release notes), and bare numpy.int64 scalars now fall through to the TypeError.

Reproduction

import numpy as np
print(np.__version__)          # e.g. 1.26.4
print(isinstance(np.int64(0), int))  # False — this is the core issue

from hyperopt import fmin, tpe, hp, STATUS_OK

space = {'layers': hp.choice('layers', [1, 2, 3])}
fmin(fn=lambda p: {'loss': p['layers'], 'status': STATUS_OK},
     space=space, algo=tpe.suggest, max_evals=5)
# Raises: TypeError: ('switch argument was', 0)

Fix

Extend the existing ndarray guard to also handle bare np.integer scalars, which have the same .item() method:

# Before:
                if isinstance(switch_i, np.ndarray):
                    switch_i = switch_i.item()
                if isinstance(switch_i, int):
# After:
                if isinstance(switch_i, np.ndarray):
                    switch_i = switch_i.item()
                if isinstance(switch_i, np.integer):
                    switch_i = int(switch_i)
                if isinstance(switch_i, int):

np.integer is the abstract base class for all numpy integer scalar types (np.int64, np.int32, etc.) and has been stable across numpy versions. This is a one-line change with no behavioral impact for environments where numpy.int64 still behaves as a Python int.

Environment
numpy >= 1.20
hyperopt @ c49ad14 (current master)
Python 3.11

Happy to open a PR with this fix if it looks good to maintainers.

Activity

  1. added a commit that references this issue on Apr 15, 2026
    96ec6ee
  2. added a commit that references this issue on Jul 15, 2026
    9834314
  3. jaberg commented on Jul 24, 2026

    @jaberg
    Contributor

    Hopefully addressed via #959, reply there if there are further issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions