parse-py3 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- parse_py3-0.1.0/LICENSE +22 -0
- parse_py3-0.1.0/PKG-INFO +37 -0
- parse_py3-0.1.0/README.md +25 -0
- parse_py3-0.1.0/pyproject.toml +21 -0
- parse_py3-0.1.0/pyproject.toml.orig +21 -0
- parse_py3-0.1.0/src/parse_py3/__init__.py +27 -0
- parse_py3-0.1.0/src/parse_py3/py.typed +0 -0
- parse_py3-0.1.0/src/parse_py3/source.py +181 -0
- parse_py3-0.1.0/tests/test_source.py +35 -0
parse_py3-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 prettynb
|
|
4
|
+
Copyright (c) 2026 wispsy
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
parse_py3-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: parse-py3
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Typed tokens, syntax trees, and import sites for Python source
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Project-URL: Source, https://github.com/wispsy/parse-py3
|
|
9
|
+
Project-URL: Issues, https://github.com/wispsy/parse-py3/issues
|
|
10
|
+
Project-URL: Project, https://github.com/orgs/wispsy/projects/23
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# parse-py3
|
|
14
|
+
|
|
15
|
+
Native Python source analysis with typed tokens, ASTs, diagnostics, and import
|
|
16
|
+
sites. Requires Python 3.12 or newer and uses only the standard library.
|
|
17
|
+
|
|
18
|
+
from parse_py3 import absolute_module, parse
|
|
19
|
+
|
|
20
|
+
result = parse("from .helpers import run\n")
|
|
21
|
+
site, = result.imports
|
|
22
|
+
assert absolute_module(site, package="example.tools") == "example.tools.helpers"
|
|
23
|
+
|
|
24
|
+
The result retains decoded source text and the native AST. Tokens preserve the
|
|
25
|
+
corresponding source slices, including original newline spellings. Spans are
|
|
26
|
+
half-open, with one-based lines and zero-based byte columns and absolute offsets
|
|
27
|
+
in the UTF-8 encoding of the decoded source. Byte input honors Python encoding
|
|
28
|
+
declarations. Source without a valid syntax tree returns a diagnostic.
|
|
29
|
+
|
|
30
|
+
Import sites include aliases and relative levels from every static import
|
|
31
|
+
statement, including nested and conditional statements. Resolution uses
|
|
32
|
+
explicit package names. Filesystem lookup, dependency policy, compiler profiles,
|
|
33
|
+
and runtime import tracing belong to callers. Use check_compile=True for scope
|
|
34
|
+
checks that CPython performs after parsing. Neither mode executes source code.
|
|
35
|
+
|
|
36
|
+
The [initial source contract](https://github.com/wispsy/parse-py3/issues/1) is
|
|
37
|
+
tracked in [Wispsy Composition](https://github.com/orgs/wispsy/projects/23).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# parse-py3
|
|
2
|
+
|
|
3
|
+
Native Python source analysis with typed tokens, ASTs, diagnostics, and import
|
|
4
|
+
sites. Requires Python 3.12 or newer and uses only the standard library.
|
|
5
|
+
|
|
6
|
+
from parse_py3 import absolute_module, parse
|
|
7
|
+
|
|
8
|
+
result = parse("from .helpers import run\n")
|
|
9
|
+
site, = result.imports
|
|
10
|
+
assert absolute_module(site, package="example.tools") == "example.tools.helpers"
|
|
11
|
+
|
|
12
|
+
The result retains decoded source text and the native AST. Tokens preserve the
|
|
13
|
+
corresponding source slices, including original newline spellings. Spans are
|
|
14
|
+
half-open, with one-based lines and zero-based byte columns and absolute offsets
|
|
15
|
+
in the UTF-8 encoding of the decoded source. Byte input honors Python encoding
|
|
16
|
+
declarations. Source without a valid syntax tree returns a diagnostic.
|
|
17
|
+
|
|
18
|
+
Import sites include aliases and relative levels from every static import
|
|
19
|
+
statement, including nested and conditional statements. Resolution uses
|
|
20
|
+
explicit package names. Filesystem lookup, dependency policy, compiler profiles,
|
|
21
|
+
and runtime import tracing belong to callers. Use check_compile=True for scope
|
|
22
|
+
checks that CPython performs after parsing. Neither mode executes source code.
|
|
23
|
+
|
|
24
|
+
The [initial source contract](https://github.com/wispsy/parse-py3/issues/1) is
|
|
25
|
+
tracked in [Wispsy Composition](https://github.com/orgs/wispsy/projects/23).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "parse-py3"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Typed tokens, syntax trees, and import sites for Python source"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = []
|
|
10
|
+
|
|
11
|
+
[project.urls]
|
|
12
|
+
Source = "https://github.com/wispsy/parse-py3"
|
|
13
|
+
Issues = "https://github.com/wispsy/parse-py3/issues"
|
|
14
|
+
Project = "https://github.com/orgs/wispsy/projects/23"
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
18
|
+
build-backend = "uv_build"
|
|
19
|
+
|
|
20
|
+
[tool.uv.build-backend]
|
|
21
|
+
source-include = ["tests/*.py"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "parse-py3"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Typed tokens, syntax trees, and import sites for Python source"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.12"
|
|
9
|
+
dependencies = []
|
|
10
|
+
|
|
11
|
+
[project.urls]
|
|
12
|
+
Source = "https://github.com/wispsy/parse-py3"
|
|
13
|
+
Issues = "https://github.com/wispsy/parse-py3/issues"
|
|
14
|
+
Project = "https://github.com/orgs/wispsy/projects/23"
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
18
|
+
build-backend = "uv_build"
|
|
19
|
+
|
|
20
|
+
[tool.uv.build-backend]
|
|
21
|
+
source-include = ["tests/*.py"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Native Python source analysis without importing the inspected code."""
|
|
2
|
+
|
|
3
|
+
from .source import (
|
|
4
|
+
Alias,
|
|
5
|
+
Diagnostic,
|
|
6
|
+
ImportSite,
|
|
7
|
+
ParseResult,
|
|
8
|
+
Position,
|
|
9
|
+
Span,
|
|
10
|
+
Token,
|
|
11
|
+
absolute_module,
|
|
12
|
+
parse,
|
|
13
|
+
parse_file,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"Alias",
|
|
18
|
+
"Diagnostic",
|
|
19
|
+
"ImportSite",
|
|
20
|
+
"ParseResult",
|
|
21
|
+
"Position",
|
|
22
|
+
"Span",
|
|
23
|
+
"Token",
|
|
24
|
+
"absolute_module",
|
|
25
|
+
"parse",
|
|
26
|
+
"parse_file",
|
|
27
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"""Native Python source analysis and its coordinate contract."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ast
|
|
6
|
+
import io
|
|
7
|
+
import re
|
|
8
|
+
import token
|
|
9
|
+
import tokenize
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from importlib.util import resolve_name
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Literal
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True, order=True)
|
|
17
|
+
class Position:
|
|
18
|
+
line: int
|
|
19
|
+
byte_column: int
|
|
20
|
+
byte_offset: int
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True)
|
|
24
|
+
class Span:
|
|
25
|
+
start: Position
|
|
26
|
+
end: Position
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class Token:
|
|
31
|
+
kind: str
|
|
32
|
+
text: str
|
|
33
|
+
span: Span
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class Diagnostic:
|
|
38
|
+
code: str
|
|
39
|
+
message: str
|
|
40
|
+
span: Span
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class Alias:
|
|
45
|
+
name: str
|
|
46
|
+
asname: str | None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class ImportSite:
|
|
51
|
+
kind: Literal["import", "from"]
|
|
52
|
+
module: str | None
|
|
53
|
+
level: int
|
|
54
|
+
names: tuple[Alias, ...]
|
|
55
|
+
span: Span
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True)
|
|
59
|
+
class ParseResult:
|
|
60
|
+
filename: str
|
|
61
|
+
source: str
|
|
62
|
+
tree: ast.Module | None
|
|
63
|
+
tokens: tuple[Token, ...] = ()
|
|
64
|
+
imports: tuple[ImportSite, ...] = ()
|
|
65
|
+
diagnostics: tuple[Diagnostic, ...] = ()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _Coordinates:
|
|
69
|
+
def __init__(self, source: str):
|
|
70
|
+
self.source = source.encode("utf-8")
|
|
71
|
+
self.lines = re.split(r"(?<=\n)|(?<=\r)(?!\n)", source)
|
|
72
|
+
self.offsets = [0]
|
|
73
|
+
for line in self.lines:
|
|
74
|
+
self.offsets.append(self.offsets[-1] + len(line.encode("utf-8")))
|
|
75
|
+
|
|
76
|
+
def position(
|
|
77
|
+
self, line: int, column: int, *, byte_column: bool = False
|
|
78
|
+
) -> Position:
|
|
79
|
+
line = max(1, line)
|
|
80
|
+
column = max(0, column)
|
|
81
|
+
if line > len(self.lines):
|
|
82
|
+
return Position(line, 0, len(self.source))
|
|
83
|
+
if not byte_column:
|
|
84
|
+
column = len(self.lines[line - 1][:column].encode("utf-8"))
|
|
85
|
+
column = min(column, self.offsets[line] - self.offsets[line - 1])
|
|
86
|
+
return Position(line, column, self.offsets[line - 1] + column)
|
|
87
|
+
|
|
88
|
+
def span(self, start, end, *, byte_column=False) -> Span:
|
|
89
|
+
beginning = self.position(*start, byte_column=byte_column)
|
|
90
|
+
ending = self.position(*end, byte_column=byte_column)
|
|
91
|
+
return Span(beginning, max(beginning, ending))
|
|
92
|
+
|
|
93
|
+
def token(self, item: tokenize.TokenInfo) -> Token:
|
|
94
|
+
span = self.span(item.start, item.end)
|
|
95
|
+
text = self.source[span.start.byte_offset : span.end.byte_offset].decode(
|
|
96
|
+
"utf-8"
|
|
97
|
+
)
|
|
98
|
+
return Token(token.tok_name[item.type], text, span)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _import_sites(
|
|
102
|
+
tree: ast.Module, coordinates: _Coordinates
|
|
103
|
+
) -> tuple[ImportSite, ...]:
|
|
104
|
+
sites = []
|
|
105
|
+
for node in ast.walk(tree):
|
|
106
|
+
if not isinstance(node, (ast.Import, ast.ImportFrom)):
|
|
107
|
+
continue
|
|
108
|
+
span = coordinates.span(
|
|
109
|
+
(node.lineno, node.col_offset),
|
|
110
|
+
(node.end_lineno or node.lineno, node.end_col_offset or node.col_offset),
|
|
111
|
+
byte_column=True,
|
|
112
|
+
)
|
|
113
|
+
aliases = tuple(Alias(alias.name, alias.asname) for alias in node.names)
|
|
114
|
+
if isinstance(node, ast.ImportFrom):
|
|
115
|
+
sites.append(ImportSite("from", node.module, node.level, aliases, span))
|
|
116
|
+
else:
|
|
117
|
+
sites.append(ImportSite("import", None, 0, aliases, span))
|
|
118
|
+
return tuple(sorted(sites, key=lambda site: site.span.start))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def absolute_module(site: ImportSite, *, package: str) -> str | None:
|
|
122
|
+
"""Resolve a from-import name using an explicit containing package."""
|
|
123
|
+
if site.kind == "import":
|
|
124
|
+
raise ValueError("Plain imports have one absolute name per alias")
|
|
125
|
+
if not site.level:
|
|
126
|
+
return site.module
|
|
127
|
+
return resolve_name("." * site.level + (site.module or ""), package)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def parse(
|
|
131
|
+
source: str | bytes,
|
|
132
|
+
*,
|
|
133
|
+
filename: str = "<source>",
|
|
134
|
+
feature_version: int | None = None,
|
|
135
|
+
check_compile: bool = False,
|
|
136
|
+
) -> ParseResult:
|
|
137
|
+
"""Parse source and report diagnostics with half-open UTF-8 spans."""
|
|
138
|
+
if isinstance(source, bytes):
|
|
139
|
+
try:
|
|
140
|
+
encoding, _ = tokenize.detect_encoding(io.BytesIO(source).readline)
|
|
141
|
+
source = source.decode(encoding)
|
|
142
|
+
except (SyntaxError, UnicodeError, LookupError) as error:
|
|
143
|
+
beginning = Position(1, 0, 0)
|
|
144
|
+
diagnostic = Diagnostic("PY002", str(error), Span(beginning, beginning))
|
|
145
|
+
return ParseResult(filename, "", None, diagnostics=(diagnostic,))
|
|
146
|
+
coordinates = _Coordinates(source)
|
|
147
|
+
try:
|
|
148
|
+
tree = ast.parse(source, filename=filename, feature_version=feature_version)
|
|
149
|
+
if check_compile:
|
|
150
|
+
compile(tree, filename, "exec", dont_inherit=True)
|
|
151
|
+
except SyntaxError as error:
|
|
152
|
+
line = error.lineno or 1
|
|
153
|
+
offset = error.offset or 1
|
|
154
|
+
span = coordinates.span(
|
|
155
|
+
(line, offset - 1),
|
|
156
|
+
(error.end_lineno or line, (error.end_offset or offset) - 1),
|
|
157
|
+
)
|
|
158
|
+
return ParseResult(
|
|
159
|
+
filename, source, None, diagnostics=(Diagnostic("PY001", error.msg, span),)
|
|
160
|
+
)
|
|
161
|
+
tokens = tuple(
|
|
162
|
+
coordinates.token(item)
|
|
163
|
+
for item in tokenize.generate_tokens(io.StringIO(source, newline="").readline)
|
|
164
|
+
)
|
|
165
|
+
return ParseResult(filename, source, tree, tokens, _import_sites(tree, coordinates))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def parse_file(
|
|
169
|
+
path: str | Path,
|
|
170
|
+
*,
|
|
171
|
+
feature_version: int | None = None,
|
|
172
|
+
check_compile: bool = False,
|
|
173
|
+
) -> ParseResult:
|
|
174
|
+
"""Read Python source using its encoding declaration or UTF-8 default."""
|
|
175
|
+
source = Path(path)
|
|
176
|
+
return parse(
|
|
177
|
+
source.read_bytes(),
|
|
178
|
+
filename=str(source),
|
|
179
|
+
feature_version=feature_version,
|
|
180
|
+
check_compile=check_compile,
|
|
181
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from parse_py3 import absolute_module, parse
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SourceTests(unittest.TestCase):
|
|
8
|
+
def test_unicode_crlf_spans_and_nested_imports(self):
|
|
9
|
+
source = (
|
|
10
|
+
"é = 1; import math as m\r\nif False:\r\n from .helpers import run\r\n"
|
|
11
|
+
)
|
|
12
|
+
result = parse(source)
|
|
13
|
+
self.assertIsInstance(result.tree, ast.Module)
|
|
14
|
+
self.assertEqual(len(result.imports), 2)
|
|
15
|
+
first, second = result.imports
|
|
16
|
+
self.assertEqual(first.span.start.byte_column, 8)
|
|
17
|
+
self.assertEqual((first.names[0].name, first.names[0].asname), ("math", "m"))
|
|
18
|
+
self.assertEqual(absolute_module(second, package="toolkit"), "toolkit.helpers")
|
|
19
|
+
self.assertIn("\r\n", [item.text for item in result.tokens])
|
|
20
|
+
|
|
21
|
+
def test_encoding_and_syntax_distinctions(self):
|
|
22
|
+
result = parse(b"# coding: latin-1\nname = '\xe9'\n")
|
|
23
|
+
self.assertIn("'é'", result.source)
|
|
24
|
+
self.assertEqual(parse(b"# coding: missing\n").diagnostics[0].code, "PY002")
|
|
25
|
+
self.assertIsNotNone(parse("return 1").tree)
|
|
26
|
+
self.assertEqual(
|
|
27
|
+
parse("return 1", check_compile=True).diagnostics[0].code, "PY001"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
def test_static_inspection_does_not_execute_source(self):
|
|
31
|
+
result = parse("raise RuntimeError('unused')\nfrom .. import feature\n")
|
|
32
|
+
self.assertEqual(
|
|
33
|
+
absolute_module(result.imports[0], package="app.plugins"), "app"
|
|
34
|
+
)
|
|
35
|
+
self.assertFalse(result.diagnostics)
|