-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import hook decoration position x 1.
This commit is the first in a commit chain enabling end users to explicitly configure **import hook decoration positions** (i.e., competing locations to which the `@beartype` decorator will be implicitly injected into existing chains of one or more decorators decorating classes and callables defined by modules imported under `beartype.claw` import hooks, each with concomitant tradeoffs with respect to decorator interoperability and quality assurance), en-route to resolving feature request #374 kindly indirectly submitted by bestest @beartype users @danielward27 and @patrick-kidger courtesy parent feature request #368. Specifically, this commit: * Defines a new public `beartype.BeartypeDecorationPosition` configuration enumeration, currently containing this pair of members: * `BeartypeDecorationPosition.FIRST`, the **first (i.e., bottom-most) decorator position**. Passing this member configures :mod:`beartype.claw` import hooks to inject the :func:`beartype.beartype` decorator as the first (i.e., bottom-most) decorator in relevant decorator chains. Examples below or it didn't happen. * `BeartypeDecorationPosition.LAST`, the **last (i.e., top-most) decorator position**. As the default, this position need *not* be explicitly passed * Defines a pair of new optional parameters with which to instantiate **beartype configurations** (i.e., `beartype.BeartypeConf` objects): * `claw_decoration_position_funcs`, the **import hook callable decorator position** (i.e., location to which the `@beartype` decorator will be implicitly injected into existing chains of one or more decorators decorating functions and methods defined by modules imported under :mod:`beartype.claw` import hooks). Defaults to `BeartypeDecorationPosition.LAST`. Modifying this configures import hooks to inject `@beartype` as the first (rather than last) decorator for callables. If your codebase requires this, consider submitting an issue to the @beartype issue tracker. Ideally, the `@beartype` decorator should be order-invariant with respect to decorator chaining and thus support decoration in *any* position – including the default last position: e.g., ```python # Registering this import hook... from beartype import BeartypeConf, BeartypeDecorationPosition from beartype.claw import beartype_this_package beartype_this_package(conf=BeartypeConf( claw_decoration_position_funcs=( BeartypeDecorationPosition.FIRST))) # ...transforms chains of function decorators like this... from functools import cache @cache def chad_func() -> int: return 42 # ...into chains of function decorators like this. from beartype import beartype from functools import cache @cache @beartype # <-- @beartype decorates first rather than last! \\o/ def chad_func() -> int: return 42 ``` * `claw_decoration_position_types`, the **import hook class decorator position** (i.e., location to which the `@beartype` decorator will be implicitly injected into existing chains of one or more decorators decorating classes defined by modules imported under `beartype.claw` import hooks). Defaults to `BeartypeDecorationPosition.LAST`. Modifying this configures import hooks to inject `@beartype` as the first (rather than last) decorator for classes. As above, if your codebase requires this, consider submitting an issue to the @beartype issue tracker: e.g., ```python # Registering this import hook... from beartype import BeartypeConf, BeartypeDecorationPosition from beartype.claw import beartype_this_package beartype_this_package(conf=BeartypeConf( claw_decoration_position_types=BeartypeDecorationPosition.FIRST)) # ...transforms chains of class decorators like this... from dataclasses import dataclass @DataClass class ClassyData(object): integral_datum: int # ...into chains of class decorators like this. from beartype import beartype from dataclasses import dataclass @DataClass @beartype # <-- @beartype decorates first rather than last! \\o/ class ClassyData(object): integral_datum: int ``` Naturally, not much is tested and even less is likely to work. (*Boomy looms of doomy rooms!*)
- Loading branch information
Showing
13 changed files
with
402 additions
and
124 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
Oops, something went wrong.