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

filter does not work with type guards #29

Open
scarf005 opened this issue Mar 26, 2023 · 1 comment
Open

filter does not work with type guards #29

scarf005 opened this issue Mar 26, 2023 · 1 comment

Comments

@scarf005
Copy link

Summary

python 3.10 introduced type guards similar to that of typescript. however, flupy does not seem to support this feature.

image

from typing import Any, TypedDict, TypeGuard

from flupy import flu


class Person(TypedDict):
    name: str
    age: int


def is_person(val: Any) -> TypeGuard[Person]:
    try:
        return (
            isinstance(val, dict)
            and isinstance(val["name"], str)
            and isinstance(val["age"], int)
        )
    except KeyError:
        return False


def get_age(val: Person):
    return f"Age: {val['age']}"


result = (
    flu([Person(name="Alice", age=20), 3, "afds", {"name": "Eve"}])
    .filter(is_person)
    .map(get_age)
    .collect()
)
@olirice
Copy link
Owner

olirice commented Mar 27, 2023

By the strictest definition, filter should not have any impact on the underlying elements but I think this is a good exception.

def filter(self, func: Callable[..., bool], *args: Any, **kwargs: Any) -> "Fluent[T]"

I'm not current on the latest enhancements to mypy but I'd be happy to accept a PR if you're interested in tinkering with the implementation of filter to support that use case?

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