Types for "truthy" and "falsy" values
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
No implementation files or tests are named. Start by reviewing the truthy and falsy examples, then read the linked intersection-types proposals and the typing project's guidance; the work is complete only after the design and expected type-checking behavior are agreed and the relevant typing support is specified.
Written by the indexing model from the issue text.
Description
I found this function in our codebase and I'm trying to type it:
def run_until_truthy(fn, num_tries=5):
for _ in range(num_tries):
if res := fn():
return res
Exception('Giving up.')
The function repeatedly calls a callable without arguments until it returns a "truthy" value. Then it returns that value. Easy enough:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T: ...
This is correct but too restrictive. Usages like this don't work:
def load_something() -> str | None:
pass
# mypy: Incompatible types in assignment (expression has type "str | None", variable has type "str")
something: str = run_until_truthy(load_something)
I can hack it to make it work:
type Falsy = Literal[None, False, 0, "", b""] | tuple[()]
def run_until_truthy[T](fn: Callable[[], T | Falsy], num_tries: int = 5) -> T: ...
Does it make sense to add official Truthy and Falsy types to the standard library?
Maybe a Truthy type would also make sense. Truthy can't be defined as a type alias of existing types, but could probably also be useful sometimes, e.g. to type the following function:
def get_falsy_values[T](seq: list[T | Truthy]) -> list[T]:
return [i for i in seq if not i]
From thinking about it for a few minutes, I think Falsy would be more often useful than Truthy, at least in combination with the current typing features.
If there's an intersection type constructor at some point, the above examples could be written like this instead, which might be easier to understand:
def run_until_truthy[T](fn: Callable[[], T], num_tries: int = 5) -> T & Truthy: ...
def get_falsy_values[T](seq: list[T]) -> list[T & Falsy]:
2024-11-25
- Updated to PEP 695 syntax.
- Using
&instead ofIntersection[]in example using proposed intersection types.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·