token-utils 0.3.2__tar.gz → 0.3.4__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.
Files changed (161) hide show
  1. {token_utils-0.3.2 → token_utils-0.3.4}/PKG-INFO +2 -12
  2. {token_utils-0.3.2 → token_utils-0.3.4}/README.md +1 -11
  3. token_utils-0.3.4/docs_examples/add_decorator.py +18 -0
  4. token_utils-0.3.4/docs_examples/inside_blocks.py +21 -0
  5. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/nobreak.py +1 -1
  6. token_utils-0.3.4/docs_examples/view_stack.py +16 -0
  7. {token_utils-0.3.2 → token_utils-0.3.4}/pyproject.toml +20 -1
  8. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/__about__.py +1 -1
  9. token_utils-0.3.4/src/token_utils/__about__.pyi +4 -0
  10. token_utils-0.3.4/src/token_utils/__init__.py +43 -0
  11. token_utils-0.3.4/src/token_utils/__init__.pyi +42 -0
  12. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/_py_tokenize.py +22 -5
  13. token_utils-0.3.4/src/token_utils/_py_tokenize.pyi +66 -0
  14. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/_token_plus.py +8 -5
  15. token_utils-0.3.4/src/token_utils/_token_plus.pyi +11 -0
  16. token_utils-0.3.4/src/token_utils/custom_warnings.pyi +14 -0
  17. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/token_class.py +24 -13
  18. token_utils-0.3.4/src/token_utils/token_class.pyi +69 -0
  19. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/tokenizing.py +10 -14
  20. token_utils-0.3.4/src/token_utils/tokenizing.pyi +29 -0
  21. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/utils.py +58 -18
  22. token_utils-0.3.4/src/token_utils/utils.pyi +33 -0
  23. {token_utils-0.3.2 → token_utils-0.3.4}/tests/test_utils.py +7 -1
  24. token_utils-0.3.2/.flake8 +0 -14
  25. token_utils-0.3.2/.idea/.gitignore +0 -5
  26. token_utils-0.3.2/.idea/inspectionProfiles/profiles_settings.xml +0 -6
  27. token_utils-0.3.2/.idea/modules.xml +0 -8
  28. token_utils-0.3.2/.idea/token-utils.iml +0 -11
  29. token_utils-0.3.2/.idea/vcs.xml +0 -6
  30. token_utils-0.3.2/.idea/workspace.xml +0 -67
  31. token_utils-0.3.2/.nojekyll +0 -0
  32. token_utils-0.3.2/ae.bat +0 -45
  33. token_utils-0.3.2/docs/Makefile +0 -20
  34. token_utils-0.3.2/docs/doctrees/about_tokens.doctree +0 -0
  35. token_utils-0.3.2/docs/doctrees/environment.pickle +0 -0
  36. token_utils-0.3.2/docs/doctrees/index.doctree +0 -0
  37. token_utils-0.3.2/docs/doctrees/tips.doctree +0 -0
  38. token_utils-0.3.2/docs/doctrees/todo.doctree +0 -0
  39. token_utils-0.3.2/docs/doctrees/token_class.doctree +0 -0
  40. token_utils-0.3.2/docs/doctrees/tokenizing_methods.doctree +0 -0
  41. token_utils-0.3.2/docs/doctrees/tutorial_prelude.doctree +0 -0
  42. token_utils-0.3.2/docs/doctrees/untokenizing.doctree +0 -0
  43. token_utils-0.3.2/docs/doctrees/utils.doctree +0 -0
  44. token_utils-0.3.2/docs/html/.buildinfo +0 -4
  45. token_utils-0.3.2/docs/html/.buildinfo.bak +0 -4
  46. token_utils-0.3.2/docs/html/.nojekyll +0 -0
  47. token_utils-0.3.2/docs/html/_modules/index.html +0 -116
  48. token_utils-0.3.2/docs/html/_modules/token_utils/custom_warnings.html +0 -154
  49. token_utils-0.3.2/docs/html/_modules/token_utils/token_class.html +0 -592
  50. token_utils-0.3.2/docs/html/_modules/token_utils/tokenizing.html +0 -941
  51. token_utils-0.3.2/docs/html/_sources/about_tokens.rst.txt +0 -414
  52. token_utils-0.3.2/docs/html/_sources/index.rst.txt +0 -81
  53. token_utils-0.3.2/docs/html/_sources/tips.rst.txt +0 -211
  54. token_utils-0.3.2/docs/html/_sources/todo.rst.txt +0 -4
  55. token_utils-0.3.2/docs/html/_sources/token_class.rst.txt +0 -21
  56. token_utils-0.3.2/docs/html/_sources/tokenizing_methods.rst.txt +0 -20
  57. token_utils-0.3.2/docs/html/_sources/tutorial_prelude.rst.txt +0 -151
  58. token_utils-0.3.2/docs/html/_sources/untokenizing.rst.txt +0 -223
  59. token_utils-0.3.2/docs/html/_sources/utils.rst.txt +0 -20
  60. token_utils-0.3.2/docs/html/_static/_sphinx_javascript_frameworks_compat.js +0 -123
  61. token_utils-0.3.2/docs/html/_static/basic.css +0 -914
  62. token_utils-0.3.2/docs/html/_static/css/badge_only.css +0 -1
  63. token_utils-0.3.2/docs/html/_static/css/fonts/Roboto-Slab-Bold.woff +0 -0
  64. token_utils-0.3.2/docs/html/_static/css/fonts/Roboto-Slab-Bold.woff2 +0 -0
  65. token_utils-0.3.2/docs/html/_static/css/fonts/Roboto-Slab-Regular.woff +0 -0
  66. token_utils-0.3.2/docs/html/_static/css/fonts/Roboto-Slab-Regular.woff2 +0 -0
  67. token_utils-0.3.2/docs/html/_static/css/fonts/fontawesome-webfont.eot +0 -0
  68. token_utils-0.3.2/docs/html/_static/css/fonts/fontawesome-webfont.svg +0 -2671
  69. token_utils-0.3.2/docs/html/_static/css/fonts/fontawesome-webfont.ttf +0 -0
  70. token_utils-0.3.2/docs/html/_static/css/fonts/fontawesome-webfont.woff +0 -0
  71. token_utils-0.3.2/docs/html/_static/css/fonts/fontawesome-webfont.woff2 +0 -0
  72. token_utils-0.3.2/docs/html/_static/css/fonts/lato-bold-italic.woff +0 -0
  73. token_utils-0.3.2/docs/html/_static/css/fonts/lato-bold-italic.woff2 +0 -0
  74. token_utils-0.3.2/docs/html/_static/css/fonts/lato-bold.woff +0 -0
  75. token_utils-0.3.2/docs/html/_static/css/fonts/lato-bold.woff2 +0 -0
  76. token_utils-0.3.2/docs/html/_static/css/fonts/lato-normal-italic.woff +0 -0
  77. token_utils-0.3.2/docs/html/_static/css/fonts/lato-normal-italic.woff2 +0 -0
  78. token_utils-0.3.2/docs/html/_static/css/fonts/lato-normal.woff +0 -0
  79. token_utils-0.3.2/docs/html/_static/css/fonts/lato-normal.woff2 +0 -0
  80. token_utils-0.3.2/docs/html/_static/css/theme.css +0 -4
  81. token_utils-0.3.2/docs/html/_static/custom.css +0 -57
  82. token_utils-0.3.2/docs/html/_static/doctools.js +0 -149
  83. token_utils-0.3.2/docs/html/_static/documentation_options.js +0 -13
  84. token_utils-0.3.2/docs/html/_static/file.png +0 -0
  85. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bold.eot +0 -0
  86. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bold.ttf +0 -0
  87. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bold.woff +0 -0
  88. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bold.woff2 +0 -0
  89. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bolditalic.eot +0 -0
  90. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bolditalic.ttf +0 -0
  91. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bolditalic.woff +0 -0
  92. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-bolditalic.woff2 +0 -0
  93. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-italic.eot +0 -0
  94. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-italic.ttf +0 -0
  95. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-italic.woff +0 -0
  96. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-italic.woff2 +0 -0
  97. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-regular.eot +0 -0
  98. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-regular.ttf +0 -0
  99. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-regular.woff +0 -0
  100. token_utils-0.3.2/docs/html/_static/fonts/Lato/lato-regular.woff2 +0 -0
  101. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-bold.eot +0 -0
  102. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-bold.ttf +0 -0
  103. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-bold.woff +0 -0
  104. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-bold.woff2 +0 -0
  105. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-regular.eot +0 -0
  106. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-regular.ttf +0 -0
  107. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-regular.woff +0 -0
  108. token_utils-0.3.2/docs/html/_static/fonts/RobotoSlab/roboto-slab-v7-regular.woff2 +0 -0
  109. token_utils-0.3.2/docs/html/_static/jquery.js +0 -2
  110. token_utils-0.3.2/docs/html/_static/js/badge_only.js +0 -1
  111. token_utils-0.3.2/docs/html/_static/js/theme.js +0 -1
  112. token_utils-0.3.2/docs/html/_static/js/versions.js +0 -228
  113. token_utils-0.3.2/docs/html/_static/language_data.js +0 -192
  114. token_utils-0.3.2/docs/html/_static/minus.png +0 -0
  115. token_utils-0.3.2/docs/html/_static/plus.png +0 -0
  116. token_utils-0.3.2/docs/html/_static/pygments.css +0 -75
  117. token_utils-0.3.2/docs/html/_static/searchtools.js +0 -632
  118. token_utils-0.3.2/docs/html/_static/sphinx-new-tab-link.css +0 -14
  119. token_utils-0.3.2/docs/html/_static/sphinx_highlight.js +0 -154
  120. token_utils-0.3.2/docs/html/about_tokens.html +0 -522
  121. token_utils-0.3.2/docs/html/genindex.html +0 -379
  122. token_utils-0.3.2/docs/html/index.html +0 -323
  123. token_utils-0.3.2/docs/html/objects.inv +0 -0
  124. token_utils-0.3.2/docs/html/py-modindex.html +0 -143
  125. token_utils-0.3.2/docs/html/search.html +0 -133
  126. token_utils-0.3.2/docs/html/searchindex.js +0 -1
  127. token_utils-0.3.2/docs/html/tips.html +0 -386
  128. token_utils-0.3.2/docs/html/todo.html +0 -122
  129. token_utils-0.3.2/docs/html/token_class.html +0 -484
  130. token_utils-0.3.2/docs/html/tokenizing_methods.html +0 -477
  131. token_utils-0.3.2/docs/html/tutorial_prelude.html +0 -269
  132. token_utils-0.3.2/docs/html/untokenizing.html +0 -382
  133. token_utils-0.3.2/docs/html/utils.html +0 -130
  134. token_utils-0.3.2/docs/make.bat +0 -36
  135. token_utils-0.3.2/docs/source/_static/custom.css +0 -57
  136. token_utils-0.3.2/docs/source/about_tokens.rst +0 -414
  137. token_utils-0.3.2/docs/source/index.rst +0 -81
  138. token_utils-0.3.2/docs/source/tips.rst +0 -211
  139. token_utils-0.3.2/docs/source/todo.rst +0 -4
  140. token_utils-0.3.2/docs/source/token_class.rst +0 -21
  141. token_utils-0.3.2/docs/source/tokenizing_methods.rst +0 -20
  142. token_utils-0.3.2/docs/source/tutorial_prelude.rst +0 -151
  143. token_utils-0.3.2/docs/source/untokenizing.rst +0 -223
  144. token_utils-0.3.2/docs_examples/add_decorator.py +0 -32
  145. token_utils-0.3.2/docs_examples/decorator_example.txt +0 -14
  146. token_utils-0.3.2/docs_examples/nobreak.txt +0 -31
  147. token_utils-0.3.2/pypi_upload.bat +0 -13
  148. token_utils-0.3.2/run_all_tests.bat +0 -25
  149. token_utils-0.3.2/src/token_utils/__init__.py +0 -10
  150. {token_utils-0.3.2 → token_utils-0.3.4}/.gitignore +0 -0
  151. {token_utils-0.3.2 → token_utils-0.3.4}/LICENSE +0 -0
  152. {token_utils-0.3.2 → token_utils-0.3.4}/docs/source/conf.py +0 -0
  153. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/decimal_py.py +0 -0
  154. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/decimal_tok.py +0 -0
  155. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/decimal_tok2.py +0 -0
  156. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/factorial_1.py +0 -0
  157. {token_utils-0.3.2 → token_utils-0.3.4}/docs_examples/factorial_2.py +0 -0
  158. {token_utils-0.3.2 → token_utils-0.3.4}/src/token_utils/custom_warnings.py +0 -0
  159. {token_utils-0.3.2 → token_utils-0.3.4}/tests/__init__.py +0 -0
  160. {token_utils-0.3.2 → token_utils-0.3.4}/tests/test_roundtrip.py +0 -0
  161. {token_utils-0.3.2 → token_utils-0.3.4}/tests/test_token_methods.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: token-utils
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Convenient tools to deal with tokens.
5
5
  Project-URL: Documentation, https://aroberge.github.io/token-utils/docs/html/
