-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
beartype.door.is_bearable()
+ PEP 647.
This commit resuscitates PEP 647-compliant `typing.TypeGuard[T]`-based type narrowing on the statement-level runtime type-checker `beartype.door.is_bearable()`, resolving feature request #255 kindly submitted by Florida Polytechnic polymath @alexander-c-b (Alexander C. Bodoh) back when @leycec still had hair. Specifically, this commit: * Restores @beartype support for PEP 647, entirely thanks to a [mammoth dissertation-length dissection](#255 (comment)) of the intersection (*oh gods, what does any of this mean anymore) of runtime and static type-checking vis-a-vis the procedural statement-level hybrid runtime-static type-checker `beartype.door.is_bearable()`, the PEP 484-compliant `@typing.overload` decorator, *and* the PEP 647-compliant `typing.TypeGuard[T]` type hint – all courtesy Python's unassailable `typing` genius @asford (Alex Ford). A prefatory comment in the @beartype codebase now glibly reads: ```python # Note that this PEP 484- and 647-compliant API is entirely the brain child of # @asford (Alex Ford). If this breaks, redirect all ~~vengeance~~ enquiries to: # https://github.com/asford ``` * Restores our previously disabled FAQ entry documenting @beartype support for PEP 647. Sadly: * This does *not* extend to the comparable object-oriented `beartype.door.TypeHint.is_bearable()` method – which continues to *not* perform type narrowing. Due to deficiencies in @leycec's wobbly brain, only the procedural `beartype.door.is_bearable()` function currently performs type narrowing. * This may *not* extend to pyright. Although mypy appears to fully support this API, pyright appears to raise fatal errors that make *no* sense and suggest pyright to have an equally wobbly brain: ``` /home/leycec/py/beartype/beartype/door/_doorcheck.py /home/leycec/py/beartype/beartype/door/_doorcheck.py:209:5 - error: Overloaded implementation is not consistent with signature of overload 1 Function return type "TypeGuard[T@is_bearable]" is incompatible with type "bool" "TypeGuard[T@is_bearable]" is incompatible with "bool" (reportInconsistentOverload) /home/leycec/py/beartype/beartype/door/_doorcheck.py:209:5 - error: Overloaded implementation is not consistent with signature of overload 2 Function return type "TypeGuard[T@is_bearable]" is incompatible with type "bool" "TypeGuard[T@is_bearable]" is incompatible with "bool" (reportInconsistentOverload) 2 errors, 0 warnings, 0 informations ``` What's "funny" about that is that: * *All* `TypeGuard[...]` type hints (including `TypeGuard[T@is_bearable]`, whatever that even is) reduce to and are thus compatible with the standard `bool` type. * PEP 647 claims that the reference implementation of PEP 647 is (*...waitforit*) pyright: The Pyright type checker supports the behavior described in this PEP. Guess it doesn't, huh? We cry wet crocodile tears for OO and pyright. (*Wet wetlands band pet band-aids!*)
- Loading branch information
Showing
19 changed files
with
276 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.