Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Fixed regression in handling of imports (fixes #160) #161

Merged
merged 4 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions doppel/bin/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,13 @@ def _is_builtin(obj):
# according to the previous checks, but if they're callable
# they should count as exported functions
elif _is_builtin(obj) and callable(obj):
out["functions"][obj_name] = {
"args": []
}
if not obj.__module__.startswith(PKG_NAME):
_log_info("Callable '{}' is a built-in not included in this package's namespace. Skipping it.".format(obj.__name__))
else:
out["functions"][obj_name] = {
"args": []
}
next

else:
_log_info("Could not figure out what {} is".format(obj_name))
Expand Down
57 changes: 56 additions & 1 deletion integration_tests/python_tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def rundescribe():
'testpkguno',
'testpkgdos',
'testpkgtres',
'pythonspecific'
'pythonspecific',
'pythonspecific2'
]

# Added this abomination because something about
Expand Down Expand Up @@ -399,3 +400,57 @@ def test_builtin_method(self, rundescribe):
result_json = rundescribe['pythonspecific']

assert result_json['classes']['MinWrapper']['public_methods']['wrap_min'] == {'args': []}


class TestPythonSpecific:
"""
Test the behavior of analyze.py for packages using
'from <module> import *' in __init__.py. This package also
tests the behavior of analyze.py in the presence of top-level
built-in imports like "from warnings import warn"
"""

def test_top_level_keys(self, rundescribe):
"""
The JSON file produce by doppel-describe
should have only the expected top-level dictionary keys
"""
result_json = rundescribe['pythonspecific2']

for top_level_key in EXPECTED_TOP_LEVEL_KEYS:
assert result_json.get(top_level_key, None) is not None
assert len(result_json.keys()) == NUM_TOP_LEVEL_KEYS

def test_functions(self, rundescribe):
"""
analyze.py should find all functions defined in the package
whose names do not start with "_", regardless of what is in
``__all__`` in ``__init__.py``.
"""
result_json = rundescribe['pythonspecific2']

assert set(result_json['functions'].keys()) == set(['create_warning', 'create_warm_things'])
# imports from other packages are included if you explicitly
# wrap them in a def()
assert 'create_warm_things' in result_json['functions']
# import from other packages are excluded even if you map them to
# a new name in your package
assert 'shmeate_schmarning' not in result_json['functions']
# internal functions
assert '_super_secret' not in result_json['functions'].keys()
# imported standard lib function
assert 'warn' not in result_json['functions'].keys()
# imported non-standard-lib function
assert 'get' not in result_json['functions'].keys()
# imports from other packages are ignored even if you rename them
# or add them to __all__ in __init__.py
assert 'custom_post' not in result_json['functions'].keys()
assert 'post' not in result_json['functions'].keys()

def test_classes(self, rundescribe):
"""
analyze.py should not have found any classes in this
package but should have created the 'classes' section
"""
result_json = rundescribe['pythonspecific2']
assert result_json['classes'] == {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__all__ = [
'create_warning',
'custom_post'
]

from .module import *
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from warnings import warn
from requests import get
from requests import post as custom_post


def create_warning():
print('uh oh')
warn('not good')


shmeate_schmarning = warn


def create_warm_things(**kwargs):

Choose a reason for hiding this comment

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

This whole file is great, but this is the best line.

warn(**kwargs)


def _super_secret():
print('shhhh')
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from setuptools import find_packages
from setuptools import setup


setup(
name='pythonspecific2',
version='0.0.1',
packages=find_packages(),
)