-
Notifications
You must be signed in to change notification settings - Fork 180
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
Fix del builtin #355
base: main
Are you sure you want to change the base?
Fix del builtin #355
Conversation
pyflakes/checker.py
Outdated
@@ -185,6 +185,16 @@ class Builtin(Definition): | |||
|
|||
def __init__(self, name): | |||
super(Builtin, self).__init__(name, None) | |||
self.can_delete = False | |||
# __file__ can only be deleted on Python 3 | |||
if not PY2 and name == '__file__': |
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.
Please do verify if __file__
can be deleted or not in Python 2.
d5ffe4d
to
ed63c89
Compare
An exception to the handling of builtins is they can not be deleted.
__builtins__, __file__ and __debug__ can be deleted. Adds a test runner that ensures defined builtins can be loaded, and can be deleted when the python runtime allows it.
@@ -185,6 +185,13 @@ class Builtin(Definition): | |||
|
|||
def __init__(self, name): | |||
super(Builtin, self).__init__(name, None) | |||
self.can_delete = False | |||
# __builtins__ and __file__ can always be deleted. | |||
# __debug__ can be deleted sometimes and not deleted other times. |
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.
Can you please elaborate on this, it is failing for me.
surfer-172-20-4-99-hotspot:Desktop macbox$ python py-test.py
Traceback (most recent call last):
File "py-test.py", line 1, in <module>
del __debug__
NameError: name '__debug__' is not defined
surfer-172-20-4-99-hotspot:Desktop macbox$ python3 py-test.py
Traceback (most recent call last):
File "py-test.py", line 1, in <module>
del __debug__
NameError: name '__debug__' is not defined
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.
On my Python 3.7 (Rawhide), __debug__
can be deleted.
On earlier Python, locally, it isnt.
However it might come down to whether the Python build had debug enabled in it.
We might need to dig into the CPython source to find out, as I dont see any mention of this in peps
# __debug__ can be deleted sometimes and not deleted other times. | ||
# Safest course of action is to assume it can be deleted, in | ||
# order that no error is reported by pyflakes | ||
elif name in ('__builtins__', '__file__', '__debug__'): |
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.
elif
should be if
.
The recent builtin rewrite had one regression, in that it allowed them to be deleted.
The first commit fixes that, but there are several builtins which can be deleted.
The second commit allows those builtins to be deleted without an UndefinedName being reported , which is an enhancement.
This also adds a test rig for determining which builtins can be deleted.