[prev in list] [next in list] [prev in thread] [next in thread]
List: python-dev
Subject: [Python-Dev] import * and __future__ imports
From: Irit Katriel via Python-Dev <python-dev () python ! org>
Date: 2022-03-28 13:25:53
Message-ID: 260221262.547806.1648473953269 () mail ! yahoo ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
If you have a __future__ import in a script, and you import * from it in another \
script, the object for this future appears in the dir() of the other script, even \
though the __future__ import has no effect there. % cat x.py
from __future__ import annotations
% cat y.py
from x import *
print(dir())
class D:
def f(self, a: D):
return 42
% ./python.exe y.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', \
'__loader__', '__name__', '__package__', '__spec__', 'annotations'] Traceback (most \
recent call last): File "/Users/iritkatriel/src/cpython-654/y.py", line 5, in \
<module> class D:
^^^^^^^^
File "/Users/iritkatriel/src/cpython-654/y.py", line 6, in D
def f(self, a: D):
^
NameError: name 'D' is not defined
I think we should change import * to exclude the __future__ import objects, and \
perhaps also to not show them in dir(x). Any objections?
This came up in the discussion about https://bugs.python.org/issue26120 . See the \
attached PR for a technique we can use to identify those objects.
[Attachment #5 (text/html)]
<html><head></head><body><div class="yahoo-style-wrap" style="font-family:Helvetica \
Neue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir="ltr" \
data-setdir="false">If you have a __future__ import in a script, and you import * \
from it in another script, the object for this future appears in the dir() of the \
other script, even though the __future__ import has no effect there.</div><div \
dir="ltr" data-setdir="false"><br></div><div dir="ltr" data-setdir="false"><pre \
style="word-wrap: break-word; font-size: 11.895340919494629px; background-color: \
rgb(239, 239, 239);">% cat x.py from __future__ import annotations
<div><pre style="word-wrap: break-word;">% cat y.py
from x import *
print(dir())
class D:
def f(self, a: D):
return 42
% ./python.exe y.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', \
'__loader__', '__name__', '__package__', '__spec__', '<font \
color="#cd232c"><b>annotations</b></font>'] Traceback (most recent call last):
File "/Users/iritkatriel/src/cpython-654/y.py", line 5, in <module>
class D:
^^^^^^^^
File "/Users/iritkatriel/src/cpython-654/y.py", line 6, in D
def f(self, a: D):
^
</pre></div><span style="font-family: "Helvetica Neue", Helvetica, Arial, \
sans-serif;">NameError: name 'D' is not defined</span></pre></div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">I think we should \
change import * to exclude the __future__ import objects, and perhaps also to not \
show them in dir(x). Any objections?<br></div><div dir="ltr" \
data-setdir="false"><br></div><div dir="ltr" data-setdir="false">This came up in the \
discussion about <a href="https://bugs.python.org/issue26120" rel="nofollow" \
target="_blank" class="">https://bugs.python.org/issue26120</a> . See the \
attached PR for a technique we can use to identify those \
objects.</div><div><br></div><div><br></div><div><br></div></div></body></html>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/3BUUCY2OHZILBJLOTS6I33M5YW4INDW3/
Code of Conduct: http://python.org/psf/codeofconduct/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic