This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 2to3: Wrong code output w/ has_key without trailing space
Type: behavior Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder: Close 2to3 issues and list them here
View: 45544
Assigned To: Nosy List: benjamin.peterson, ericvw, josh.r, r.david.murray, rippey.e, terry.reedy
Priority: normal Keywords:

Created on 2016-08-25 00:11 by rippey.e, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg273613 - (view) Author: E Rippey (rippey.e) Date: 2016-08-25 00:11
The "has_key" fixer in 2to3 produces wrong code on the following example:

input:"a.has_key(b)and x"
output:"b in aand x"

Note the lack of space before "and" in the input.
msg273622 - (view) Author: Eric N. Vander Weele (ericvw) * Date: 2016-08-25 04:21
I am unable to replicate this issue with '2to3' from Python 2.7.12, Python 3.4.5, nor Python 3.5.2.

Below is what I get with the versions I mentioned above.

---

$ cat test.py
a.has_key(b) and x

$ 2to3 test.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored test.py
--- test.py    	(original)
+++ test.py    	(refactored)
@@ -1 +1 @@
-a.has_key(b) and x
+b in a and x
RefactoringTool: Files that need to be modified:
RefactoringTool: test.py

---
msg273623 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-08-25 04:55
Eric, remove the space from after the has_key call to match OP:

$ cat test.py
a.has_key(b)and x

It's legal to omit the space there.
msg273624 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-08-25 04:59
Testing it myself that way:

C:\>type test.py
a.has_key(b)and x

C:\>"C:\Program Files\Python 3.5\Tools\scripts\2to3.py" test.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored test.py
--- test.py     (original)
+++ test.py     (refactored)
@@ -1 +1 @@
-a.has_key(b)and x
+b in aand x
RefactoringTool: Files that need to be modified:
RefactoringTool: test.py
msg273626 - (view) Author: Eric N. Vander Weele (ericvw) * Date: 2016-08-25 05:26
> Eric, remove the space from after the has_key call to match OP:

> $ cat test.py
> a.has_key(b)and x

Good catch.  Disregard msg273622 from me - I am able to replicate what the OP has described.
msg273646 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-25 12:20
I don't think it is necessary or reasonable to expect 2to3 to handle this.  It is a tool for the developer to use, so just clean up the input.
msg273673 - (view) Author: E Rippey (rippey.e) Date: 2016-08-25 18:45
I have one line change to "Lib/lib2to3/fixes/fix_has_key.py" that will resolve this issue.  Would a patch be accepted?

It doesn't seem like the input is unusually contorted.
msg273715 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-08-26 18:49
If you don't want to sign the CA, https://www.python.org/psf/contrib/, and upload a file without a guarantee, which is never given ;-), just give the patch in a text message.  While it may be unreasonable to *expect* 2to3 to handle this, it also might not be unreasonable for it to do so.  It would be especially useful if one were converting to Py3 and not planning to maintain the 2.x code.

My concern is that this might be one of multiple similar situations, so that patching just one might not be all that helpful.  But I have not looked at the list of fixers to determine is other are affected.
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72042
2021-10-20 22:47:17iritkatrielsetstatus: open -> closed
superseder: Close 2to3 issues and list them here
2016-08-26 18:49:15terry.reedysetnosy: + terry.reedy, benjamin.peterson

messages: + msg273715
versions: + Python 3.5, Python 3.6, - Python 3.4
2016-08-25 18:45:35rippey.esetstatus: closed -> open

messages: + msg273673
2016-08-25 12:20:37r.david.murraysettitle: 2to3: Wrong code output w/ has_key -> 2to3: Wrong code output w/ has_key without trailing space
2016-08-25 12:20:11r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg273646

resolution: rejected
stage: resolved
2016-08-25 05:26:05ericvwsetmessages: + msg273626
2016-08-25 04:59:15josh.rsetmessages: + msg273624
2016-08-25 04:55:45josh.rsetnosy: + josh.r
messages: + msg273623
2016-08-25 04:21:41ericvwsetnosy: + ericvw
messages: + msg273622
2016-08-25 00:11:33rippey.ecreate