Title: 2to3 fix_renames doesn't rename string.lowercase/uppercase/letters
Type: behavior Stage: patch review
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.4, Python 3.5, Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Anthony.Kong, aldwinaldwin, benjamin.peterson, ezio.melotti, meador.inge, superluser
Priority: normal Keywords: easy, patch

Created on 2011-10-27 07:27 by ezio.melotti, last changed 2019-07-18 08:42 by aldwinaldwin.

File name Uploaded Description Edit
string_partial.patch superluser, 2014-09-09 20:52 review
Pull Requests
URL Status Linked Edit
PR 14835 open aldwinaldwin, 2019-07-18 08:34
Messages (5)
msg146475 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-10-27 07:27
$ cat 
from string import lowercase, uppercase, letters
print lowercase == 'abcdefghijklmnopqrstuvwxyz'
print letters == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
$ python 
$ 2to3 -w 
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored
--- (original)
+++ (refactored)
@@ -1,4 +1,4 @@
 from string import lowercase, uppercase, letters
-print lowercase == 'abcdefghijklmnopqrstuvwxyz'
-print letters == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+print(lowercase == 'abcdefghijklmnopqrstuvwxyz')
+print(letters == 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
RefactoringTool: Files that were modified:
$ python3 
Traceback (most recent call last):
  File "", line 1, in <module>
    from string import lowercase, uppercase, letters
ImportError: cannot import name lowercase

They should be renamed to ascii_*.
msg221808 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-28 18:22
@Ezio can you prepare a patch for this?
msg226659 - (view) Author: Rose Ames (superluser) * Date: 2014-09-09 20:52
Changing the imports only is straightforward, but I'm having trouble detecting and changing future uses of the variables, without also clobbering user-defined variables with the same names.

I notice some of the current fixers have similar problems, for example the itertools fixer changes:

def imap(): pass


def imap(): pass

This patch is a little smarter than that, but it only detects top-level definitions.  For example, the lowercase in 

class Foo:
  def __init__(self):
    self.lowercase = "blargh"

still gets renamed.

I can work on this more, but would like to know:

a) whether this fix is still wanted
b) how smart it needs to be
c) whether my approach is reasonable.  I feel like there should be an easier way.
msg226660 - (view) Author: Rose Ames (superluser) * Date: 2014-09-09 20:55

d) how to format code :\
msg348107 - (view) Author: Aldwin Pollefeyt (aldwinaldwin) * Date: 2019-07-18 08:42
* there were many small issues with the patch, improved as good as possible the FixString to catch ```import as```, ```from string import *``` and ```import string```

* did not include the FixStringImports as it's not a necessity to work in Python3

* indeed, some uses of the constant names that are user-defined will also rename, only when it's in a ```import string```. Added a ```Caution``` in the documentation. Still, when all are changed, it should not affect the Python3 version, will just work with the renamed variable name.
Date User Action Args
2019-07-18 08:42:25aldwinaldwinsetnosy: + aldwinaldwin
messages: + msg348107
2019-07-18 08:34:06aldwinaldwinsetstage: test needed -> patch review
pull_requests: + pull_request14626
2019-04-26 20:34:34BreamoreBoysetnosy: - BreamoreBoy
2014-09-09 20:55:39superlusersetmessages: + msg226660
2014-09-09 20:52:39superlusersetfiles: + string_partial.patch

nosy: + superluser
messages: + msg226659

keywords: + patch
2014-07-28 02:53:49Anthony.Kongsetnosy: + Anthony.Kong
2014-06-28 22:17:07ezio.melottisetkeywords: + easy
2014-06-28 18:22:48BreamoreBoysetnosy: + BreamoreBoy

messages: + msg221808
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2011-10-27 14:07:22meador.ingesetnosy: + meador.inge
2011-10-27 07:27:05ezio.melotticreate