Skip to content

Commit 5d75512

Browse files
committed
Use Ruff, fix various tests
1 parent e8092d6 commit 5d75512

12 files changed

+144
-106
lines changed

.github/workflows/publish.yml

+24-24
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ jobs:
1010
name: Build and publish Python distributions to PyPI and TestPyPI
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
14-
- name: Set up Python 3.10
15-
uses: actions/setup-python@v4
16-
with:
17-
python-version: "3.10"
18-
- name: Install pypa/build
19-
run: >-
20-
python -m
21-
pip install
22-
build
23-
--user
24-
- name: Build a binary wheel and a source tarball
25-
run: >-
26-
python -m
27-
build
28-
--sdist
29-
--wheel
30-
--outdir dist/
31-
.
32-
- name: Publish distribution to PyPI
33-
if: startsWith(github.ref, 'refs/tags')
34-
uses: pypa/gh-action-pypi-publish@master
35-
with:
36-
password: ${{ secrets.PYPI_API_TOKEN }}
13+
- uses: actions/checkout@v3
14+
- name: Set up Python 3.10
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: "3.10"
18+
- name: Install pypa/build
19+
run: >-
20+
python -m
21+
pip install
22+
build
23+
--user
24+
- name: Build a binary wheel and a source tarball
25+
run: >-
26+
python -m
27+
build
28+
--sdist
29+
--wheel
30+
--outdir dist/
31+
.
32+
- name: Publish distribution to PyPI
33+
if: startsWith(github.ref, 'refs/tags')
34+
uses: pypa/gh-action-pypi-publish@master
35+
with:
36+
password: ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/test.yml

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ jobs:
1212
python-version: [3.7, 3.8, 3.9, "3.10", "3.11", "3.12"]
1313

1414
steps:
15-
- uses: actions/checkout@v3
16-
- name: Set up Python ${{ matrix.python-version }}
17-
uses: actions/setup-python@v4
18-
with:
19-
python-version: ${{ matrix.python-version }}
20-
allow-prereleases: true
21-
- name: Install dependencies
22-
run: |
23-
python -m pip install --upgrade pip
24-
pip install tox tox-gh-actions
25-
- name: Test with tox
26-
run: tox
15+
- uses: actions/checkout@v3
16+
- name: Set up Python ${{ matrix.python-version }}
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: ${{ matrix.python-version }}
20+
allow-prereleases: true
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install tox tox-gh-actions
25+
- name: Test with tox
26+
run: tox

.pre-commit-config.yaml

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.2.1
4+
hooks:
5+
- id: ruff
6+
args: [--fix]
7+
28
- repo: https://github.com/psf/black
3-
rev: 24.1.1
9+
rev: 24.2.0
410
hooks:
511
- id: black
6-
language_version: python3.9
7-
8-
ci:
9-
autofix_commit_msg: '[pre-commit.ci] auto fixes from pre-commit.com hooks'
10-
autofix_prs: true
11-
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
12-
autoupdate_schedule: weekly
13-
skip: []
14-
submodules: false
12+
language_version: python3.12
13+
14+
- repo: https://github.com/pre-commit/mirrors-mypy
15+
rev: v1.8.0
16+
hooks:
17+
- id: mypy
18+
additional_dependencies:
19+
- types-requests
20+
- aiohttp
21+
exclude: ^(typeshed_client/typeshed|tests/(site-packages|typeshed))/
22+
23+
- repo: https://github.com/pre-commit/mirrors-prettier
24+
rev: v4.0.0-alpha.8
25+
hooks:
26+
- id: prettier
27+
28+
- repo: https://github.com/pre-commit/pre-commit-hooks
29+
rev: v4.5.0
30+
hooks:
31+
- id: end-of-file-fixer
32+
- id: trailing-whitespace

pyproject.toml

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.black]
2-
target_version = ['py37', 'py38', 'py39', 'py310', 'py311']
2+
target_version = ['py38', 'py39', 'py310', 'py311', 'py312']
33
include = '\.pyi?$'
44
skip-magic-trailing-comma = true
55
preview = true
@@ -50,7 +50,41 @@ disallow_any_decorated = true
5050

5151
exclude = [
5252
"typeshed_client/typeshed",
53+
"tests/site-packages",
5354
"tests/typeshed",
5455
"build/",
56+
"thirdparty",
5557
".tox/",
5658
]
59+
60+
[tool.ruff]
61+
62+
line-length = 100
63+
target-version = "py38"
64+
65+
exclude = [
66+
"typeshed_client/typeshed",
67+
"tests/typeshed",
68+
"build/",
69+
".tox/",
70+
]
71+
72+
[tool.ruff.lint]
73+
select = [
74+
"F",
75+
"E",
76+
"I", # import sorting
77+
"ANN", # enforce type annotations
78+
"C4", # flake8-comprehensions
79+
"B", # bugbear
80+
"SIM", # simplify
81+
"UP", # pyupgrade
82+
"PIE",
83+
"PERF",
84+
]
85+
86+
ignore = [
87+
"ANN101", # missing type annotation for self in method
88+
"ANN102", # missing type annotation for cls in classmethod
89+
"SIM105", # I don't like contextlib.suppress
90+
]

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import ast
22
import os
3-
from pathlib import Path
43
import re
4+
from pathlib import Path
55
from typing import Iterable
6-
from setuptools import setup
76

