PyCQA / modernize

Modernizes Python code for eventual Python 3 migration. Built on top of fissix (a fork of lib2to3)
https://modernize.readthedocs.org/
Other
355 stars 51 forks source link

Add fixture for ValueError.message to modernize #261

Open CarstenGrohmann opened 2 years ago

CarstenGrohmann commented 2 years ago

Bug report

Please extend the modernize tool to display a warning when ValueError.message is used, or replace the message attribute e.g. with str(<exception>), since ValueError.message no longer exists in Python 3.

Reproducer

$ cat example.py2
#!/usr/bin/env python2.7
try:
    raise ValueError('Foo')
except ValueError as e:
    print "Caught: %s" % e.message

$ modernize example.py2 
 Loading the following fixers:
    fissix.fixes.fix_apply  (apply)
    fissix.fixes.fix_except  (except)
    fissix.fixes.fix_exec  (exec)
    fissix.fixes.fix_execfile  (execfile)
    fissix.fixes.fix_exitfunc  (exitfunc)
    fissix.fixes.fix_funcattrs  (funcattrs)
    fissix.fixes.fix_has_key  (has_key)
    fissix.fixes.fix_idioms  (idioms)
    fissix.fixes.fix_long  (long)
    fissix.fixes.fix_methodattrs  (methodattrs)
    fissix.fixes.fix_ne  (ne)
    fissix.fixes.fix_numliterals  (numliterals)
    fissix.fixes.fix_operator  (operator)
    fissix.fixes.fix_paren  (paren)
    fissix.fixes.fix_reduce  (reduce)
    fissix.fixes.fix_renames  (renames)
    fissix.fixes.fix_repr  (repr)
    fissix.fixes.fix_set_literal  (set_literal)
    fissix.fixes.fix_standarderror  (standarderror)
    fissix.fixes.fix_sys_exc  (sys_exc)
    fissix.fixes.fix_throw  (throw)
    fissix.fixes.fix_tuple_params  (tuple_params)
    fissix.fixes.fix_types  (types)
    fissix.fixes.fix_ws_comma  (ws_comma)
    fissix.fixes.fix_xreadlines  (xreadlines)
    libmodernize.fixes.fix_basestring  (basestring)
    libmodernize.fixes.fix_dict_six  (dict_six)
    libmodernize.fixes.fix_file  (file)
    libmodernize.fixes.fix_filter  (filter)
    libmodernize.fixes.fix_import  (import)
    libmodernize.fixes.fix_imports_six  (imports_six)
    libmodernize.fixes.fix_input_six  (input_six)
    libmodernize.fixes.fix_int_long_tuple  (int_long_tuple)
    libmodernize.fixes.fix_itertools_imports_six  (itertools_imports_six)
    libmodernize.fixes.fix_itertools_six  (itertools_six)
    libmodernize.fixes.fix_map  (map)
    libmodernize.fixes.fix_metaclass  (metaclass)
    libmodernize.fixes.fix_next  (next)
    libmodernize.fixes.fix_print  (print)
    libmodernize.fixes.fix_raise  (raise)
    libmodernize.fixes.fix_raise_six  (raise_six)
    libmodernize.fixes.fix_unichr  (unichr)
    libmodernize.fixes.fix_unicode_type  (unicode_type)
    libmodernize.fixes.fix_urllib_six  (urllib_six)
    libmodernize.fixes.fix_xrange_six  (xrange_six)
    libmodernize.fixes.fix_zip  (zip)
 Applying the following explicit transformations:
    (None)

RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored example.py2
--- example.py2 (original)
+++ example.py2 (refactored)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python2.7
+from __future__ import print_function
 try:
     raise ValueError('Foo')
 except ValueError as e:
-    print "Caught: %s" % e.message
+    print("Caught: %s" % e.message)
RefactoringTool: Files that need to be modified:
RefactoringTool: example.py2

Example of an expected result

--- example.py2 (original)
+++ example.py2 (refactored)
@@ -2,4 +2,4 @@
 try:
     raise ValueError('Foo')
 except ValueError as e:
-    print "Caught: %s" % e.message
+    print("Caught: %s" % str(e))

My environment