-
-
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.
Assignment expression code optimization x 2.
This commit is the last in a commit chain significantly optimizing generation of **assignment expressions** (i.e., PEP 572-compliant Python expressions containing the ":=" walrus operator) throughout code snippets dynamically generated by @beartype's type-checking code generator. Since strings are expensive to repeatedly generate, @beartype now internally caches and reuses frequently used local variables across assignment expressions. In truth, this means nothing. But @leycec just *had* to do this to satisfy his aberrant OCD disorder. I swear! It isn't my fault! It's fast or it ain't @beartype. (*Holy bawling bowlers!*)
- Loading branch information
Showing
6 changed files
with
158 additions
and
22 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
Empty file.
53 changes: 53 additions & 0 deletions
53
beartype_test/a00_unit/a60_check/a00_code/snip/test_codesnipcls.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,53 @@ | ||
#!/usr/bin/env python3 | ||
# --------------------( LICENSE )-------------------- | ||
# Copyright (c) 2014-2024 Beartype authors. | ||
# See "LICENSE" for further details. | ||
|
||
''' | ||
Beartype **type-checking expression snippet class unit tests.** | ||
This submodule unit tests the public API of the public | ||
:mod:`beartype._check.code.snip.codesnipcls.PITH_INDEX_TO_VAR_NAME` submodule. | ||
''' | ||
|
||
# ....................{ IMPORTS }.................... | ||
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
# WARNING: To raise human-readable test errors, avoid importing from | ||
# package-specific submodules at module scope. | ||
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
||
# ....................{ TESTS }.................... | ||
def test_pith_index_to_var_name() -> None: | ||
''' | ||
Test the :obj:`beartype._check.code.snip.codesnipcls.PITH_INDEX_TO_VAR_NAME` | ||
dictionary singleton. | ||
''' | ||
|
||
# ....................{ IMPORTS }.................... | ||
# Defer test-specific imports. | ||
from beartype._check.code.snip.codesnipcls import PITH_INDEX_TO_VAR_NAME | ||
from beartype._check.checkmagic import VAR_NAME_PITH_PREFIX | ||
from pytest import raises | ||
|
||
# ....................{ PASS }.................... | ||
# Assert this dictionary indexed by various positive integers creates and | ||
# returns the expected local pith variable names. | ||
assert PITH_INDEX_TO_VAR_NAME[1] == f'{VAR_NAME_PITH_PREFIX}1' | ||
assert PITH_INDEX_TO_VAR_NAME[2] == f'{VAR_NAME_PITH_PREFIX}2' | ||
|
||
# Assert this dictionary internally caches these constants. | ||
assert PITH_INDEX_TO_VAR_NAME[1] is PITH_INDEX_TO_VAR_NAME[1] | ||
assert PITH_INDEX_TO_VAR_NAME[2] is PITH_INDEX_TO_VAR_NAME[2] | ||
|
||
# ....................{ FAIL }.................... | ||
# Assert that attempting to index this dictionary by non-integer indices | ||
# raises the expected exception. | ||
with raises(AssertionError): | ||
PITH_INDEX_TO_VAR_NAME[2.34] | ||
|
||
# Assert that attempting to index this dictionary by non-positive indices | ||
# raises the expected exception. | ||
with raises(AssertionError): | ||
PITH_INDEX_TO_VAR_NAME[0] | ||
with raises(AssertionError): | ||
PITH_INDEX_TO_VAR_NAME[-1] |