7+
from setuptools import setup
88

99
current_dir = Path(__file__).parent.resolve()
1010
ts_client_dir = current_dir / "typeshed_client"

tests/test.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
from pathlib import Path
21
import ast
2+
import unittest
3+
from pathlib import Path
4+
from typing import Any, Optional, Set, Type
5+
from unittest import mock
6+
37
import typeshed_client
48
from typeshed_client.finder import (
5-
get_search_context,
9+
ModulePath,
610
PythonVersion,
7-
get_stub_file,
811
SearchContext,
9-
ModulePath,
12+
get_search_context,
13+
get_stub_file,
1014
)
1115
from typeshed_client.parser import get_stub_names
12-
from typing import Any, Set, Type, Optional
13-
from unittest import mock
14-
import unittest
1516

1617
TEST_TYPESHED = Path(__file__).parent / "typeshed"
1718
PACKAGES = Path(__file__).parent / "site-packages"
@@ -334,7 +335,12 @@ def test(self) -> None:
334335
ctx = get_search_context(raise_on_warnings=True)
335336
for module_name, module_path in typeshed_client.get_all_stub_files(ctx):
336337
with self.subTest(path=module_name):
337-
ast = typeshed_client.get_stub_ast(module_name, search_context=ctx)
338+
try:
339+
ast = typeshed_client.get_stub_ast(module_name, search_context=ctx)
340+
except SyntaxError:
341+
# idlelib for some reason ships an example stub file with a syntax error.
342+
# typeshed-client should also throw a SyntaxError in this case.
343+
pass
338344
assert ast is not None
339345
is_init = module_path.name == "__init__.pyi"
340346
typeshed_client.parser.parse_ast(

tox.ini

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ commands =
99

1010
[testenv:black]
1111
deps =
12-
black == 24.1.1
12+
black == 24.2.0
1313
commands =
1414
black --check .
1515

@@ -22,9 +22,8 @@ commands =
2222

2323
[gh-actions]
2424
python =
25-
3.7: py37
2625
3.8: py38
2726
3.9: py39
2827
3.10: py310
29-
3.11: py311, black, mypy
30-
3.12: py312
28+
3.11: py311
29+
3.12: py312, black, mypy

typeshed_client/__init__.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
"""Package for retrieving data from typeshed."""
22

3-
from . import finder
4-
from . import parser
5-
from . import resolver
6-
73
# Exported names
84
from .finder import (
9-
get_stub_ast,
10-
get_stub_file,
5+
ModulePath,
6+
SearchContext,
117
get_all_stub_files,
128
get_search_context,
13-
SearchContext,
14-
ModulePath,
9+
get_stub_ast,
10+
get_stub_file,
1511
)
1612
from .parser import (
17-
get_stub_names,
18-
parse_ast,
1913
ImportedName,
2014
NameDict,
2115
NameInfo,
2216
OverloadedName,
17+
get_stub_names,
18+
parse_ast,
2319
)
2420
from .resolver import ImportedInfo, Resolver
2521

26-
2722
__version__ = "2.4.0"
2823

2924

typeshed_client/finder.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""This module is responsible for finding stub files."""
22

3-
from functools import lru_cache
4-
import importlib_resources
3+
import ast
54
import json
65
import os
7-
from pathlib import Path
86
import subprocess
97
import sys
10-
import ast
8+
from functools import lru_cache
9+
from pathlib import Path
1110
from typing import (
1211
TYPE_CHECKING,
1312
Dict,
@@ -22,6 +21,8 @@
2221
Tuple,
2322
)
2423

24+
import importlib_resources
25+
2526
PythonVersion = Tuple[int, int]
2627
ModulePath = NewType("ModulePath", Tuple[str, ...])
2728

@@ -207,7 +208,7 @@ def _get_all_stub_files_from_directory(
207208
return new_seen
208209

209210

210-
@lru_cache()
211+
@lru_cache
211212
def get_search_path(typeshed_dir: Path, pyversion: Tuple[int, int]) -> Tuple[Path, ...]:
212213
# mirrors default_lib_path in mypy/build.py
213214
path: List[Path] = []
@@ -275,7 +276,7 @@ class _VersionData(NamedTuple):
275276
in_python2: bool
276277

277278

278-
@lru_cache()
279+
@lru_cache
279280
def get_typeshed_versions(typeshed: Path) -> Dict[str, _VersionData]:
280281
versions = {}
281282
try:
@@ -293,10 +294,7 @@ def get_typeshed_versions(typeshed: Path) -> Dict[str, _VersionData]:
293294
else:
294295
min_version_str = version
295296
max_version_str = None
296-
if max_version_str:
297-
max_version = _parse_version(max_version_str)
298-
else:
299-
max_version = None
297+
max_version = _parse_version(max_version_str) if max_version_str else None
300298
min_version = _parse_version(min_version_str)
301299
python2_only = module in python2_files or module + ".pyi" in python2_files
302300
versions[module] = _VersionData(min_version, max_version, python2_only)

0 commit comments

Comments
 (0)