Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Setting __dir__ with a list is no more allowed
Python 3.7.0 (default, Jun 28 2018, 18:00:41)
[GCC 8.1.1 20180531] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import astropy
>>> dir(astropy)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
  • Loading branch information
saimn committed Jul 2, 2018
commit a6e2c154bfe596961e4995bc6217e78660d2033b
10 changes: 5 additions & 5 deletions astropy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,17 @@ def online_help(query):
webbrowser.open(url)


__dir__ = ['__version__', '__githash__', '__minimum_numpy_version__',
'__bibtex__', 'test', 'log', 'find_api_page', 'online_help',
'online_docs_root', 'conf']
__dir_inc__ = ['__version__', '__githash__', '__minimum_numpy_version__',
'__bibtex__', 'test', 'log', 'find_api_page', 'online_help',
'online_docs_root', 'conf']


from types import ModuleType as __module_type__
# Clean up top-level namespace--delete everything that isn't in __dir__
# Clean up top-level namespace--delete everything that isn't in __dir_inc__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must admit I find this auto-cleanup bad form, but that's somewhat out of scope of this PR!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a cleanup, I had to change the use of __dir__ to list the attributes that shall not be deleted because setting a list to __dir__ no more works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saimn - I meant that I found the whole stanza rather bad form, not your change to it! It just seems that by being a bit more careful in the code itself, one can avoid introducing these unwanted things in the namespace (or delete them after their use). Anyway, as said, definitely out of scope here!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhvk - Ah ok! I agree with you, we could find a simpler way to keep the namespace clean.

# or is a magic attribute, and that isn't a submodule of this package
for varname in dir():
if not ((varname.startswith('__') and varname.endswith('__')) or
varname in __dir__ or
varname in __dir_inc__ or
(varname[0] != '_' and
isinstance(locals()[varname], __module_type__) and
locals()[varname].__name__.startswith(__name__ + '.'))):
Expand Down