Skip to content

Commit

Permalink
Use blib2to3 parser to support match statement
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nrser committed Feb 26, 2023
1 parent 6569b0e commit 7412f63
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 66 deletions.
3 changes: 2 additions & 1 deletion docspec-python/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "docspec-python"
version = "2.0.2"
version = "2.0.2+blib2to3"
description = "A parser based on lib2to3 producing docspec data from Python source code."
authors = ["Niklas Rosenstein <rosensteinniklas@gmail.com>"]
license = "MIT"
Expand All @@ -12,6 +12,7 @@ packages = [{ include = "docspec_python", from="src" }]
python = "^3.7"
docspec = "^2.0.2"
"nr.util" = ">=0.7.0"
black = "^23.1.0"

[tool.poetry.dev-dependencies]
mypy = "*"
Expand Down
12 changes: 6 additions & 6 deletions docspec-python/src/docspec_python/__init__.py
Expand Up @@ -46,10 +46,10 @@


def load_python_modules(
modules: t.Sequence[str] = None,
packages: t.Sequence[str] = None,
search_path: t.Sequence[t.Union[str, Path]] = None,
options: ParserOptions = None,
modules: t.Optional[t.Sequence[str]] = None,
packages: t.Optional[t.Sequence[str]] = None,
search_path: t.Optional[t.Sequence[t.Union[str, Path]]] = None,
options: t.Optional[ParserOptions] = None,
raise_: bool = True,
encoding: t.Optional[str] = None,
) -> t.Iterable[Module]:
Expand Down Expand Up @@ -133,7 +133,7 @@ def parse_python_module( # type: ignore
return parser.parse(ast, filename, module_name)


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

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

0 comments on commit 7412f63

Please sign in to comment.