Assertion and checking functions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Description
I'll just leave this here for potential future consideration as it came up on gitter's typing-dev channel. Typescript has something called assertion functions, which enables the type checker to understand that a function does not return if a certain condition is false. I think something similar, but more generalized, would be interesting, especially for unit test functions like assert_is_instance(), assert_true() etc.
def assert_positive(x: object) -> AssertsInstance["x", int]:
assert isinstance(x, int) and x > 0
Related, checking functions could work like this:
def is_positive(x: object) -> ChecksInstance["x", int]:
return isinstance(x, int) and x > 0
def my_func(x: Union[int, str]) -> None:
if is_positive(x):
# x must be an int
...
else:
# x can be an int or str
...
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.
Research direction
Start with the linked TypeScript 3.7 assertion-functions documentation and the issue's assert_positive and is_positive examples. Since no repository files or tests are named, first define how generalized assertion and checking annotations should express type narrowing, then document the agreed behavior and required test coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100