dbrattli / Expression

Pragmatic functional programming for Python inspired by F#
https://expression.readthedocs.io
MIT License
424 stars 31 forks source link

Nothing_.__iter__ method not typed correctly? #51

Closed ssjw closed 2 years ago

ssjw commented 2 years ago

The example from the tutorial does not type check with Pylance:

from expression import effect, Some, Nothing

@effect.option
def fn():
    x = yield from Nothing # or a function returning Nothing

    # -- The rest of the function will never be executed --
    y = yield from Some(43)

    return x + y

xs = fn()
assert xs is Nothing

Type checking fails at x = yield from Nothing -- Pylance thinks it is Unknown type. However, if I change the return type of Nothing_.__iter__ to be like the return type from Some:

    def __iter__(self) -> Generator[TSource, TSource, TSource]:

Then x = yield from Nothing type checks to an Any. Which, given the type of Nothing seems correct. Though of course nothing will yield from Nothing because of the raise Nothing in __iter__.

However, I don't know if changing the return type to Generator has negative consequences in other contexts.

dbrattli commented 2 years ago

@ssjw This is a great suggestion! Will fix.