Skip to content

Problem: TOP_DIR_x are global #112

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ matrix:
env: USE_MYPY=true
allow_failures:
- python: 3.8-dev
- python: 3.5.1

install:
- pip install -r requirements.txt
Expand Down
24 changes: 14 additions & 10 deletions pyannotate_runtime/collect_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,6 @@ def type_consumer():

running = False

TOP_DIR = os.path.join(os.getcwd(), '') # current dir with trailing slash
TOP_DIR_DOT = os.path.join(TOP_DIR, '.')
TOP_DIR_LEN = len(TOP_DIR)


def _make_sampling_sequence(n):
# type: (int) -> List[int]
"""
Expand Down Expand Up @@ -784,29 +779,38 @@ def start():
sampling_counters.clear()


def default_filter_filename(filename):
# type: (Optional[str]) -> Optional[str]
def _default_filter_filename(top_dir, filename):
# type: (str, Optional[str]) -> Optional[str]
"""Default filter for filenames.

Returns either a normalized filename or None.
You can pass your own filter to init_types_collection().
"""
if filename is None:
return None
elif filename.startswith(TOP_DIR):
if filename.startswith(TOP_DIR_DOT):
elif filename.startswith(top_dir):
top_dir_dot = os.path.join(top_dir, '.')
if filename.startswith(top_dir_dot):
# Skip subdirectories starting with dot (e.g. .vagrant).
return None
else:
# Strip current directory and following slashes.
return filename[TOP_DIR_LEN:].lstrip(os.sep)
return filename[len(top_dir):].lstrip(os.sep)
elif filename.startswith(os.sep):
# Skip absolute paths not under current directory.
return None
else:
return filename


def configure_default_filter_top_dir(directory):
from functools import partial
return partial(_default_filter_filename, directory)


default_filter_filename = configure_default_filter_top_dir(
os.path.join(os.getcwd()))

_filter_filename = default_filter_filename # type: Callable[[Optional[str]], Optional[str]]


Expand Down
2 changes: 1 addition & 1 deletion pyannotate_runtime/tests/test_collect_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def assert_type_comments(self, func_name, comments):
print(' ' + comment)
assert set(item['type_comments']) == set(comments)
assert len(item['type_comments']) == len(comments)
assert os.path.join(collect_types.TOP_DIR, item['path']) == __file__
assert os.path.join(os.getcwd(), item['path']) == __file__


class TestCollectTypes(TestBaseClass):
Expand Down