Skip to content

traitlets.Set should support generic type annotations for type safety #927

Open
@dlqqq

Description

@dlqqq

Problem

traitlets.Set currently lacks generic type support, making it impossible to specify the type of elements it contains for proper type checking.

Current Situation

from traitlets import Set
import traitlets

class MyClass(traitlets.HasTraits):
    # This works but provides no type information
    my_set = Set()
    
    # This doesn't work - no generic support
    # my_set: Set[str] = Set()  # Type error

Desired Behavior

traitlets.Set should support generic type annotations similar to how Python's built-in collections work:

from traitlets import Set
import traitlets

class MyClass(traitlets.HasTraits):
    # Should be possible for type safety
    room_ids: Set[str] = Set()
    numbers: Set[int] = Set(default_value={1, 2, 3})

Reference

The Set class is implemented in traitlets/traitlets.py as a container trait.

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