scipp / sciline

Build scientific pipelines for your data
https://scipp.github.io/sciline/
BSD 3-Clause "New" or "Revised" License
10 stars 2 forks source link

Param setting not working with default values #182

Closed YooSunYoung closed 1 month ago

YooSunYoung commented 2 months ago

For essimaging I wanted to make some arguments optional,


import sciline as sl
from typing import NewType

OptionalInt = NewType("OptionalInt", int | None)

def foo(a: OptionalInt) -> str:
    return str(a) if a is not None else "Here is your default message."

wf = sl.Pipeline([foo], params={OptionalInt: OptionalInt(None)})
wf.compute(str)

# 'Here is your default message.'

But if the argument has a default value of None, it fails.


import sciline as sl
from typing import NewType

OptionalInt = NewType("OptionalInt", int | None)

def foo(a: OptionalInt = None) -> str:
    return str(a) if a is not None else "Here is your default message."

wf = sl.Pipeline([foo], params={OptionalInt: OptionalInt(None)})
wf.compute(str)

# sciline.handler.UnsatisfiedRequirement: ('No provider found for type', typing.Optional[__main__.OptionalInt])

Is it a known limitation or a bug...?

SimonHeybrock commented 1 month ago

The error message shows that something odd is going on: Why is it looking for typing.Optional[__main__.OptionalInt] and not __main__.OptionalInt?

jokasimr commented 1 month ago

This is because of how get_type_hints worked prior to python 3.11.

Changed in version 3.11: Previously, Optional[t] was added for function and method annotations if a default value equal to None was set. Now the annotation is returned unchanged.

I've verified that the bug is not present when this is run in 3.11.

jokasimr commented 1 month ago

What's best to do? Should we

Edit: decided to fix it

MridulS commented 1 month ago

do nothing ;)

Just wait it out? https://scientific-python.org/specs/spec-0000/#support-window Python 3.10 could be dropped in a bit :)

jokasimr commented 1 month ago

Just wait it out? https://scientific-python.org/specs/spec-0000/#support-window Python 3.10 could be dropped in a bit :)

3.10 already out? Looking at that schedule makes me feel old. But yeah this could well be the best option

jokasimr commented 1 month ago

The proposed fix was insufficient so it was decided to do nothing instead.