Skip to content

Commit

Permalink
SWitched to pyright plus uv fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spookylukey committed Dec 25, 2024
1 parent cc8b204 commit df2c3fb
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 35 deletions.
24 changes: 10 additions & 14 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@ jobs:
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }} with uv
uses: drivendataorg/setup-python-uv-action@v1.0.0
with:
python-version: ${{ matrix.python-version }}
- uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ matrix.python }}-${{ hashFiles('requirements*.txt') }}
cache: 'packages'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .
pip install -r requirements-test.txt
uv sync
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand All @@ -37,10 +33,10 @@ jobs:
flake8 . --count --exit-zero --statistics
- name: Test with pytest
run: |
pytest
- name: Test with mypy
uv run pytest
- name: Test with pyright
if: ${{ matrix.python-version == '3.11' }}
# We do this as test rather than pre-commit, to ensure mypy sees all the dependencies
# We do this as test rather than pre-commit, to ensure pyright sees all the dependencies
# and any stubs for them etc.
run: |
mypy .
uv run pyright src tests
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ repos:
rev: 23.12.1
hooks:
- id: black
language_version: python3.10
language_version: python3.11
6 changes: 3 additions & 3 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ If you want to contribute to pyastgrep, great! You'll need to:

pre-commit run --all --all-files

- Optionally, run mypy::
- Optionally, run pyright::

mypy .
pyright .

We only expect it to pass on Python 3.11, which you can check by doing::

tox -e mypy
tox -e pyright


Bug fixes and other changes can be submitted using pull requests on GitHub. For
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ disallow_incomplete_defs = false

[tool.uv]
dev-dependencies = [
"mypy>=1.11.2",
"pre-commit>=3.5.0",
"pyright>=1.1.391",
"pytest>=8.3.3",
"tox-uv>=1.13.1",
"tox>=4.21.2",
"types-lxml>=2024.12.13",
]
16 changes: 16 additions & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": [
"src",
"tests"
],

"exclude": [
"**/dist",
"**/build",
"tests/examples"
],

"ignore": [
"tests/examples"
],
}
3 changes: 0 additions & 3 deletions requirements-test.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/pyastgrep/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_context_lines_for_result(self, result: Match) -> tuple[int, int]:
statement_node = ast_utils.get_ast_statement_node(result_node)
first_line = statement_node.lineno # type: ignore [attr-defined]
if hasattr(statement_node, "decorator_list"):
decorator_list = statement_node.decorator_list
decorator_list = statement_node.decorator_list # type: ignore[reportAttributeAccessIssue]
first_line = min((first_line, *(n.lineno for n in decorator_list)))
before_context = result_node.lineno - first_line # type: ignore [attr-defined]
if isinstance(statement_node.end_lineno, int): # type: ignore [attr-defined]
Expand Down
2 changes: 1 addition & 1 deletion src/pyastgrep/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def line_printer(line: str) -> None:
context_handler.handle_result(result)

if print_ast:
line_printer(astpretty.pformat(result.ast_node))
line_printer(astpretty.pformat(result.ast_node)) # type: ignore[reportPossiblyUnboundVariable]

if print_xml:
line_printer(xml.tostring(result.xml_element, pretty_print=True).decode("utf-8"))
Expand Down
7 changes: 4 additions & 3 deletions src/pyastgrep/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import BinaryIO, Callable, Iterable, Literal, Sequence, Union

from lxml.etree import _Element
from lxml.etree import _Element, _ElementUnicodeResult

from pyastgrep.ignores import WalkError

Expand Down Expand Up @@ -58,7 +58,7 @@ def position_from_node(node: ast.AST) -> Position | None:
return None


def get_query_func(*, xpath2: bool) -> Callable[[_Element, str], Iterable[_Element]]:
def get_query_func(*, xpath2: bool) -> Callable[[_Element, str], Iterable[_Element | _ElementUnicodeResult]]:
if xpath2:
from .xpath2 import elementpath_query

Expand Down Expand Up @@ -101,7 +101,7 @@ def search_python_files(

def search_python_file(
path: Path | BinaryIO,
query_func: Callable[[ast.AST, str], Iterable[_Element]],
query_func: Callable[[_Element, str], Iterable[_Element | _ElementUnicodeResult]],
expression: str,
) -> Iterable[Match | ReadError | NonElementReturned]:
node_mappings: dict[_Element, ast.AST] = {}
Expand Down Expand Up @@ -135,6 +135,7 @@ def search_python_file(
# an attribute rather than a node. We have no way of getting from here to
# something representing an AST node.
yield NonElementReturned(element)
continue

ast_node = node_mappings.get(element, None)
if ast_node is not None:
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pyastgrep.search import search_python_files

if hasattr(contextlib, "chdir"):
chdir = contextlib.chdir
chdir = contextlib.chdir # type: ignore[reportAttributeAccessIssue]
else:
# Python < 3.11
class chdir(contextlib.AbstractContextManager): # type: ignore[type-arg,no-redef]
Expand Down
18 changes: 11 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
[tox]
# Remember to add to .github/workflows/tests.yml if this is added to.
envlist = py38, py39, py310, py311, py312, mypy
envlist = py38, py39, py310, py311, py312, pyright

[testenv]
commands = pytest
allowlist_externals = ["pytest"]
deps = -e .
pytest

[testenv:mypy]
runner = uv-venv-runner
with_dev = true
deps = .

commands = mypy --exclude=build --exclude=dist .
[testenv:pyright]
commands = pyright src tests
runner = uv-venv-runner
basepython = python3.11
with_dev = true
deps = .
types-lxml
pytest

0 comments on commit df2c3fb

Please sign in to comment.