[Python-3000] [patch] Should 2to3 point out *possible*, but not definite changes?

Adrian Holovaty adrian at holovaty.com
Sat Aug 25 07:58:57 CEST 2007


On 8/24/07, Collin Winter <collinw at gmail.com> wrote:
> Adrian and I talked about this this morning, and he said he's going to
> go ahead with an implementation. The original warning messages were a
> good idea, but they tend to get lost when converting large projects.

(I assume this is the place to post patches for the 2to3 utility, but
please set me straight if I should use bugs.python.org instead...)

I've attached two patches that implement the 2to3 change discussed in
this thread. In 2to3_insert_comment.diff --

* fixes/util.py gets an insert_comment() function. Give it a Node/Leaf
and a comment message, and it will insert a Python comment before the
given Node/Leaf. This takes indentation into account, such that the
comment will be indented to fix the indentation of the line it is
commenting. For example:

    if foo:
        # comment about bar()
        bar()

It also handles existing comments gracefully. If a line already has a
comment above it, the new comment will be added on a new line under
the old one.

* pytree.Base gets two new methods: get_previous_sibling() and
get_previous_in_tree(). These just made it easier and clearer to
implement insert_comment().

* tests/test_util.py has unit tests for insert_comment(), and
tests/test_pytree.py has tests for the two new pytree.Base methods.

The other patch, 2to3_comment_warnings.diff, is an example of how we
could integrate this new insert_comment() method to replace the
current functionality of fixes.basefix.BaseFix.warning(). To see this
in action, apply these two patches and run the 2to3 script
(refactor.py) on the following input:

    foo()
    map(f, x)

The resulting output should display a Python comment above the map()
call instead of outputting a warning to stdout, which was the previous
behavior.

If these patches are accepted, the next steps would be to change the
behavior of warns() and warns_unchanged() in tests/test_fixers.py, so
that the tests can catch the new behavior.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2to3_comment_warnings.diff
Type: application/octet-stream
Size: 741 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20070825/c87ff631/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2to3_insert_comment.diff
Type: application/octet-stream
Size: 11031 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-3000/attachments/20070825/c87ff631/attachment-0001.obj 


More information about the Python-3000 mailing list