Commit b324a3e
Fix strict-module is_mutable matching substrings of "mutable" (#123)
Summary:
`is_mutable` in `compiler/strict/code_gen_base.py`:
```python
def is_mutable(node: AST) -> bool:
return isinstance(node, Name) and node.id in ("mutable")
```
`("mutable")` is a parenthesized string, **not a tuple** (no trailing comma), so `node.id in ("mutable")` is a **substring** test, not equality. Any bare-`Name` decorator whose id is a substring of `"mutable"` — e.g. `table`, `able`, `mut`, `mutabl`, `e`, even `@""` — wrongly matches. The sibling helper in the same package, `strict/preprocessor.py`, has the intended form: `node.id == "mutable"`.
### Impact
`is_mutable` gates immutability/freezing of strict-module classes:
- `find_immutability_flag` strips matching decorators:
```python
node.decorator_list = [d for d in node.decorator_list if not is_mutable(d)]
return old_size == len(node.decorator_list)
```
A class decorated with a substring-of-"mutable" name (notably `table`) has that decorator **silently dropped** from `decorator_list` (so the decorator never runs) and is reported as mutable, so it is **not frozen**.
- `FindClassDef.visit_ClassDef` uses it to decide whether the module contains a freezable class, so detection is also wrong.
### Fix
Use `== "mutable"`, matching `preprocessor.py`. One line.
### Tests
Added `test_strict/test_is_mutable.py`:
| decorator | before | after |
|-----------|--------|-------|
| `nutritious` | matches ✓ | matches ✓ |
| `table`, `able`, `mut`, `e`, `@""` | **matches (bug)** | does not match ✓ |
| `frozen` | no match ✓ | no match ✓ |
Pull Request resolved: #123
Reviewed By: mpage
Differential Revision: D110795208
Pulled By: alexmalyshev
fbshipit-source-id: 7b1d0b540359fddb1050c1b989b6e49273e19e041 parent 0b234df commit b324a3e
2 files changed
Lines changed: 34 additions & 1 deletion
File tree
- cinderx/PythonLib
- cinderx/compiler/strict
- test_cinderx/test_compiler/test_strict
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
0 commit comments