Skip to content

Draft: Typed params

Valentin Valls requested to merge typed-params into main

This PR is a proposal to replace FScanParamBase by a BLISS implementation with default python object types.

Enum should replace FScanModeBase.

Types should replace most (all?) validator we use.

  • Typed valid mypy implementation based on dataclasses
  • Provide a Struct settings
  • Allow to inherit from other classes (to create kind of composite structs -> that's the way we use it in bliss-tomo)
  • Supports int/float/str/bool, with runtime validation
  • Supports Enum or a string as the Enum name, with runtime validation
  • Supports derivative props as @property function
  • Optionally supports types containing validator class method (for custom use cases)
  • NO_SETTINGS is that useful? or what is the use case of this feature? Can be replaced by a Field flag.

Example

import enum
from tomo.helpers.typed_setting import settingclass

class EnumTest(enum.Enum):
    FOO = 0
    BAR = 1
    BAZ = 2


@settingclass
class Foo:
    foo1: str = "foo"
    foo2: int = 10
    foo3: float = 10.01
    foo4: bool = True
    foo5: EnumTest = EnumTest.FOO


def test_settingsclass(beacon, foo: str = "a"):
    my_foo = Foo("my_foo")
    print(my_foo)
    assert my_foo.foo1 == "foo"
    my_foo.foo1 = "fo"
    assert my_foo.foo1 == "fo"
    my_foo.foo1 = 1               <-- mypy error as it should be a string
    my_foo.foo2 = 1
    my_foo.foo3 = 1
    my_foo.foo4 = False
    my_foo.foo5 = EnumTest.BAR
    my_foo.foo5 = "BAR"           <-- We know the enum, we can do the conversion
                                  <-- And we could try to play with ptpython completion
Edited by Valentin Valls

Merge request reports