Commit 636926d
authored
Add Variable type attribution for Python identifiers (#6772)
* ChangeImport: rename bare references when from-import name changes
When `from X import old_name` is rewritten to `from X import new_name`,
bare references to `old_name` in the code (e.g., function calls like
`clock()`) are now renamed to `new_name` (e.g., `perf_counter()`).
This is done via a new `visit_identifier` method in ChangeImportVisitor
that renames matching identifiers outside of import statements. An xfail
test documents that scope analysis for shadowed locals is not yet
implemented.
* Add Variable type attribution for Python identifiers
Populate field_type with JavaType.Variable on Identifiers for local
variables, module-level constants, and class fields (self.x). Uses a
single ty hover query per name/attribute to avoid double lookups.
- Implement JavaType.Variable as a full dataclass with name, type, owner
- Add name_type_info() and attribute_type_info() to PythonTypeMapping
- Wire visit_Name and visit_Attribute to use the new combined methods
- Add RPC sender/receiver support for Variable serialization
- Add tests for variable hover detection, Variable creation, and
integration tests with ty for name/attribute type info
* ChangeImport: skip renaming local variables that shadow imports
Use field_type from Variable type attribution to distinguish local
variable references from imported symbol references. Also exclude
Unknown hover results from being treated as variables.
* Address code review feedback
- Fix frozen mismatch: remove frozen=True from Variable .pyi stub
- Use constructor kwargs in _make_variable instead of post-mutation
- Remove duplicate Variable receiver code in python_receiver.py
- Make remove_import.py visit_import/visit_multi_import exception-safe
with try/finally
- Document limitations: field_type shadow detection needs ty,
visit_method_invocation only handles simple Identifier selects,
attribute_type_info column offset for multiline expressions
- Add test for both from-import and direct import simultaneously
* ChangeImport: scope shadow check to function bodies only
Module-level bare references to an imported name were incorrectly
skipped when type attribution populated field_type. Limit the
field_type shadow check to identifiers inside MethodDeclaration
scopes so module-level references are always renamed.
Also add default initializers to visitor fields, expand inline
documentation, and remove redundant truthiness guards in the RPC
variable receiver.
* Skip shadow detection test when ty CLI is not available1 parent 3d47629 commit 636926d
10 files changed
Lines changed: 623 additions & 29 deletions
File tree
- rewrite-python/rewrite
- src/rewrite
- java
- python
- recipes
- rpc
- tests
- python
- recipes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| 301 | + | |
301 | 302 | | |
302 | | - | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
303 | 328 | | |
304 | 329 | | |
305 | 330 | | |
| |||
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
147 | 148 | | |
148 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
149 | 165 | | |
150 | 166 | | |
151 | 167 | | |
| |||
Lines changed: 19 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
1050 | 1051 | | |
1051 | 1052 | | |
1052 | 1053 | | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
1053 | 1065 | | |
1054 | 1066 | | |
1055 | | - | |
| 1067 | + | |
1056 | 1068 | | |
1057 | | - | |
1058 | | - | |
1059 | | - | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
1060 | 1072 | | |
1061 | 1073 | | |
1062 | 1074 | | |
| |||
2548 | 2560 | | |
2549 | 2561 | | |
2550 | 2562 | | |
| 2563 | + | |
2551 | 2564 | | |
2552 | 2565 | | |
2553 | 2566 | | |
2554 | 2567 | | |
2555 | 2568 | | |
2556 | 2569 | | |
2557 | | - | |
2558 | | - | |
| 2570 | + | |
| 2571 | + | |
2559 | 2572 | | |
2560 | 2573 | | |
2561 | 2574 | | |
| |||
Lines changed: 47 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
131 | 135 | | |
132 | 136 | | |
133 | 137 | | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
140 | 144 | | |
141 | 145 | | |
142 | 146 | | |
| |||
237 | 241 | | |
238 | 242 | | |
239 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
240 | 269 | | |
241 | 270 | | |
242 | | - | |
243 | | - | |
244 | 271 | | |
245 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
246 | 278 | | |
247 | 279 | | |
248 | 280 | | |
249 | 281 | | |
250 | 282 | | |
251 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
252 | 289 | | |
253 | 290 | | |
254 | 291 | | |
| |||
Lines changed: 8 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
117 | | - | |
118 | | - | |
119 | | - | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
124 | | - | |
125 | | - | |
126 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
127 | 129 | | |
128 | 130 | | |
129 | 131 | | |
| |||
Lines changed: 82 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
269 | 317 | | |
270 | 318 | | |
271 | 319 | | |
| |||
281 | 329 | | |
282 | 330 | | |
283 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
284 | 366 | | |
285 | 367 | | |
286 | 368 | | |
| |||
0 commit comments