Skip to content

Commit 289e7dc

Browse files
committed
Problem: TOP_DIR_x are global
Solution: use functools.partial to avoid the need for global vars.
1 parent cbc5855 commit 289e7dc

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

pyannotate_runtime/collect_types.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -703,11 +703,6 @@ def type_consumer():
703703

704704
running = False
705705

706-
TOP_DIR = os.path.join(os.getcwd(), '') # current dir with trailing slash
707-
TOP_DIR_DOT = os.path.join(TOP_DIR, '.')
708-
TOP_DIR_LEN = len(TOP_DIR)
709-
710-
711706
def _make_sampling_sequence(n):
712707
# type: (int) -> List[int]
713708
"""
@@ -784,30 +779,36 @@ def start():
784779
sampling_counters.clear()
785780

786781

787-
def default_filter_filename(filename):
788-
# type: (Optional[str]) -> Optional[str]
782+
def default_filter_filename(top_dir, filename):
783+
# type: (str, Optional[str]) -> Optional[str]
789784
"""Default filter for filenames.
790785
791786
Returns either a normalized filename or None.
792787
You can pass your own filter to init_types_collection().
793788
"""
794789
if filename is None:
795790
return None
796-
elif filename.startswith(TOP_DIR):
797-
if filename.startswith(TOP_DIR_DOT):
791+
elif filename.startswith(top_dir):
792+
top_dir_dot = os.path.join(top_dir, '.')
793+
if filename.startswith(top_dir_dot):
798794
# Skip subdirectories starting with dot (e.g. .vagrant).
799795
return None
800796
else:
801797
# Strip current directory and following slashes.
802-
return filename[TOP_DIR_LEN:].lstrip(os.sep)
798+
return filename[len(top_dir):].lstrip(os.sep)
803799
elif filename.startswith(os.sep):
804800
# Skip absolute paths not under current directory.
805801
return None
806802
else:
807803
return filename
808804

805+
from functools import partial
806+
807+
cwd_default_filter_filename = partial(default_filter_filename,
808+
os.path.join(os.getcwd()))
809+
809810

810-
_filter_filename = default_filter_filename # type: Callable[[Optional[str]], Optional[str]]
811+
_filter_filename = cwd_default_filter_filename # type: Callable[[Optional[str]], Optional[str]]
811812

812813

813814
if sys.version_info[0] == 2:

0 commit comments

Comments
 (0)