pydoc: move __future__ imports out of the DATA block #70308
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
assignee = None closed_at = <Date 2022-03-29.22:07:47.957> created_at = <Date 2016-01-15.06:31:39.957> labels = ['3.11', 'type-feature', 'library', 'docs'] title = 'pydoc: move __future__ imports out of the DATA block' updated_at = <Date 2022-03-29.22:07:47.956> user = 'https://github.com/anntzer'
activity = <Date 2022-03-29.22:07:47.956> actor = 'iritkatriel' assignee = 'docs@python' closed = True closed_date = <Date 2022-03-29.22:07:47.957> closer = 'iritkatriel' components = ['Documentation', 'Library (Lib)'] creation = <Date 2016-01-15.06:31:39.957> creator = 'Antony.Lee' dependencies =  files =  hgrepos =  issue_num = 26120 keywords = ['patch'] message_count = 15.0 messages = ['258272', '407922', '407939', '411604', '416086', '416155', '416160', '416192', '416212', '416213', '416263', '416264', '416276', '416284', '416301'] nosy_count = 4.0 nosy_names = ['docs@python', 'serhiy.storchaka', 'veky', 'iritkatriel'] pr_nums = ['30888', '32180'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue26120' versions = ['Python 3.11']
The text was updated successfully, but these errors were encountered:
Currently, for a module that uses future imports, the DATA section of
Even though it is fully-featured _Feature objects that are imported, it probably makes sense to move them out of this section.
It's like this:
>>> import foo >>> dir(foo) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'annotations', 'x'] >>> foo.annotations _Feature((3, 7, 0, 'beta', 1), (3, 11, 0, 'alpha', 0), 16777216)
So the attribute in foo is called annotations, i.e. it's not private.
I am positive about this idea, but we must also think about the possible negative consequences. For example, the future annotations will be included in the star-import by default and can override some global names. The fact that some names not visible in the module help can override global names can be confusing.
I think it should be discussed with a wider auditory.
That's a good point. I see that the __future__ imports appear in the dir() of the module, and indeed they are imported with 'from m import *'.
But I wonder if that is actually a bug. If you try this:
% cat x.py
from __future__ import annotations
% cat y.py
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
but if you add "from __future__ import annotations" at the top of y.py, then it does run.
So perhaps the future imports should be excluded by "from m import *"?
I am sorry that I did not test the changes manually, but it seems that __future__ annotations are now disappeared from the pydoc output for the __future__ module help.
$ ./python -m pydoc __future__
It is now difficult to get a list of available features from the help.
They should be kept in the DATA section for the __future__ module.
I'm not sure - if I revert the change like this:
if isinstance(getattr(obj, name, None), __future__._Feature): - return False + pass # return False
then test_html_doc fails:
Traceback (most recent call last): File "/Users/iritkatriel/src/cpython/Lib/test/test_pydoc.py", line 414, in test_html_doc self.assertEqual(text_lines, expected_lines) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AssertionError: Lists differ: ['tes[1392 chars]t]', "print_function = _Feature((2, 6, 0, 'alp[156 chars]ody'] != ['tes[1392 chars]t]', 'type_union1 = typing.Union[int, str]', '[72 chars]ody']
First differing element 55:
First list contains 1 additional elements.
['test.pydoc_mod (version 126.96.36.199)',