-
-
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.
PEP 563 + PEP 604 + Python <= 3.9 = exception.
This commit improves the `@beartype` decorator to raise a human-readable `beartype.roar.BeartypeDecorHintPep604Exception` on detecting a discrepancy between PEP 563 (i.e., `from __future__ import annotations`) and PEP 604 (i.e., new-style unions like `int | str`) under Python <= 3.9, which supports PEP 563 but fails to support PEP 604, kinda-but-not-really resolving unlucky issue #300 kindly submitted by the explosive supernova that suspiciously resembles the Eye of Sauron @tvdboom (Mavs). For example, `@beartype` now raises exceptions resembling the following when detecting attempts to enable both PEP 563 *and* PEP 604 under Python <= 3.19: ```bash $ python3.9 >>> from __future__ import annotations >>> from beartype import beartype >>> @beartype ... def ohgods(OHGODS: str | bytes) -> str | bytes: ... return OHGODS beartype.roar.BeartypeDecorHintPep604Exception: Function __main__.ohgods() parameter "OHGODS" stringified PEP 604 type hint 'str | bytes' syntactically invalid under Python < 3.10 (i.e., TypeError("unsupported operand type(s) for |: 'type' and 'type'")). Consider either: * Requiring Python >= 3.10. Abandon Python < 3.10 all ye who code here. * Refactoring PEP 604 type hints into equivalent PEP 484 type hints: e.g., # Instead of this... from __future__ import annotations def bad_func() -> int | str: ... # Do this. Ugly, yet it works. Worky >>>> pretty. from typing import Union def bad_func() -> Union[int, str]: ... ``` (*Lumbering plumbing of bars in lower lumbars!*)
- Loading branch information
Showing
8 changed files
with
179 additions
and
34 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
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