Dark magics about variable names in python
Overall
score
90%
A utility for logging and tracking variable assignments in different contexts.
Track variable names for direct assignments where the assignment is the primary statement.
user_data = create_record() should return "user_data") @testfirst, second = create_pair() should return ("first", "second")) @testconfig: Config = create_config() should return "config") @testTrack variable names when assignments occur within expressions or control flow statements.
if data := fetch_data(): ... should return "data") @testwhile item := get_next(): ... should return "item") @testresult = process(data := fetch()) where fetch() is called should return "data") @test@generates
def track_direct_assignment() -> str | tuple[str, ...]:
"""
Captures the variable name(s) from a direct assignment statement.
Returns a string for single assignment or tuple of strings for multiple assignments.
Should be called from functions that are directly assigned to variables.
"""
pass
def track_expression_assignment() -> str:
"""
Captures the variable name from an assignment within an expression context.
Returns a string with the variable name.
Should be called from functions used in walrus operators or expression contexts.
"""
passProvides variable name introspection capabilities.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/pypi-varnameevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10