Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Atoti 0.9.1: Switch Function Throws Error When Used with Levels #966

@shahin-y

Description

@shahin-y

Hi everyone,

Apparently the internals of the Constant class were changed in Atoti 0.9.1 . When creating a new instance, the validation of it can throw an exception because the ValidationInfo instance does not always provide information on the data_type. This happens when the switch function is called with a Level.

Steps to reproduce

The below example works in Atoti 0.9.0 but throws an error in 0.9.1:

import atoti as tt
import pandas as pd

data = pd.DataFrame(
    columns=["ID", "Origin", "Target", "Qty"],
    data=[
        ("1", "Paris", "London", 240.0),
        ("2", "Nantes", "New York", 270.0),
        ("3", "Frankfurt", "Paris", 200.0),
        ("4", "Nantes", "Nantes",800),
        ("5", "Frankfurt", "Frankfurt",600)
    ],
)

session = tt.Session.start()
table = session.read_pandas(data, table_name="data")

cube = session.create_cube(table)
m, l, h = cube.measures, cube.levels, cube.hierarchies

m["Out Qty"] = tt.switch(l["Origin"], {l["Target"]: 0}, default=tt.agg.single_value(table["Qty"]))  # error

Actual Result

...
File <...>\atoti\function\switch.py:143, in switch(subject, cases, default)
    137     else:
    138         flatten_cases[key] = value
    140 constant_cases: dict[ConstantValue | None, MeasureConvertible] = {
    141     key: value
    142     for key, value in flatten_cases.items()
--> 143     if key is None or is_constant_value(key)
    144 }
...
File <...>\atoti\_constant.py:358, in is_constant_value(value)
    356 def is_constant_value(value: object, /) -> TypeGuard[ConstantValue]:
    357     try:
--> 358         Constant.of(value)  # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
    359     except (TypeError, ValueError):
    360         return False
...
File <...>\atoti\_constant.py:113, in _parse_value(value, validation_info)
    108 def _parse_value(  # noqa: C901, PLR0911, PLR0912
    109     value: ConstantValue,
    110     validation_info: ValidationInfo,
    111     /,
    112 ) -> ConstantValue:
--> 113     data_type = cast(DataType, validation_info.data["data_type"])


KeyError: 'data_type'

Environment

  • atoti: 0.9.1
  • Python: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugunexpected or wrong behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions