Skip to content

filter does not work with type guards #29

@scarf005

Description

@scarf005

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()
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions