Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling typing.Annotated #36

Open
SimonHeybrock opened this issue Aug 11, 2023 · 2 comments
Open

Handling typing.Annotated #36

SimonHeybrock opened this issue Aug 11, 2023 · 2 comments

Comments

@SimonHeybrock
Copy link
Member

SimonHeybrock commented Aug 11, 2023

Currently using typing.Annotated as parameters or for type hints in providers leads to mostly cryptic exceptions. One could consider supporting it, but it would require using __annotations__ (or inspect.get_annotations in newer Python) instead of typing.get_type_hints.

One could probably get this too work, but I fear it may be more brittle. Therefore, we should carefully consider whether support for Annotated is desirable. If not, we should attempt to raise clearer error messages.

@jl-wynen
Copy link
Member

More details from separate issue:

from typing import NewType, Annotated
import sciline as sl

Int = NewType('Int', int)
Str = NewType('Str', str)
AStr = Annotated[Str, 'metadata']


def foo(x: Int) -> AStr:
    return Str(str(x))


def bar(s: Str, a: AStr) -> list[str]:
    return [s, a]


pl = sl.Pipeline([foo, bar], params={Int: Int(3)})
# Can compute results:
print(pl.compute(Str))  # -> 3
print(pl.compute(list[str]))  # -> ['3', '3']

# But the annotation is lost in the graph:
g = pl.get(Str)
print(list(g._graph))  # -> [__main__.Str, __main__.Int]
g = pl.get(list[str])
print(list(g._graph))  # -> [list[str], __main__.Str, __main__.Int]
print(list(g._graph[list[str]].arg_spec.keys()))  # -> [__main__.Str, __main__.Str]

# Raises
# sciline.handler.UnsatisfiedRequirement: ('No provider found for type', typing.Annotated[__main__.Str, 'metadata'])
pl.compute(AStr)

@jl-wynen
Copy link
Member

One could probably get this too work, but I fear it may be more brittle. Therefore, we should carefully consider whether support for Annotated is desirable. If not, we should attempt to raise clearer error messages.

Disallowing this would mean that annotated types can never be used with Sciline. This may be too limiting.

@jl-wynen jl-wynen mentioned this issue Mar 1, 2024
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

No branches or pull requests

2 participants