Python: an import normalizer for files mixing `import m` and `from m import x`
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 42/100
Research direction
Start by reading issue #8790 and the import-binding behavior described here. Define the normalizer's configured preference and whole-file, all-or-nothing coverage rules for converting between import styles. Done means imports added by splices and already present in the file can be normalized without unsafe partial collapsing or shadowing.
Written by the indexing model from the issue text.
Description
- Since #8790, a template's
contextbinds its modules in the file it is spliced into. Binding is per (module, member), so a file that imports the member gets the module import added next to it:
# file: from subprocess import run
# template: template(f"subprocess.run({arg}, shell=True)", context=["import subprocess"])
from subprocess import run
import subprocess # added
out = subprocess.run('ls', shell=True)
That is correct and it runs, but the file already had what the splice needed. The pattern side is style-agnostic — pattern("json.dumps({val})", context=["import json"]) matches a file that wrote from json import dumps; dumps(x) — so matching treats the two styles as one thing and emitting does not.
Adapting the splice to the file's style is the wrong fix at the template layer. Binding is per member, so a template reading both subprocess.run and subprocess.PIPE in a file that imports only run can be collapsed for neither use or for one, and collapsing one is worse than collapsing neither. It is also not a rename: subprocess.run(...) is a MethodInvocation with a select and subprocess.PIPE a FieldAccess, so both change shape, and the resulting bare name has to be unshadowed where it lands.
A normalizer recipe is the right owner. It sees the whole file rather than one splice, so all-or-nothing coverage is decidable; it can go in both directions against a configured preference; and it reaches imports no template put there.
- Dominant language
- Java
- Stars
- 3.9k
- Forks
- 599
- Avg merge
- 9h 38m
- Merged PRs (30d)
- 557
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openrewrite/rewrite
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
openrewrite/rewrite#9835 ·
Maintainers usually reply within 1 day
-
bug gradle
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
openrewrite/rewrite#7576 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
openrewrite/rewrite#9834 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
openrewrite/rewrite#9819 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 55/100
openrewrite/rewrite#9817 ·
Maintainers usually reply within 1 day
All issues in openrewrite/rewrite
Similar issues
-
Four TestConstraintBasedAlgorithm tests pass when nothing is thrownPossibly taken @sharanggupta claimed this today. Open
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
-
bug documentation good first issue help wanted priority low size XS
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
martin-francois/symphony-trello#1351 ·
Maintainers usually reply within 1 day
-
Add test coverage for ConvertUtil.parseDate() with NULL inputPossibly taken @mirajul-mollah7 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 78/100
eclipse-sw360/sw360#4792 · 1 comment ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-emulator#30 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 4 days