freeCodeCamp / freeCodeCamp/curriculum-helpers

get_variable raises AttributeError for negative numbers, collections, and expressions

Open
#599 0 comments 0 reactions 0 assignees Claimed by @majestic-owl448 View on GitHub
Dominant language
TypeScript
Stars
28
Forks
34
PR merge metrics
No merged PRs in 30d

Description

The Python AST helper `Node.get_variable()` raises `AttributeError` for valid assignments whose initializer is an expression or a collection, including a negative number.

Reproduced against current main, `99fb1034a3b23141a51470ede0f1988b967bc3ca`, by importing `Node` from `packages/helpers/python/py_helpers.py`:

```python
Node('x = 1').get_variable('x') # 1
Node('x = "hello"').get_variable('x') # 'hello'
Node('x = -1').get_variable('x')
# AttributeError: 'UnaryOp' object has no attribute 'value'
Node('x = [1, 2]').get_variable('x')
# AttributeError: 'List' object has no attribute 'value'
Node('x = {"a": 1}').get_variable('x')
# AttributeError: 'Dict' object has no attribute 'value'
Node('x = 1 + 1').get_variable('x')
# AttributeError: 'BinOp' object has no attribute 'value'
```

The [implementation](https://github.com/freeCodeCamp/curriculum-helpers/blob/99fb1034a3b23141a51470ede0f1988b967bc3ca/packages/helpers/python/py_helpers.py#L293-L298) returns `var.tree.value.value`. This works for an `ast.Constant` initializer, but the nodes in the failing examples do not have the second `.value` attribute. The helper does not evaluate the initializer.

The helper should define which initializer forms it supports and handle unsupported forms deliberately instead of exposing an internal AST attribute error. The documentation should state that contract. Supporting literal values should not require executing camper code.

Contributor guide

Open the contributing guide

Research direction

Start in packages/helpers/python/py_helpers.py around Node.get_variable(), where the implementation returns var.tree.value.value. Inspect the AST node types shown in the examples and define which initializer forms are supported without execution, how unsupported forms are reported, and what documentation should say. Add coverage for negative numbers, collections, expressions, and the supported contract so completion is observable.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.