Skip to content

Commit 1db8d16

Browse files
Use blib2to3 parser from black package to support match statement (#80)
* Use blib2to3 parser to support match statement The built-in lib2to3 does not support pattern matching (Python 3.10+): https://docs.python.org/3.11/library/2to3.html#module-lib2to3 The [black][] project managed to get some level of parsing support for `match` out of their modified version `blib2to3`, see: 1. psf/black#2242 2. psf/black#2586 [black]: https://github.com/psf/black This change adds `black` as a dependency and switches to using `blib2to3` to parse. Tests pass, but that's all that's been attempted thus far. * Add a _unreleased changelog for blib2to3 integration * fix mypy in docspec/src/docspec/__init__.py * fix mypy * update changelog format * update GitHub workflow * fix workflow * insert PR url * use `--no-venv-check` also for `slap run` in docs job --------- Co-authored-by: Niklas Rosenstein <[email protected]>
1 parent 6569b0e commit 1db8d16

File tree

7 files changed

+365
-77
lines changed

7 files changed

+365
-77
lines changed

.github/workflows/python.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
python-version: ["3.7", "3.8", "3.9", "3.10", "3.x"]
1818
project: ["docspec", "docspec-python"]
1919
steps:
20-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v3
2121
- uses: NiklasRosenstein/slap@gha/install/v1
22-
- uses: actions/setup-python@v2
22+
- uses: actions/setup-python@v3
2323
with: { python-version: "${{ matrix.python-version }}" }
2424
- run: slap install --only ${{ matrix.project }} --no-venv-check -v
2525
- run: DOCSPEC_TEST_NO_DEVELOP=true slap test ${{ matrix.project }}
@@ -28,16 +28,16 @@ jobs:
2828
runs-on: ubuntu-latest
2929
if: github.event_name == 'pull_request'
3030
steps:
31-
- uses: actions/checkout@v2
32-
- uses: NiklasRosenstein/slap@gha/changelog/update/v1
31+
- uses: actions/checkout@v3
32+
- uses: NiklasRosenstein/slap@gha/changelog/update/v2
3333

3434
docs:
3535
runs-on: ubuntu-latest
3636
steps:
37-
- uses: actions/checkout@v2
37+
- uses: actions/checkout@v3
3838
- uses: NiklasRosenstein/slap@gha/install/v1
39-
- run: slap install --only-extras docs --no-venv-check
40-
- run: slap run docs:build
39+
- run: slap install --no-venv-check --only-extras docs
40+
- run: slap run --no-venv-check docs:build
4141
- uses: JamesIves/[email protected]
4242
if: github.ref == 'refs/heads/develop'
4343
with: { branch: gh-pages, folder: docs/_site, ssh-key: "${{ secrets.DEPLOY_KEY }}" }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[[entries]]
2+
id = "8628524b-3376-45db-a676-240b00c20d08"
3+
type = "fix"
4+
description = "Swap in `blib2to3` parser (bundled with the `black` package) for the stdlib `lib2to3` module in order to support `match` statements (PEP 634 - Structural Pattern Matching)."
5+
author = "@nrser"
6+
pr = "https://github.com/NiklasRosenstein/docspec/pull/80"

docspec-python/pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "docspec-python"
3-
version = "2.0.2"
3+
version = "2.0.2+blib2to3"
44
description = "A parser based on lib2to3 producing docspec data from Python source code."
55
authors = ["Niklas Rosenstein <[email protected]>"]
66
license = "MIT"
@@ -12,6 +12,7 @@ packages = [{ include = "docspec_python", from="src" }]
1212
python = "^3.7"
1313
docspec = "^2.0.2"
1414
"nr.util" = ">=0.7.0"
15+
black = "^23.1.0"
1516

1617
[tool.poetry.dev-dependencies]
1718
mypy = "*"
@@ -28,6 +29,10 @@ typed = true
2829
pytest = "pytest test/ -vv"
2930
mypy = "mypy src/ test/ --check-untyped-defs"
3031

32+
[[tool.mypy.overrides]]
33+
module = "blib2to3.*"
34+
ignore_missing_imports = true
35+
3136
[build-system]
3237
requires = ["poetry-core"]
3338
build-backend = "poetry.core.masonry.api"

docspec-python/src/docspec_python/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646

4747

4848
def load_python_modules(
49-
modules: t.Sequence[str] = None,
50-
packages: t.Sequence[str] = None,
51-
search_path: t.Sequence[t.Union[str, Path]] = None,
52-
options: ParserOptions = None,
49+
modules: t.Optional[t.Sequence[str]] = None,
50+
packages: t.Optional[t.Sequence[str]] = None,
51+
search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None,
52+
options: t.Optional[ParserOptions] = None,
5353
raise_: bool = True,
5454
encoding: t.Optional[str] = None,
5555
) -> t.Iterable[Module]:
@@ -133,7 +133,7 @@ def parse_python_module( # type: ignore
133133
return parser.parse(ast, filename, module_name)
134134

135135

136-
def find_module(module_name: str, search_path: t.Sequence[t.Union[str, Path]] = None) -> str:
136+
def find_module(module_name: str, search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None) -> str:
137137
""" Finds the filename of a module that can be parsed with #parse_python_module(). If *search_path* is not set,
138138
the default #sys.path is used to search for the module. If *module_name* is a Python package, it will return the
139139
path to the package's `__init__.py` file. If the module does not exist, an #ImportError is raised. This is also
@@ -165,7 +165,7 @@ def find_module(module_name: str, search_path: t.Sequence[t.Union[str, Path]] =
165165

166166
def iter_package_files(
167167
package_name: str,
168-
search_path: t.Sequence[t.Union[str, Path]] = None,
168+
search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None,
169169
) -> t.Iterable[t.Tuple[str, str]]:
170170
""" Returns an iterator for the Python source files in the specified package. The items returned
171171
by the iterator are tuples of the module name and filename. Supports a PEP 420 namespace package

0 commit comments

Comments
 (0)