Skip to content

Enable None as default and instantiation value for hp.int, hp.float, hp.bool, and hp.select#12

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-c6d60b4a-f8fd-4b92-aa28-482c36df5650
Draft

Enable None as default and instantiation value for hp.int, hp.float, hp.bool, and hp.select#12
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-c6d60b4a-f8fd-4b92-aa28-482c36df5650

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 29, 2025

This PR implements support for using None as both default values and instantiation values for hp.int, hp.float, hp.bool, and hp.select parameter types.

Problem

Previously, users could not define None as a valid value for hyperparameters, which limited flexibility when dealing with optional parameters. The following use cases were not supported:

# Using None as default
hp.int(default=None, min=1, max=10, name="dimensions")

# Passing None during instantiation
def conf(hp: HP):
    hp.int(default=7, max=10, name="dimensions")

instantiate(conf, values={"dimensions": None})

Solution

Core Changes

  1. Updated parameter validators (IntValidator, FloatValidator, BoolValidator) to accept and return Optional types, with early return for None values
  2. Modified bounds validation to skip validation when the value is None, preventing unnecessary validation errors
  3. Enhanced hp.select behavior to properly distinguish between no explicit default and an explicit None default using a sentinel value
  4. Updated type signatures to reflect Optional parameter and return types

Key Features

  • None as default values: hp.int(default=None, name="param") now works
  • None as instantiation values: instantiate(config, values={"param": None}) now works
  • Smart bounds validation: None values bypass min/max validation, but non-None values are still validated
  • Improved hp.select: Distinguishes between hp.select(['a', 'b']) (uses first option) and hp.select(['a', 'b'], default=None) (returns None)
  • Full backward compatibility: Existing code continues to work unchanged

Example Usage

from hypster import HP, instantiate

def config(hp: HP):
    return {
        # None defaults
        'learning_rate': hp.float(default=None, min=0.0, max=1.0, name='learning_rate'),
        'batch_size': hp.int(default=None, min=1, name='batch_size'), 
        'use_dropout': hp.bool(default=None, name='use_dropout'),
        'optimizer': hp.select(['adam', 'sgd'], default=None, name='optimizer'),
        
        # Regular defaults still work
        'epochs': hp.int(default=100, name='epochs')
    }

# All parameters get None values
result1 = instantiate(config)
# {'learning_rate': None, 'batch_size': None, 'use_dropout': None, 'optimizer': None, 'epochs': 100}

# Mix of None and regular values
result2 = instantiate(config, values={
    'learning_rate': 0.001,
    'batch_size': None,  # Explicitly None
    'epochs': 50
})
# {'learning_rate': 0.001, 'batch_size': None, 'use_dropout': None, 'optimizer': None, 'epochs': 50}

This enhancement provides greater flexibility for optional hyperparameters while maintaining all existing functionality and validation behavior.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 29, 2025 14:28
…lect

Co-authored-by: gilad-rubin <6134299+gilad-rubin@users.noreply.github.com>
Co-authored-by: gilad-rubin <6134299+gilad-rubin@users.noreply.github.com>
Copilot AI changed the title [WIP] can you enable a user to define None as the value and/or default for hp.select, hp.int, hp.float, hp.bool? if so - add that option example hp.int(default=None, min=1, max=10, name="dimensions") or def conf(hp: HP): hp.int(default=7, max=10, name... Enable None as default and instantiation value for hp.int, hp.float, hp.bool, and hp.select Aug 29, 2025
Copilot AI requested a review from gilad-rubin August 29, 2025 14:31
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Aug 29, 2025

CodSpeed Performance Report

Merging #12 will not alter performance

Comparing copilot/fix-c6d60b4a-f8fd-4b92-aa28-482c36df5650 (946e9a3) with master (2264902)

Summary

✅ 2 untouched benchmarks

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 13, 2026

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants