-
-
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.
Import hook decoration position x 3.
This commit is the last 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), resolving feature request #374 kindly indirectly submitted by bestest @beartype users @danielward27 and @patrick-kidger courtesy parent feature request #368. Specifically, this commit exhaustively tests this functionality as shockingly working. Would @leycec lie!? (*Win some, winsome yards of vineyards!*)
- Loading branch information
Showing
11 changed files
with
274 additions
and
45 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
43 changes: 43 additions & 0 deletions
43
beartype_test/a00_unit/data/claw/intraprocess/hookable_package/conf/__init__.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
**Beartype import hook configuration subpackage initialization submodule** | ||
(i.e., data module mimicking real-world usage of various :func:`beartype.claw` | ||
import hooks configured by different beartype configurations). | ||
''' | ||
|
||
# ....................{ IMPORTS }.................... | ||
from beartype import ( | ||
BeartypeConf, | ||
BeartypeDecorationPosition, | ||
) | ||
from beartype.claw import beartype_package | ||
|
||
# Subject this single module to a beartype import hook configured to inject the | ||
# @beartype decorator first before (i.e., below) all other class decorators. | ||
beartype_package( | ||
package_name=( | ||
'beartype_test.a00_unit.data.claw.intraprocess.hookable_package.conf.' | ||
'data_claw_conf_decoration_position_funcs_first' | ||
), | ||
conf=BeartypeConf( | ||
claw_decoration_position_funcs=BeartypeDecorationPosition.FIRST), | ||
) | ||
from beartype_test.a00_unit.data.claw.intraprocess.hookable_package.conf import ( | ||
data_claw_conf_decoration_position_funcs_first) | ||
|
||
# Subject this single module to a beartype import hook configured to inject the | ||
# @beartype decorator first before (i.e., below) all other class decorators. | ||
beartype_package( | ||
package_name=( | ||
'beartype_test.a00_unit.data.claw.intraprocess.hookable_package.conf.' | ||
'data_claw_conf_decoration_position_types_first' | ||
), | ||
conf=BeartypeConf( | ||
claw_decoration_position_types=BeartypeDecorationPosition.FIRST), | ||
) | ||
from beartype_test.a00_unit.data.claw.intraprocess.hookable_package.conf import ( | ||
data_claw_conf_decoration_position_types_first) |
70 changes: 70 additions & 0 deletions
70
...claw/intraprocess/hookable_package/conf/data_claw_conf_decoration_position_funcs_first.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
**Beartype import hookable configuration first callable decorator position | ||
submodule** (i.e., data module defining callables decorated by chains of one or | ||
more decorators into which the :mod:`beartype.beartype` decorator will be | ||
injected as the first decorator, mimicking real-world usage of the | ||
:func:`beartype.claw.beartype_package` import hook from external callers). | ||
''' | ||
|
||
# ....................{ IMPORTS }.................... | ||
from beartype.roar import BeartypeCallHintReturnViolation | ||
from beartype.typing import no_type_check | ||
from beartype._util.py.utilpyversion import IS_PYTHON_AT_LEAST_3_11 | ||
from pytest import raises | ||
|
||
# ....................{ FUNCTIONS }.................... | ||
# Validate that the import hook presumably registered by the caller implicitly | ||
# injects the @beartype decorator as the first decorator in *ALL* chains of | ||
# callable decorations. | ||
|
||
@no_type_check | ||
def night_followed() -> int: | ||
''' | ||
Arbitrary function trivially violating its return type hint. | ||
''' | ||
|
||
return 'Night followed, clad with stars. On every side' | ||
|
||
# Assert that the import hook registered by the caller ignored the | ||
# @no_type_check decorator decorating the callable defined above by forcefully | ||
# injecting the @beartype decorator as the first decorator on that callable. | ||
with raises(BeartypeCallHintReturnViolation): | ||
night_followed() | ||
|
||
# ....................{ CLASSES }.................... | ||
# Validate that the import hook presumably registered by the caller implicitly | ||
# injects the @beartype decorator as the last decorator in *ALL* chains of | ||
# class decorations. | ||
|
||
# If the active Python interpreter targets Python >= 3.11, the standard | ||
# @no_type_check decorator correctly applies itself to classes. In this case... | ||
if IS_PYTHON_AT_LEAST_3_11: | ||
@no_type_check | ||
class MoreHorribly(object): | ||
''' | ||
Arbitrary class decorated by one or more decorators, which the import | ||
hook registered by the caller will then respect by continuing to inject | ||
the :func:`beartype.beartype` decorator as the last decorator. | ||
''' | ||
|
||
@staticmethod | ||
def the_multitudinous_streams() -> int: | ||
''' | ||
Arbitrary static method trivially violating its return type hint. | ||
''' | ||
|
||
return 'More horribly the multitudinous streams' | ||
|
||
# Assert that the import hook registered by the caller respected the | ||
# @no_type_check decorator decorating the class defined above by continuing | ||
# to inject the @beartype decorator as the last decorator on that class. | ||
assert MoreHorribly.the_multitudinous_streams() == ( | ||
'More horribly the multitudinous streams') | ||
# Else, the active Python interpreter targets Python <= 3.10. In this case, the | ||
# standard @no_type_check decorator fails to correctly apply itself to classes. | ||
# In this case, simply avoid performing the prior test for simplicity. *shrug* |
62 changes: 62 additions & 0 deletions
62
...claw/intraprocess/hookable_package/conf/data_claw_conf_decoration_position_types_first.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
**Beartype import hookable configuration first class decorator position | ||
submodule** (i.e., data module defining classes decorated by chains of one or | ||
more decorators into which the :mod:`beartype.beartype` decorator will be | ||
injected as the first decorator, mimicking real-world usage of the | ||
:func:`beartype.claw.beartype_package` import hook from external callers). | ||
''' | ||
|
||
# ....................{ IMPORTS }.................... | ||
from beartype.roar import BeartypeCallHintReturnViolation | ||
from beartype.typing import no_type_check | ||
from pytest import raises | ||
|
||
# ....................{ CLASSES }.................... | ||
# Validate that the import hook presumably registered by the caller implicitly | ||
# injects the @beartype decorator as the first decorator in *ALL* chains of | ||
# class decorations. | ||
|
||
@no_type_check | ||
class EntwinedInDuskierWreaths(object): | ||
''' | ||
Arbitrary class decorated by one or more decorators, which the import hook | ||
registered by the caller will then ignore by forcefully injecting the | ||
:func:`beartype.beartype` decorator as the first decorator. | ||
''' | ||
|
||
@staticmethod | ||
def her_braided_locks() -> int: | ||
''' | ||
Arbitrary static method trivially violating its return type hint. | ||
''' | ||
|
||
return 'Entwined in duskier wreaths her braided locks' | ||
|
||
# Assert that the import hook registered by the caller ignored the | ||
# @no_type_check decorator decorating the class defined above by forcefully | ||
# injecting the @beartype decorator as the first decorator on that class. | ||
with raises(BeartypeCallHintReturnViolation): | ||
EntwinedInDuskierWreaths.her_braided_locks() | ||
|
||
# ....................{ FUNCTIONS }.................... | ||
# Validate that the import hook presumably registered by the caller implicitly | ||
# injects the @beartype decorator as the last decorator in *ALL* chains of | ||
# callable decorations. | ||
|
||
@no_type_check | ||
def over_the_fair_front() -> int: | ||
''' | ||
Arbitrary function trivially violating its return type hint. | ||
''' | ||
|
||
return "O'er the fair front and radiant eyes of day;" | ||
|
||
# Assert that the import hook registered by the caller respected the | ||
# @no_type_check decorator decorating the function defined above by continuing | ||
# to inject the @beartype decorator as the last decorator on that function. | ||
assert over_the_fair_front() == "O'er the fair front and radiant eyes of day;" |
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
22 changes: 22 additions & 0 deletions
22
...st/a00_unit/data/claw/intraprocess/hookable_package/pep/pep526/data_claw_pep526_ignore.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
**Beartype import hookable** :pep:`526` **ignorance submodule** (i.e., data | ||
module containing *only* :pep:`526`-compliant annotated variable assignments | ||
ignored rather than type-checked, mimicking real-world usage of the | ||
:func:`beartype.claw.beartype_package` import hook from external callers). | ||
''' | ||
|
||
# ....................{ PEP 526 }.................... | ||
# Validate that the import hook presumably installed by the caller avoids | ||
# implicitly appending all PEP 526-compliant annotated assignment statements in | ||
# this submodule with calls to beartype's statement-level | ||
# beartype.door.die_if_unbearable() exception-raiser. | ||
|
||
# Assert that a PEP 526-compliant annotated assignment statement assigning an | ||
# object violating the type hint annotating that statement raises *NO* | ||
# exception. | ||
twilight_ascending_slowly: int = 'Twilight, ascending slowly from the east,' |
Oops, something went wrong.