-
Notifications
You must be signed in to change notification settings - Fork 15
filter does not work with type guards #29
Copy link
Copy link
Open
Description
Summary
python 3.10 introduced type guards similar to that of typescript. however, flupy does not seem to support this feature.
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()
)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
