-
-
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.
This commit is the first in a commit chain adding support for deprecated **PEP 613 type aliases** (i.e., the `typing.TypeAlias` type hint singleton), en-route to resolving feature request #328 kindly submitted by spaghetti-loving Bay Area pasta guru @jamesbraza (James Braza). Although since deprecated by **PEP 695 type aliases** (e.g., type hints of the form `type {alias_name} = {alias_value}` under Python � 3.12), PEP 613 type aliases are still widely prevalent throughout the open-source community. Specifically, @beartype now: * Emits one non-fatal `BeartypeDecorHintPep613DeprecationWarning` for each PEP 613 type alias resembling: ```python BeartypeDecorHintPep613DeprecationWarning: PEP 613 type hint typing.TypeAlias deprecated by PEP 695. Consider either: * Requiring Python >= 3.12 and refactoring PEP 613 type aliases into PEP 695 type aliases. Note that Python < 3.12 will hate you for this: e.g., # Instead of this... from typing import TypeAlias alias_name: TypeAlias = alias_value # ...just do this. type alias_name = alias_value * Refactoring PEP 613 type aliases into PEP 484 "typing.NewType"-based type aliases. Note that static type-checkers (e.g., mypy, pyright, Pyre) will hate you for this: e.g., # Instead of this... from typing import TypeAlias alias_name: TypeAlias = alias_value # ...just do this. from typing import NewType alias_name = NewType("alias_name", alias_value) Combine the above two approaches via The Ultimate Type Alias (TUTA), a hidden ninja technique that supports all Python versions and static type-checkers but may cause coworker heads to pop off like in that one jolly Kingsman scene: # Instead of this... from typing import TypeAlias alias_name: TypeAlias = alias_value # ..."just" do this. If you think this sucks, know that you are not alone. from typing import TYPE_CHECKING, NewType, TypeAlias # <-- sus af from sys import version_info # <-- code just got real if TYPE_CHECKING: # <-- if static type-checking, then PEP 613 alias_name: TypeAlias = alias_value # <-- grimdark coding style elif version_info >= (3, 12): # <-- if Python >= 3.12, then PEP 695 exec("type alias_name = alias_value") # <-- eldritch abomination else: # <-- if Python < 3.12, then PEP 484 alias_name = NewType("alias_name", alias_value) # <-- coworker gives up here ``` * Otherwise ignores each PEP 613 type alias, which conveys *no* meaningful semantics or metadata. Frankly, it's unclear why PEP 613 even exists. The CPython developer community felt similarly, which is why PEP 695 type aliases deprecate PEP 613. @beartype shrugs noncommittally. (*A hundred red thunders!*)
- Loading branch information
Showing
10 changed files
with
228 additions
and
74 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
Oops, something went wrong.