Skip to content

Commit

Permalink
Merge pull request #1942 from SublimeLinter/fix-python-project-lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste committed May 2, 2024
2 parents 9c804e8 + 2dae995 commit b85ac7f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
35 changes: 7 additions & 28 deletions lint/base_linter/node_linter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This module exports the NodeLinter subclass of Linter."""

from functools import lru_cache
from itertools import chain, takewhile
from itertools import chain
import json
import os
import shutil
Expand All @@ -14,36 +14,15 @@
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union


HOME = os.path.expanduser('~')


def paths_upwards(path):
# type: (str) -> Iterator[str]
while True:
yield path

next_path = os.path.dirname(path)
# Stop just before root in *nix systems
if next_path == '/':
return

if next_path == path:
return

path = next_path


def paths_upwards_until_home(path):
# type: (str) -> Iterator[str]
return chain(takewhile(lambda p: p != HOME, paths_upwards(path)), [HOME])


def smart_paths_upwards(start_dir):
# type: (str) -> Iterator[str]
# This is special as we also may yield HOME. This is so because
# we might have "global" installations there; we don't expect to
# find a marker file (e.g. "package.json") at HOME.
return (
paths_upwards_until_home(start_dir)
if os.path.commonprefix([start_dir, HOME]) == HOME
else paths_upwards(start_dir)
chain(util.paths_upwards_until_home(start_dir), [util.HOME])
if os.path.commonprefix([start_dir, util.HOME]) == util.HOME
else util.paths_upwards(start_dir)
)


Expand Down
3 changes: 1 addition & 2 deletions lint/base_linter/python_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import sublime

from . import node_linter
from .. import linter, util


Expand Down Expand Up @@ -172,7 +171,7 @@ def get_start_dir(self):

def _nearest_virtual_environment(self, start_dir):
# type: (str) -> Tuple[Optional[str], Optional[str]]
paths = node_linter.smart_paths_upwards(start_dir)
paths = util.paths_upwards_until_home(start_dir)
root_dir_markers = ROOT_MARKERS + self.config_file_names
root_dir = None
for path in paths:
Expand Down
29 changes: 29 additions & 0 deletions lint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import ChainMap
from contextlib import contextmanager
from functools import lru_cache, partial, wraps
from itertools import takewhile
import locale
import logging
import os
Expand Down Expand Up @@ -29,6 +30,7 @@

logger = logging.getLogger(__name__)

HOME = os.path.expanduser('~')

STREAM_STDOUT = 1
STREAM_STDERR = 2
Expand Down Expand Up @@ -219,6 +221,33 @@ def canonical_filename(view):
return view.file_name() or '<untitled {}>'.format(view.buffer_id())


def paths_upwards(path):
# type: (str) -> Iterator[str]
while True:
yield path

next_path = os.path.dirname(path)
# Stop just before root in *nix systems
if next_path == '/':
return

if next_path == path:
return

path = next_path


def paths_upwards_until_home(path):
# type: (str) -> Iterator[str]
"""Yield paths 'upwards' but stop on HOME (excluding it).
Note: If the starting `path` is not below HOME we yield until
root, excluding root on *nix systems, but including it on
Windows.
"""
return takewhile(lambda p: p != HOME, paths_upwards(path))


def get_syntax(view):
# type: (sublime.View) -> str
"""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_node_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def close_view(self, view):
view.close()

def patch_home(self, home):
previous_state = node_linter.HOME
node_linter.HOME = home
self.addCleanup(lambda: setattr(node_linter, 'HOME', previous_state))
previous_state = util.HOME
util.HOME = home
self.addCleanup(lambda: setattr(util, 'HOME', previous_state))

def test_globally_installed(self):
linter = make_fake_linter(self.view)
Expand Down

0 comments on commit b85ac7f

Please sign in to comment.