Non-empty container types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Feature
It would be very helpful to have a way of specifying that a container type must be non-empty.
Pitch
As an example of why this feature would be helpful, consider the following function, average:
from typing import List
def average(numbers: List[float]) -> float:
"""Return the average of a non-empty list of numbers."""
return sum(numbers) / len(numbers)
Rather than relying on exceptions for handling the case when the number of numbers is zero, is seems like something along the lines of the following would be more convenient and more explicit:
from typing import List, NonEmpty
def average(numbers: NonEmpty[List[float]]) -> float:
"""Return the average of a non-empty list of numbers."""
return sum(numbers) / len(numbers)
I don't know anything about the way mypy is implemented so I'm not sure if this is a possible.
Contributor guide
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.
Research direction
The issue proposes a NonEmpty type for Python container annotations, illustrated with NonEmpty[List[float]] and the average function. Start by reviewing mypy's existing type-system and typing-construct support; the issue names no files or tests. Done would require an agreed design and type checking that preserves the non-empty container invariant.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100