6
6
  Project-URL: Issues, https://github.com/aroberge/token-utils/issues
@@ -27,19 +27,9 @@ Installation: `pip install token-utils`.
27
27
 
28
28
  The purpose of token-utils is to simplify manipulations of tokens normally
29
29
  obtaind from Python's [tokenize module](https://docs.python.org/3/library/tokenize.html).
30
- One of token-utils' features is that, unlike Python's version, the following
31
- is always guaranteed:
32
-
33
- ```python
34
- from token_utils import tokenize, untokenize
35
-
36
- source = "Arbitrary Python code here"
37
-
38
- assert source == untokenize(tokenize(source))
39
- ```
40
30
 
41
31
  To get an idea of the simplicity of using token-utils,
42
- consider [this example from Python's standard library](https://docs.python.org/3/library/tokenize.html#examples) which substitute Decimals for floats in a string of statements:
32
+ consider [this example from Python's own documentation](https://docs.python.org/3/library/tokenize.html#examples) which substitute Decimals for floats in a string of statements:
43
33
 
44
34
  ```python
45
35
  from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
@@ -4,19 +4,9 @@ Installation: `pip install token-utils`.
4
4
 
5
5
  The purpose of token-utils is to simplify manipulations of tokens normally
6
6
  obtaind from Python's [tokenize module](https://docs.python.org/3/library/tokenize.html).
7
- One of token-utils' features is that, unlike Python's version, the following
8
- is always guaranteed:
9
-
10
- ```python
11
- from token_utils import tokenize, untokenize
12
-
13
- source = "Arbitrary Python code here"
14
-
15
- assert source == untokenize(tokenize(source))
16
- ```
17
7
 
18
8
  To get an idea of the simplicity of using token-utils,
19
- consider [this example from Python's standard library](https://docs.python.org/3/library/tokenize.html#examples) which substitute Decimals for floats in a string of statements:
9
+ consider [this example from Python's own documentation](https://docs.python.org/3/library/tokenize.html#examples) which substitute Decimals for floats in a string of statements:
20
10
 
21
11
  ```python
22
12
  from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
@@ -0,0 +1,18 @@
1
+ # add_decorator.py
2
+
3
+ from token_utils import get_logical_lines, IndentStack, untokenize, stringify
4
+
5
+ with open("decorator_example.txt", "r") as f:
6
+ source = f.read()
7
+
8
+ stack = IndentStack()
9
+ new_lines = []
10
+ for line in get_logical_lines(source):
11
+ stack.update(line)
12
+ first_token = line[0]
13
+ if first_token == "def" and not stack.is_token_in_named_block(first_token, "class"):
14
+ new_lines.append([" " * first_token.indentation() + "@decorator\n"])
15
+
16
+ new_lines.append(line)
17
+
18
+ print(stringify(new_lines))
@@ -0,0 +1,21 @@
1
+ # inside_blocks.py
2
+ from token_utils import get_logical_lines, IndentStack, stringify
3
+
4
+ with open("decorator_example.txt", "r") as f:
5
+ source = f.read()
6
+
7
+ stack = IndentStack()
8
+
9
+ for line in get_logical_lines(source):
10
+ stack.update(line)
11
+ first_token = line[0]
12
+ inside_class = stack.is_token_in_named_block(first_token, "class")
13
+ inside_def = stack.is_token_in_named_block(first_token, "def")
14
+ print(
15
+ f"{stringify(line).rstrip():20}",
16
+ f"{inside_def=};",
17
+ f"{inside_class=};",
18
+ " stack:",
19
+ end=" ",
20
+ )
21
+ stack.print_stack()
@@ -10,7 +10,7 @@ stack.add_same_indent_keyword("nobreak")
10
10
 
11
11
  for line in get_logical_lines(source):
12
12
  top = stack.update(line)
13
- if top is None:
13
+ if not top: # this line does not introduce an indented block
14
14
  new_lines.append(line)
15
15
  continue
16
16
 
@@ -0,0 +1,16 @@
1
+ # view_stack.py
2
+ from token_utils import get_physical_lines, IndentStack, untokenize
3
+
4
+ with open("decorator_example.txt", "r") as f:
5
+ source = f.read()
6
+ stack = IndentStack()
7
+
8
+ # It is highly preferable to use get_logical_lines as this example shows.
9
+ for line in get_physical_lines(source):
10
+ print(
11
+ f"{untokenize(line).rstrip():<25}",
12
+ f".update() --> {str(stack.update(line)):<7}",
13
+ "==> ",
14
+ end=" ",
15
+ )
16
+ stack.print_stack()
@@ -35,4 +35,23 @@ Source = "https://github.com/aroberge/token-utils"
35
35
  path = "src/token_utils/__about__.py"
36
36
 
37
37
  [tool.pytest.ini_options]
38
- filterwarnings = ["ignore"]
38
+ filterwarnings = ["ignore"]
39
+
40
+ [tool.mypy]
41
+ mypy_path = "src/token_utils/"
42
+
43
+ [tool.pyrefly]
44
+ search-path = ["src/token_utils/"]
45
+ preset = "legacy"
46
+ project-excludes = ["venv*", "build/", "docs_examples/"]
47
+
48
+ [tool.pyright]
49
+ include = ["src/token_utils", "tests"]
50
+ stubPath = "."
51
+ extraPaths = ["."]
52
+
53
+ [tool.hatch.build]
54
+ include = [
55
+ "*.py",
56
+ "*.pyi",
57
+ ]
@@ -1,2 +1,2 @@
1
- version = "0.3.2"
1
+ version = "0.3.4"
2
2
  name = "token_utils"
@@ -0,0 +1,4 @@
1
+ from typing import Literal
2
+
3
+ version: Literal['0.3.4'] = "0.3.4"
4
+ name: Literal['token_utils'] = "token_utils"
@@ -0,0 +1,43 @@
1
+ from token_utils.token_class import Token, make_fake_token, add_operator, NULL
2
+ from token_utils.tokenizing import (
3
+ generate_tokens,
4
+ tokenize,
5
+ get_logical_lines,
6
+ get_physical_lines,
7
+ untokenize,
8
+ stringify,
9
+ dedent,
10
+ print_tokens,
11
+ )
12
+
13
+ from token_utils.utils import (
14
+ BracketStack,
15
+ IndentStack,
16
+ pairwise,
17
+ sliding_window,
18
+ split_at_token,
19
+ ) # noqa
20
+ from token_utils.__about__ import version
21
+ from token_utils.custom_warnings import disable_warnings
22
+
23
+ __all__ = [
24
+ "Token",
25
+ "NULL",
26
+ "make_fake_token",
27
+ "add_operator",
28
+ "BracketStack",
29
+ "IndentStack",
30
+ "pairwise",
31
+ "sliding_window",
32
+ "split_at_token",
33
+ "version",
34
+ "disable_warnings",
35
+ "generate_tokens",
36
+ "tokenize",
37
+ "get_physical_lines",
38
+ "get_logical_lines",
39
+ "untokenize",
40
+ "stringify",
41
+ "print_tokens",
42
+ "dedent",
43
+ ]
@@ -0,0 +1,42 @@
1
+ from token_utils.token_class import Token, make_fake_token, add_operator, NULL
2
+ from token_utils.tokenizing import (
3
+ generate_tokens,
4
+ tokenize,
5
+ get_logical_lines,
6
+ get_physical_lines,
7
+ untokenize,
8
+ stringify,
9
+ dedent,
10
+ print_tokens,
11
+ )
12
+ from token_utils.utils import (
13
+ BracketStack,
14
+ IndentStack,
15
+ pairwise,
16
+ sliding_window,
17
+ split_at_token,
18
+ )
19
+ from token_utils.__about__ import version
20
+ from token_utils.custom_warnings import disable_warnings
21
+
22
+ __all__ = [
23
+ "Token",
24
+ "NULL",
25
+ "make_fake_token",
26
+ "add_operator",
27
+ "BracketStack",
28
+ "IndentStack",
29
+ "pairwise",
30
+ "sliding_window",
31
+ "split_at_token",
32
+ "version",
33
+ "disable_warnings",
34
+ "generate_tokens",
35
+ "tokenize",
36
+ "get_logical_lines",
37
+ "get_physical_lines",
38
+ "untokenize",
39
+ "stringify",
40
+ "dedent",
41
+ "print_tokens",
42
+ ]
@@ -16,6 +16,22 @@ import itertools as _itertools
16
16
  import re
17
17
  import warnings
18
18
  from token_utils._token_plus import *
19
+ from token_utils._token_plus import ( # to satisfy typing
20
+ UNCL_SINGLE,
21
+ UNCL_TRIPLE,
22
+ ERRORTOKEN,
23
+ ENDMARKER,
24
+ OP,
25
+ NAME,
26
+ NL,
27
+ NEWLINE,
28
+ NUMBER,
29
+ STRING,
30
+ COMMENT,
31
+ DEDENT,
32
+ BAD_DEDENT,
33
+ INDENT,
34
+ )
19
35
  from token_utils.custom_warnings import TokenUtilsEOFWarning
20
36
 
21
37
  # prevent accidently importing * from here as names might conflict with our own
@@ -26,7 +42,7 @@ blank_re = re.compile(rb"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII)
26
42
 
27
43
  class TokenInfo(collections.namedtuple("TokenInfo", "type string start end line")):
28
44
  def __repr__(self): # not really needed for us.
29
- annotated_type = "%d (%s)" % (self.type, tok_name[self.type])
45
+ annotated_type = "%d (%s)" % (self.type, tok_name_plus[self.type])
30
46
  return (
31
47
  "TokenInfo(type=%s, string=%r, start=%r, end=%r, line=%r)"
32
48
  % self._replace(type=annotated_type)
@@ -141,7 +157,7 @@ for _prefix in _all_string_prefixes():
141
157
  endpats[_prefix + '"'] = Double
142
158
  endpats[_prefix + "'''"] = Single3
143
159
  endpats[_prefix + '"""'] = Double3
144
- del _prefix
160
+ del _prefix # type: ignore
145
161
 
146
162
  # A set of all of the single and triple quoted string prefixes,
147
163
  # including the opening quotes.
@@ -152,7 +168,7 @@ for t in _all_string_prefixes():
152
168
  single_quoted.add(u)
153
169
  for u in (t + '"""', t + "'''"):
154
170
  triple_quoted.add(u)
155
- del t, u
171
+ del t, u # type: ignore
156
172
 
157
173
  tabsize = 8
158
174
 
@@ -179,6 +195,7 @@ def generate_tokens(readline):
179
195
  contstr, needcont = "", 0
180
196
  contline = None
181
197
  indents = [0]
198
+ strstart = (0, 0) # to silence a typing warning
182
199
 
183
200
  last_line = ""
184
201
  line = ""
@@ -199,7 +216,7 @@ def generate_tokens(readline):
199
216
 
200
217
  if contstr: # continued string
201
218
  if not line:
202
- end = len(contline.split("\n")[-1])
219
+ end = len(contline.split("\n")[-1]) # type: ignore
203
220
  warnings.warn(
204
221
  "Unterminated triple quoted string.",
205
222
  TokenUtilsEOFWarning,
@@ -208,7 +225,7 @@ def generate_tokens(readline):
208
225
  unterminated_triple = True
209
226
  yield TokenInfo(UNCL_TRIPLE, contstr, strstart, (lnum, end), contline)
210
227
  break
211
- endmatch = endprog.match(line)
228
+ endmatch = endprog.match(line) # type: ignore
212
229
  if endmatch:
213
230
  pos = end = endmatch.end(0)
214
231
  yield TokenInfo(
@@ -0,0 +1,66 @@
1
+ from typing import Literal, Pattern
2
+ from _typeshed import Incomplete
3
+
4
+ import collections
5
+ from token_utils._token_plus import *
6
+ from token_utils.custom_warnings import TokenUtilsEOFWarning
7
+ from token_utils._token_plus import __all__
8
+
9
+ blank_re: Pattern[bytes]
10
+
11
+ class TokenInfo(collections.namedtuple("TokenInfo", "type string start end line")):
12
+ def __repr__(self) -> Incomplete: ...
13
+ @property
14
+ def exact_type(self) -> Incomplete: ...
15
+
16
+ def group(*choices: Incomplete) -> Incomplete: ...
17
+ def any(*choices: Incomplete) -> Incomplete: ...
18
+ def maybe(*choices: Incomplete) -> Incomplete: ...
19
+
20
+ Whitespace: Literal["[ \\f\\t]*"] = r"[ \f\t]*"
21
+ Comment: Literal["#[^\\r\\n]*"] = r"#[^\r\n]*"
22
+ Ignore: Incomplete
23
+ Name: Literal["\\w+"] = r"\w+"
24
+ Hexnumber: Literal["0[xX](?:_?[0-9a-fA-F])+"] = r"0[xX](?:_?[0-9a-fA-F])+"
25
+ Binnumber: Literal["0[bB](?:_?[01])+"] = r"0[bB](?:_?[01])+"
26
+ Octnumber: Literal["0[oO](?:_?[0-7])+"] = r"0[oO](?:_?[0-7])+"
27
+ Decnumber: Literal["(?:0(?:_?0)*|[1-9](?:_?[0-9])*)"] = (
28
+ r"(?:0(?:_?0)*|[1-9](?:_?[0-9])*)"
29
+ )
30
+ Intnumber: Incomplete
31
+ Exponent: Literal["[eE][-+]?[0-9](?:_?[0-9])*"] = r"[eE][-+]?[0-9](?:_?[0-9])*"
32
+ Pointfloat: Incomplete
33
+ Expfloat: str
34
+ Floatnumber: Incomplete
35
+ Imagnumber: Incomplete
36
+ Number: Incomplete
37
+ StringPrefix: Incomplete
38
+ Single: Literal["[^'\\\\]*(?:\\\\.[^'\\\\]*)*'"] = r"[^'\\]*(?:\\.[^'\\]*)*'"
39
+ Double: Literal['[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'] = r'[^"\\]*(?:\\.[^"\\]*)*"'
40
+ Single3: Literal["[^'\\\\]*(?:(?:\\\\.|'(?!''))[^'\\\\]*)*'''"] = (
41
+ r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
42
+ )
43
+ Double3: Literal['[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""'] = (
44
+ r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
45
+ )
46
+ Triple: Incomplete
47
+ String: Incomplete
48
+ Special: Incomplete
49
+ Funny: Incomplete
50
+ PlainToken: Incomplete
51
+ Token: Incomplete
52
+ ContStr: Incomplete
53
+ PseudoExtras: Incomplete
54
+ PseudoToken: Incomplete
55
+ contline: str | None
56
+ endpats: Incomplete
57
+ single_quoted: Incomplete
58
+ triple_quoted: Incomplete
59
+ tabsize: Literal[8] = 8
60
+
61
+ def generate_tokens(readline: Incomplete) -> Incomplete: ...
62
+
63
+ BAD_DEDENT: Literal[-1]
64
+ UNCL_SINGLE: Literal[-2]
65
+ UNCL_TRIPLE: Literal[-3]
66
+ FAKE_TOKEN: Literal[-4]
@@ -1,9 +1,12 @@
1
1
  """Token constants with additions to Python's stdlib token.py"""
2
2
 
3
- from token import *
4
- from token import EXACT_TOKEN_TYPES
3
+ from token import * # pyright: ignore[reportWildcardImportFromLibrary]
4
+ from token import EXACT_TOKEN_TYPES # type: ignore
5
+ from token import tok_name # type: ignore # implicit for typing
5
6
 
6
- __all__ = ["tok_name", "ISTERMINAL", "ISNONTERMINAL", "ISEOF", "EXACT_TOKEN_TYPES"]
7
+ # tok_name is apparently defined as Final; so we use a different name
8
+
9
+ __all__ = ["tok_name_plus", "ISTERMINAL", "ISNONTERMINAL", "ISEOF", "EXACT_TOKEN_TYPES"]
7
10
 
8
11
  # Special negative values for token_utils
9
12
 
@@ -18,9 +21,9 @@ UNCL_TRIPLE = -3
18
21
  FAKE_TOKEN = -4
19
22
 
20
23
 
21
- tok_name = {
24
+ tok_name_plus = {
22
25
  value: name
23
26
  for name, value in globals().items()
24
27
  if isinstance(value, int) and not name.startswith("_")
25
28
  }
26
- __all__.extend(tok_name.values())
29
+ __all__.extend(tok_name_plus.values()) # type: ignore
@@ -0,0 +1,11 @@
1
+ from typing import Literal
2
+
3
+ from token import *
4
+ from token import EXACT_TOKEN_TYPES
5
+
6
+ __all__ = ["tok_name_plus", "ISTERMINAL", "ISNONTERMINAL", "ISEOF", "EXACT_TOKEN_TYPES"]
7
+ BAD_DEDENT: Literal[-1]
8
+ UNCL_SINGLE: Literal[-2]
9
+ UNCL_TRIPLE: Literal[-3]
10
+ FAKE_TOKEN: Literal[-4]
11
+ tok_name_plus: dict[int, str]
@@ -0,0 +1,14 @@
1
+ from typing import TextIO
2
+
3
+ class TokenUtilsEOFWarning(UserWarning): ...
4
+ class SemiColonWarning(UserWarning): ...
5
+
6
+ def disable_warnings() -> None: ...
7
+ def custom_showwarning(
8
+ message: str,
9
+ category: Warning,
10
+ filename: str,
11
+ lineno: int,
12
+ file: TextIO | None = None,
13
+ line: str | None = None,
14
+ ) -> None: ...
@@ -1,6 +1,7 @@
1
1
  import ast
2
2
 
3
3
  from token_utils import _py_tokenize
4
+ from token_utils._token_plus import FAKE_TOKEN
4
5
 
5
6
  _token_format = "type={type} string={string} start={start} end={end} line={line}"
6
7
  _op_categories = {
@@ -53,6 +54,12 @@ class Token:
53
54
  self.end = self.end_row, self.end_col = token[3]
54
55
  self.line = token[4]
55
56
 
57
+ def __bool__(self):
58
+ """Returns ``False`` if the object is identical to the ``NULL`` Token,
59
+ ``True`` otherwise. Therefore, ``if NULL == if False``.
60
+ """
61
+ return not self.is_identical(NULL)
62
+
56
63
  def __eq__(self, other):
57
64
  """Compares a Token with another object; returns true if
58
65
  self.string == other.string or if self.string == other.
@@ -70,19 +77,17 @@ class Token:
70
77
  """Nicely formatted token to help with debugging session.
71
78
 
72
79
  Note that it does **not** print a string representation that could be
73
- used to create a new ``Token`` instance, which is something you should
74
- never need to do other than indirectly by using the functions
75
- provided in this module.
80
+ used to create a new ``Token`` instance.
76
81
  """
77
82
  line = self.line
78
83
  if len(line) > 20:
79
84
  line = line[:10] + " <...> " + line[-5:]
80
- name = _py_tokenize.tok_name[self.type]
85
+ name = _py_tokenize.tok_name_plus[self.type]
81
86
  if name == "OP":
82
87
  if self.string in _py_tokenize.EXACT_TOKEN_TYPES:
83
88
  exact_type = _py_tokenize.EXACT_TOKEN_TYPES[self.string]
84
- if exact_type in _py_tokenize.tok_name:
85
- exact_name = _py_tokenize.tok_name[exact_type]
89
+ if exact_type in _py_tokenize.tok_name_plus:
90
+ exact_name = _py_tokenize.tok_name_plus[exact_type]
86
91
  name = f"OP: {exact_name}"
87
92
  return _token_format.format(
88
93
  type="%s (%s)" % (self.type, name),
@@ -111,7 +116,7 @@ class Token:
111
116
  return Token((self.type, self.string, self.start, self.end, self.line))
112
117
 
113
118
  def indentation(self):
114
- """Returns the indentation as a number of spaces."""
119
+ """Returns the indentation (the value of ``start_col``) as a number of spaces."""
115
120
  # Easier to remember than the start_col attribute
116
121
  return self.start_col
117
122
 
@@ -247,6 +252,10 @@ class Token:
247
252
  """Returns True if the token represents a number."""
248
253
  return self.type == _py_tokenize.NUMBER
249
254
 
255
+ def is_NULL(self):
256
+ """Returns True if the token is the NULL token."""
257
+ return self.is_identical(NULL)
258
+
250
259
  def is_operator(self):
251
260
  """Returns true if the token is of type OP"""
252
261
  return self.type == _py_tokenize.OP
@@ -288,7 +297,8 @@ class Token:
288
297
 
289
298
 
290
299
  def add_operator(string, name, category="other"):
291
- """Adds a string defining an operator to the tokenizer.
300
+ """Adds a string defining an operator to the tokenizer,
301
+ and updates the required ``Token`` methods.
292
302
  If the string is already a known operator, nothing other than
293
303
  returning ``False`` is done, otherwise ``True`` is returned.
294
304
 
@@ -300,7 +310,7 @@ def add_operator(string, name, category="other"):
300
310
  as a suffix will be created, with as many underscore needed as to make
301
311
  the name unique.
302
312
 
303
- Defining an operator is essential for proper tokenizing. For example,
313
+ Defining an operator might be essential for proper tokenizing. For example,
304
314
  if one does not define ``!!`` as an operator, ``!!`` would be tokenized
305
315
  as two individual ``!`` tokens.
306
316
 
@@ -332,7 +342,7 @@ def add_operator(string, name, category="other"):
332
342
  # update the info for tokenizing.
333
343
  tok.__dict__[name] = value
334
344
  tok.EXACT_TOKEN_TYPES[string] = value
335
- tok.tok_name[value] = name
345
+ tok.tok_name_plus[value] = name
336
346
  tok.Special = tok.group(
337
347
  *map(re.escape, sorted(tok.EXACT_TOKEN_TYPES, reverse=True))
338
348
  )
@@ -343,9 +353,7 @@ def add_operator(string, name, category="other"):
343
353
  return True
344
354
 
345
355
 
346
- def make_fake_token(
347
- type=_py_tokenize.FAKE_TOKEN, string="$", start=(0, 0), end=(0, 0), line=""
348
- ):
356
+ def make_fake_token(type=FAKE_TOKEN, string="$", start=(0, 0), end=(0, 0), line=""):
349
357
  """Useful when we need to process a list of tokens with
350
358
  multiple consecutive at a time, and we need to lengthen
351
359
  the list for doing so.
@@ -358,3 +366,6 @@ def make_fake_token(
358
366
  """
359
367
  fake = (type, string, start, end, line)
360
368
  return Token(fake)
369
+
370
+
371
+ NULL = make_fake_token(string="")
@@ -0,0 +1,69 @@
1
+ from typing import Final
2
+ from token_utils import _py_tokenize
3
+
4
+ class Token:
5
+ end: tuple[int, int]
6
+ end_col: int
7
+ end_row: int
8
+ line: str
9
+ start: tuple[int, int]
10
+ start_col: int
11
+ start_row: int
12
+ string: str
13
+ type: int
14
+
15
+ def __init__(
16
+ self,
17
+ token: (
18
+ _py_tokenize.TokenInfo
19
+ | tuple[int, str, tuple[int, int], tuple[int, int], str]
20
+ ),
21
+ ) -> None: ...
22
+ def __eq__(self, other: Token | str) -> bool: ... # type: ignore[override]
23
+ def __bool__(self) -> bool: ...
24
+ def __repr__(self) -> str: ...
25
+ def __str__(self) -> str: ...
26
+ def __contains__(self, str_arg: str) -> bool: ...
27
+ def __len__(self) -> int: ...
28
+ def clone(self) -> Token: ...
29
+ def indentation(self) -> int: ...
30
+ def is_assignment(self) -> bool: ...
31
+ def is_bitwise(self) -> bool: ...
32
+ def is_open_bracket(self) -> bool: ...
33
+ def is_close_bracket(self) -> bool: ...
34
+ def is_bracket(self) -> bool: ...
35
+ def is_comment(self) -> bool: ...
36
+ def is_comparison(self) -> bool: ...
37
+ def is_complex(self) -> bool: ...
38
+ def is_f_string(self) -> bool: ...
39
+ def is_float(self) -> bool: ...
40
+ def is_identical(self, other: Token) -> bool: ...
41
+ def is_identifier(self) -> bool: ...
42
+ def is_immediately_before(self, other: Token) -> bool: ...
43
+ def is_immediately_after(self, other: Token) -> bool: ...
44
+ def is_in(self, sequence_of_strings: list[str]) -> bool: ...
45
+ def is_indentation(self) -> bool: ...
46
+ def is_integer(self) -> bool: ...
47
+ def is_keyword(self) -> bool: ...
48
+ def is_matching_bracket(self, other: Token) -> bool: ...
49
+ def is_math_operator(self) -> bool: ...
50
+ def is_name(self) -> bool: ...
51
+ def is_newline(self) -> bool: ...
52
+ def is_number(self) -> bool: ...
53
+ def is_NULL(self) -> bool: ...
54
+ def is_operator(self) -> bool: ...
55
+ def is_other_operator(self) -> bool: ...
56
+ def is_space(self) -> bool: ...
57
+ def is_string(self) -> bool: ...
58
+ def is_unclosed_string(self) -> bool: ...
59
+
60
+ def add_operator(string: str, name: str, category: str = "other") -> bool: ...
61
+ def make_fake_token(
62
+ type: int = ...,
63
+ string: str = "$",
64
+ start: tuple[int, int] = (0, 0),
65
+ end: tuple[int, int] = (0, 0),
66
+ line: str = "",
67
+ ) -> Token: ...
68
+
69
+ NULL: Final[Token]
@@ -391,17 +391,13 @@ def dedent(tokens, nb):
391
391
  return tokenize(end)
392
392
 
393
393
 
394
- __all__ = ["__all__"]
395
- _names = dir()
396
-
397
-
398
- def _make_all():
399
- for name in _names:
400
- if not name.startswith("_"):
401
- __all__.append(name)
402
-
403
-
404
- _make_all()
405
- __all__.remove("__all__")
406
- __all__.remove("Token")
407
- __all__.remove("StringIO")
394
+ __all__ = [
395
+ "generate_tokens",
396
+ "tokenize",
397
+ "get_physical_lines",
398
+ "get_logical_lines",
399
+ "untokenize",
400
+ "stringify",
401
+ "print_tokens",
402
+ "dedent",
403
+ ]
@@ -0,0 +1,29 @@
1
+ from token_utils.token_class import Token
2
+ from collections.abc import Iterator
3
+
4
+ def _fix_empty_line(source: str, prev_token: Token | None, last_token: Token) -> Token: ...
5
+ def generate_tokens(source: str) -> Iterator[Token]: ...
6
+ def tokenize(source: str) -> list[Token]: ...
7
+ def get_physical_lines(
8
+ source: str, remove_comments: bool = True
9
+ ) -> Iterator[list[Token]]: ...
10
+ def get_logical_lines(
11
+ source: str, remove_comments: bool = True, remove_semi_colons: bool = True
12
+ ) -> Iterator[list[Token]]: ...
13
+ def untokenize(tokens: list[Token | str] | list[list[Token | str]]) -> str: ...
14
+ def stringify(
15
+ tokens: list[Token | str] | list[list[Token | str]], remove_comments: bool = False
16
+ ) -> str: ...
17
+ def print_tokens(tokens: str | list[Token] | list[list[Token]]) -> None: ...
18
+ def dedent(tokens: list[Token], nb: int) -> list[Token]: ...
19
+
20
+ __all__ = [
21
+ "generate_tokens",
22
+ "tokenize",
23
+ "get_physical_lines",
24
+ "get_logical_lines",
25
+ "untokenize",
26
+ "stringify",
27
+ "print_tokens",
28
+ "dedent",
29
+ ]