Skip to content

Commit

Permalink
Improve some error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rec committed Oct 21, 2024
1 parent 8b01277 commit 4c1628d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
16 changes: 7 additions & 9 deletions python/setlint/setlint/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,32 @@
from typing import Sequence


def resolve_python_files(
include: list[str], exclude: list[str], root: str = "."
) -> list[Path]:
def resolve_python_files(include: list[str], exclude: list[str]) -> list[Path]:
include = [j for i in include for j in i.split(":")]
exclude = [j for i in exclude or () for j in i.split(":")]

iglobs = python_glob(Path(root), include, check_errors=True)
eglobs = python_glob(Path(root), exclude, check_errors=False)
iglobs = python_glob(include, check_errors=True)
eglobs = python_glob(exclude, check_errors=False)

return sorted(iglobs - eglobs)


def python_glob(root: Path, strings: Sequence[str], *, check_errors) -> set[Path]:
def python_glob(strings: Sequence[str], *, check_errors) -> set[Path]:
result: set[Path] = set()

nonexistent: list[str] = []
not_python: list[str] = []

for s in strings:
p = root / s
p = Path(s).expanduser()
if p.is_dir():
result.update(p.glob("**/*.py"))
elif p.suffix != ".py":
nonexistent.append((str(p)))
not_python.append((str(p)))
elif p.exists():
result.add(p)
else:
not_python.append(str(p))
nonexistent.append(str(p))

if check_errors and (nonexistent or not_python):
raise ValueError(
Expand Down
8 changes: 4 additions & 4 deletions python/setlint/setlint/fix_set_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ def _add_import(pf: PythonFile) -> None:
for tl in pf.token_lines:
t = tl.tokens[0]
if t.type == INDENT:
DEBUG and print('INDENT', tl)
DEBUG and print("INDENT", tl)
break
elif t.type == COMMENT:
DEBUG and print('COMMENT', tl)
DEBUG and print("COMMENT", tl)
comments.append(tl)
elif t.type == NAME and t.string in ("from", "import"):
print('import', tl)
DEBUG and print("import", tl)
if any(i.type == NAME and i.string == "OrderedSet" for i in tl.tokens):
return
elif t.string == "from":
froms.append(tl)
else:
imports.append(tl)
else:
DEBUG and print('other', t)
DEBUG and print("other", t)

if section := froms or imports or comments:
insert_before = section[-1].tokens[-1].start[0] + 1
Expand Down

0 comments on commit 4c1628d

Please sign in to comment.