-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fix_nonzero fixer to rename and alias __nonzero__
to __bool__
.
#205
Open
jamesdbowman
wants to merge
2
commits into
PyCQA:master
Choose a base branch
from
jamesdbowman:fix-nonzero
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Fixer for `__nonzero__` -> `__bool__`, adding an alias for PY2 compatibility. | ||
|
||
Based on Lib/lib2to3/fixes/fix_nonzero.py. | ||
|
||
""" | ||
# This is a derived work of Lib/lib2to3/fixes/fix_nonzero.py. That file | ||
# is under the copyright of the Python Software Foundation and licensed | ||
# under the Python Software Foundation License 2. | ||
# | ||
# Copyright notice: | ||
# | ||
# 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, | ||
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 | ||
# Python Software Foundation; All Rights Reserved. | ||
|
||
# Author: Collin Winter, James Bowman | ||
|
||
from __future__ import absolute_import | ||
from lib2to3 import fixer_base | ||
from lib2to3.fixer_util import Assign, Dot, find_indentation, Leaf, Name, Newline, Node, syms | ||
from lib2to3.pygram import token | ||
import libmodernize | ||
|
||
|
||
class FixNonzero(fixer_base.BaseFix): | ||
BM_compatible = True | ||
|
||
PATTERN = """ | ||
classdef< 'class' any+ ':' | ||
suite< any* | ||
funcdef< 'def' name='__nonzero__' | ||
parameters< '(' NAME ')' > any+ > | ||
any* > > | ||
""" | ||
|
||
def transform(self, node, results): | ||
# Start by making the same transformation as lib2to3.fix_nonzero, purely | ||
# renaming the method: | ||
|
||
name = results["name"] | ||
bool_funcdef = name.parent | ||
new_name = Name("__bool__", prefix=name.prefix) | ||
name.replace(new_name) | ||
|
||
# Then, import six and add a conditional PY2-enabled function alias: | ||
|
||
libmodernize.touch_import(None, u'six', node) | ||
|
||
suite = bool_funcdef.parent | ||
bool_funcdef_suite = bool_funcdef.children[-1] | ||
old_dedent = bool_funcdef_suite.children[-1] | ||
new_dedent = Leaf(token.DEDENT, '', prefix='\n' + find_indentation(bool_funcdef)) | ||
bool_funcdef_suite.children[-1].replace(new_dedent) | ||
|
||
reassignment = Node(syms.suite, [ | ||
Newline(), | ||
Leaf(token.INDENT, | ||
find_indentation(bool_funcdef.children[-1].children[1])), | ||
Node(syms.simple_stmt, | ||
[Assign(Name('__nonzero__'), Name('__bool__')), | ||
Newline()]), old_dedent | ||
]) | ||
|
||
if_stmt = Node(syms.if_stmt, [ | ||
Name('if'), | ||
Node(syms.power, | ||
[Name(' six'), | ||
Node(syms.trailer, [Dot(), Name('PY2')])]), | ||
Leaf(token.COLON, ':'), reassignment | ||
]) | ||
|
||
suite.insert_child(suite.children.index(bool_funcdef) + 1, if_stmt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from __future__ import absolute_import | ||
|
||
from utils import check_on_input | ||
|
||
|
||
NONZERO_DEF = ("""\ | ||
class Foo: | ||
def other(self): | ||
return false | ||
|
||
def __nonzero__(self): | ||
return true | ||
|
||
def another(self): | ||
return false | ||
""", """\ | ||
from __future__ import absolute_import | ||
import six | ||
class Foo: | ||
def other(self): | ||
return false | ||
|
||
def __bool__(self): | ||
return true | ||
|
||
if six.PY2: | ||
__nonzero__ = __bool__ | ||
|
||
def another(self): | ||
return false | ||
""") | ||
|
||
NONZERO_DEF_2_SPACE_INDENT = ("""\ | ||
class Foo: | ||
def other(self): | ||
return false | ||
|
||
def __nonzero__(self): | ||
return true | ||
|
||
def another(self): | ||
return false | ||
""", """\ | ||
from __future__ import absolute_import | ||
import six | ||
class Foo: | ||
def other(self): | ||
return false | ||
|
||
def __bool__(self): | ||
return true | ||
|
||
if six.PY2: | ||
__nonzero__ = __bool__ | ||
|
||
def another(self): | ||
return false | ||
""") | ||
|
||
MULTI_CLASS_NONZERO_DEF = ("""\ | ||
class Foo: | ||
def __nonzero__(self): | ||
return true | ||
|
||
|
||
class Bar: | ||
pass | ||
""", """\ | ||
from __future__ import absolute_import | ||
import six | ||
class Foo: | ||
def __bool__(self): | ||
return true | ||
|
||
if six.PY2: | ||
__nonzero__ = __bool__ | ||
|
||
|
||
class Bar: | ||
pass | ||
""") | ||
|
||
INNER_CLASS_NONZERO_DEF = ("""\ | ||
class Baz: | ||
class Foo: | ||
def __nonzero__(self): | ||
return true | ||
""", """\ | ||
from __future__ import absolute_import | ||
import six | ||
class Baz: | ||
class Foo: | ||
def __bool__(self): | ||
return true | ||
|
||
if six.PY2: | ||
__nonzero__ = __bool__ | ||
""") | ||
|
||
|
||
def test_nonzero_def(): | ||
check_on_input(*NONZERO_DEF) | ||
|
||
def test_nonzero_def_2_space_indent(): | ||
check_on_input(*NONZERO_DEF_2_SPACE_INDENT) | ||
|
||
def test_multi_class_nonzero_def(): | ||
check_on_input(*MULTI_CLASS_NONZERO_DEF) | ||
|
||
def test_inner_class_nonzero_def(): | ||
check_on_input(*INNER_CLASS_NONZERO_DEF) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace
bool_funcdef.children[-1]
withbool_funcdef_suite
.