pycroscope 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.
Files changed (117) hide show
  1. pycroscope-0.1.0/LICENSE +176 -0
  2. pycroscope-0.1.0/PKG-INFO +186 -0
  3. pycroscope-0.1.0/README.md +140 -0
  4. pycroscope-0.1.0/pycroscope/__init__.py +73 -0
  5. pycroscope-0.1.0/pycroscope/__main__.py +16 -0
  6. pycroscope-0.1.0/pycroscope/analysis_lib.py +157 -0
  7. pycroscope-0.1.0/pycroscope/annotated_types.py +327 -0
  8. pycroscope-0.1.0/pycroscope/annotations.py +1425 -0
  9. pycroscope-0.1.0/pycroscope/arg_spec.py +1132 -0
  10. pycroscope-0.1.0/pycroscope/ast_annotator.py +183 -0
  11. pycroscope-0.1.0/pycroscope/asynq_checker.py +241 -0
  12. pycroscope-0.1.0/pycroscope/attributes.py +631 -0
  13. pycroscope-0.1.0/pycroscope/boolability.py +195 -0
  14. pycroscope-0.1.0/pycroscope/checker.py +482 -0
  15. pycroscope-0.1.0/pycroscope/error_code.py +218 -0
  16. pycroscope-0.1.0/pycroscope/extensions.py +643 -0
  17. pycroscope-0.1.0/pycroscope/find_unused.py +238 -0
  18. pycroscope-0.1.0/pycroscope/format_strings.py +676 -0
  19. pycroscope-0.1.0/pycroscope/functions.py +449 -0
  20. pycroscope-0.1.0/pycroscope/implementation.py +2294 -0
  21. pycroscope-0.1.0/pycroscope/importer.py +112 -0
  22. pycroscope-0.1.0/pycroscope/name_check_visitor.py +6099 -0
  23. pycroscope-0.1.0/pycroscope/node_visitor.py +1174 -0
  24. pycroscope-0.1.0/pycroscope/options.py +434 -0
  25. pycroscope-0.1.0/pycroscope/patma.py +508 -0
  26. pycroscope-0.1.0/pycroscope/predicates.py +177 -0
  27. pycroscope-0.1.0/pycroscope/reexport.py +68 -0
  28. pycroscope-0.1.0/pycroscope/runtime.py +82 -0
  29. pycroscope-0.1.0/pycroscope/safe.py +196 -0
  30. pycroscope-0.1.0/pycroscope/shared_options.py +69 -0
  31. pycroscope-0.1.0/pycroscope/signature.py +2795 -0
  32. pycroscope-0.1.0/pycroscope/stacked_scopes.py +1614 -0
  33. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/aliases.pyi +8 -0
  34. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/args.pyi +10 -0
  35. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/callable.pyi +4 -0
  36. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/cdata.pyi +3 -0
  37. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/contextmanager.pyi +3 -0
  38. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/defaults.pyi +10 -0
  39. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/deprecated.pyi +18 -0
  40. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/evaluated.pyi +19 -0
  41. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/initnew.pyi +25 -0
  42. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/nested.pyi +3 -0
  43. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/overloaded.pyi +10 -0
  44. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/paramspec.pyi +9 -0
  45. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/posonly.pyi +4 -0
  46. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/recursion.pyi +9 -0
  47. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/self.pyi +9 -0
  48. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/tsself.pyi +5 -0
  49. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/typeddict.pyi +16 -0
  50. pycroscope-0.1.0/pycroscope/stubs/_pycroscope_tests-stubs/typevar.pyi +7 -0
  51. pycroscope-0.1.0/pycroscope/stubs/pycroscope-stubs/extensions.pyi +19 -0
  52. pycroscope-0.1.0/pycroscope/suggested_type.py +250 -0
  53. pycroscope-0.1.0/pycroscope/test.toml +23 -0
  54. pycroscope-0.1.0/pycroscope/test_analysis_lib.py +44 -0
  55. pycroscope-0.1.0/pycroscope/test_annotated_types.py +323 -0
  56. pycroscope-0.1.0/pycroscope/test_annotations.py +2004 -0
  57. pycroscope-0.1.0/pycroscope/test_arg_spec.py +443 -0
  58. pycroscope-0.1.0/pycroscope/test_ast_annotator.py +80 -0
  59. pycroscope-0.1.0/pycroscope/test_async_await.py +338 -0
  60. pycroscope-0.1.0/pycroscope/test_asynq.py +269 -0
  61. pycroscope-0.1.0/pycroscope/test_asynq_checker.py +459 -0
  62. pycroscope-0.1.0/pycroscope/test_attributes.py +422 -0
  63. pycroscope-0.1.0/pycroscope/test_boolability.py +205 -0
  64. pycroscope-0.1.0/pycroscope/test_config.py +154 -0
  65. pycroscope-0.1.0/pycroscope/test_definite_value.py +37 -0
  66. pycroscope-0.1.0/pycroscope/test_deprecated.py +97 -0
  67. pycroscope-0.1.0/pycroscope/test_enum.py +138 -0
  68. pycroscope-0.1.0/pycroscope/test_error_code.py +11 -0
  69. pycroscope-0.1.0/pycroscope/test_extensions.py +81 -0
  70. pycroscope-0.1.0/pycroscope/test_format_strings.py +506 -0
  71. pycroscope-0.1.0/pycroscope/test_functions.py +244 -0
  72. pycroscope-0.1.0/pycroscope/test_generators.py +85 -0
  73. pycroscope-0.1.0/pycroscope/test_implementation.py +1564 -0
  74. pycroscope-0.1.0/pycroscope/test_import.py +90 -0
  75. pycroscope-0.1.0/pycroscope/test_inference_helpers.py +68 -0
  76. pycroscope-0.1.0/pycroscope/test_literal_string.py +20 -0
  77. pycroscope-0.1.0/pycroscope/test_name_check_visitor.py +2283 -0
  78. pycroscope-0.1.0/pycroscope/test_never.py +148 -0
  79. pycroscope-0.1.0/pycroscope/test_node_visitor.py +478 -0
  80. pycroscope-0.1.0/pycroscope/test_operations.py +316 -0
  81. pycroscope-0.1.0/pycroscope/test_override.py +40 -0
  82. pycroscope-0.1.0/pycroscope/test_patma.py +258 -0
  83. pycroscope-0.1.0/pycroscope/test_pep673.py +182 -0
  84. pycroscope-0.1.0/pycroscope/test_recursion.py +30 -0
  85. pycroscope-0.1.0/pycroscope/test_runtime.py +48 -0
  86. pycroscope-0.1.0/pycroscope/test_self.py +20 -0
  87. pycroscope-0.1.0/pycroscope/test_signature.py +1337 -0
  88. pycroscope-0.1.0/pycroscope/test_stacked_scopes.py +1974 -0
  89. pycroscope-0.1.0/pycroscope/test_suggested_type.py +69 -0
  90. pycroscope-0.1.0/pycroscope/test_thrift_enum.py +87 -0
  91. pycroscope-0.1.0/pycroscope/test_try.py +128 -0
  92. pycroscope-0.1.0/pycroscope/test_type_aliases.py +103 -0
  93. pycroscope-0.1.0/pycroscope/test_type_evaluation.py +590 -0
  94. pycroscope-0.1.0/pycroscope/test_type_object.py +407 -0
  95. pycroscope-0.1.0/pycroscope/test_typeddict.py +373 -0
  96. pycroscope-0.1.0/pycroscope/test_typeis.py +886 -0
  97. pycroscope-0.1.0/pycroscope/test_typeshed.py +843 -0
  98. pycroscope-0.1.0/pycroscope/test_typevar.py +424 -0
  99. pycroscope-0.1.0/pycroscope/test_unsafe_comparison.py +137 -0
  100. pycroscope-0.1.0/pycroscope/test_value.py +711 -0
  101. pycroscope-0.1.0/pycroscope/test_yield_checker.py +805 -0
  102. pycroscope-0.1.0/pycroscope/tests.py +245 -0
  103. pycroscope-0.1.0/pycroscope/type_evaluation.py +810 -0
  104. pycroscope-0.1.0/pycroscope/type_object.py +273 -0
  105. pycroscope-0.1.0/pycroscope/typeshed.py +1238 -0
  106. pycroscope-0.1.0/pycroscope/typevar.py +180 -0
  107. pycroscope-0.1.0/pycroscope/value.py +3393 -0
  108. pycroscope-0.1.0/pycroscope/yield_checker.py +627 -0
  109. pycroscope-0.1.0/pycroscope.egg-info/PKG-INFO +186 -0
  110. pycroscope-0.1.0/pycroscope.egg-info/SOURCES.txt +115 -0
  111. pycroscope-0.1.0/pycroscope.egg-info/dependency_links.txt +1 -0
  112. pycroscope-0.1.0/pycroscope.egg-info/entry_points.txt +2 -0
  113. pycroscope-0.1.0/pycroscope.egg-info/requires.txt +14 -0
  114. pycroscope-0.1.0/pycroscope.egg-info/top_level.txt +1 -0
  115. pycroscope-0.1.0/pyproject.toml +81 -0
  116. pycroscope-0.1.0/setup.cfg +4 -0
  117. pycroscope-0.1.0/setup.py +54 -0
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,186 @@
1
+ Metadata-Version: 2.4
2
+ Name: pycroscope
3
+ Version: 0.1.0
4
+ Summary: A static analyzer for Python
5
+ Home-page: https://github.com/JelleZijlstra/pycroscope
6
+ Author: Jelle Zijlstra
7
+ Author-email: jelle.zijlstra@gmail.com
8
+ License: Apache Software License
9
+ Keywords: type checker static analysis
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: asynq
21
+ Requires-Dist: qcore>=0.5.1
22
+ Requires-Dist: ast_decompiler>=0.4.0
23
+ Requires-Dist: typeshed_client>=2.1.0
24
+ Requires-Dist: typing_extensions>=4.12.0
25
+ Requires-Dist: codemod
26
+ Requires-Dist: tomli>=1.1.0
27
+ Provides-Extra: tests
28
+ Requires-Dist: pytest; extra == "tests"
29
+ Requires-Dist: mypy_extensions; extra == "tests"
30
+ Requires-Dist: attrs; extra == "tests"
31
+ Requires-Dist: pydantic; extra == "tests"
32
+ Requires-Dist: annotated-types; extra == "tests"
33
+ Dynamic: author
34
+ Dynamic: author-email
35
+ Dynamic: classifier
36
+ Dynamic: description
37
+ Dynamic: description-content-type
38
+ Dynamic: home-page
39
+ Dynamic: keywords
40
+ Dynamic: license
41
+ Dynamic: license-file
42
+ Dynamic: provides-extra
43
+ Dynamic: requires-dist
44
+ Dynamic: requires-python
45
+ Dynamic: summary
46
+
47
+ # pycroscope
48
+
49
+ Pycroscope is a semi-static type checker for Python code. Like a static type checker (e.g., mypy or pyright), it
50
+ detects type errors in your code so bugs can be found before they reach production. Unlike such tools, however,
51
+ it imports the modules it type checks, enabling pycroscope to understand many dynamic constructs that other type
52
+ checkers will reject. This property also makes it possible to extend pycroscope with plugins that interact directly
53
+ with your code.
54
+
55
+ Pycroscope is a friendly fork of [pyanalyze](https://github.com/quora/pyanalyze).
56
+
57
+ ## Usage
58
+
59
+ You can install pycroscope with:
60
+
61
+ ```bash
62
+ $ pip install pycroscope
63
+ ```
64
+
65
+ Once it is installed, you can run pycroscope on a Python file or package as follows:
66
+
67
+ ```bash
68
+ $ python -m pycroscope file.py
69
+ $ python -m pycroscope package/
70
+ ```
71
+
72
+ But note that this will try to import all Python files it is passed. If you have scripts that perform operations without `if __name__ == "__main__":` blocks, pycroscope may end up executing them.
73
+
74
+ In order to run successfully, pycroscope needs to be able to import the code it checks. To make this work you may have to manually adjust Python's import path using the `$PYTHONPATH` environment variable.
75
+
76
+ For quick experimentation, you can also use the `-c` option to directly type check a piece of code:
77
+
78
+ ```
79
+ $ python -m pycroscope -c 'import typing; typing.reveal_type(1)'
80
+ Runtime type is 'int'
81
+
82
+ Revealed type is 'Literal[1]' (code: reveal_type)
83
+ In <code> at line 1
84
+ 1: import typing; typing.reveal_type(1)
85
+ ^
86
+ ```
87
+
88
+ ### Configuration
89
+
90
+ Pycroscope has a number of command-line options, which you can see by running `python -m pycroscope --help`. Important ones include `-f`, which runs an interactive prompt that lets you examine and fix each error found by pycroscope, and `--enable`/`--disable`, which enable and disable specific error codes.
91
+
92
+ Configuration through a `pyproject.toml` file is also supported. See
93
+ [the documentation](https://pycroscope.readthedocs.io/en/latest/configuration.html) for
94
+ details.
95
+
96
+ ### Extending pycroscope
97
+
98
+ One of the main ways to extend pycroscope is by providing a specification for a particular function. This allows you to run arbitrary code that inspects the arguments to the function and raises errors if something is wrong.
99
+
100
+ As an example, suppose your codebase contains a function `database.run_query()` that takes as an argument a SQL string, like this:
101
+
102
+ ```python
103
+ database.run_query("SELECT answer, question FROM content")
104
+ ```
105
+
106
+ You want to detect when a call to `run_query()` contains syntactically invalid SQL or refers to a non-existent table or column. You could set that up with code like this:
107
+
108
+ ```python
109
+ from pycroscope.error_code import ErrorCode
110
+ from pycroscope.signature import CallContext, Signature, SigParameter
111
+ from pycroscope.value import KnownValue, TypedValue, AnyValue, AnySource, Value
112
+
113
+ from database import run_query, parse_sql
114
+
115
+
116
+ def run_query_impl(ctx: CallContext) -> Value:
117
+ sql = ctx.vars["sql"]
118
+ if not isinstance(sql, KnownValue) or not isinstance(sql.val, str):
119
+ ctx.show_error(
120
+ "Argument to run_query() must be a string literal",
121
+ ErrorCode.incompatible_call,
122
+ )
123
+ return AnyValue(AnySource.error)
124
+
125
+ try:
126
+ parsed = parse_sql(sql)
127
+ except ValueError as e:
128
+ ctx.show_error(
129
+ f"Invalid sql passed to run_query(): {e}",
130
+ ErrorCode.incompatible_call,
131
+ )
132
+ return AnyValue(AnySource.error)
133
+
134
+ # check that the parsed SQL is valid...
135
+
136
+ # pycroscope will use this as the inferred return type for the function
137
+ return TypedValue(list)
138
+
139
+
140
+ # in pyproject.toml, set:
141
+ # known_signatures = ["<module>.get_known_argspecs"]
142
+ def get_known_argspecs(arg_spec_cache):
143
+ return {
144
+ # This infers the parameter types and names from the function signature
145
+ run_query: arg_spec_cache.get_argspec(
146
+ run_query, impl=run_query_impl
147
+ ),
148
+ # You can also write the signature manually
149
+ run_query: Signature.make(
150
+ [SigParameter("sql", annotation=TypedValue(str))],
151
+ callable=run_query,
152
+ impl=run_query_impl,
153
+ ),
154
+ }
155
+ ```
156
+
157
+ ### Supported features
158
+
159
+ Pycroscope generally aims to implement [the Python typing spec](https://typing.readthedocs.io/en/latest/spec/index.html),
160
+ but support for some features is incomplete. See [the documentation](https://pycroscope.readthedocs.io/en/latest/)
161
+ for details.
162
+
163
+ ### Ignoring errors
164
+
165
+ Sometimes pycroscope gets things wrong and you need to ignore an error it emits. This can be done as follows:
166
+
167
+ - Add `# static analysis: ignore` on a line by itself before the line that generates the error.
168
+ - Add `# static analysis: ignore` at the end of the line that generates the error.
169
+ - Add `# static analysis: ignore` at the top of the file; this will ignore errors in the entire file.
170
+
171
+ You can add an error code, like `# static analysis: ignore[undefined_name]`, to ignore only a specific error code. This does not work for whole-file ignores. If the `bare_ignore` error code is turned on, pycroscope will emit an error if you don't specify an error code on an ignore comment.
172
+
173
+ Pycroscope does not currently support the standard `# type: ignore` comment syntax.
174
+
175
+ ### Python version support
176
+
177
+ Pycroscope supports all versions of Python that have not reached end-of-life. Because it imports the code it checks, you have to run it using the same version of Python you use to run your code.
178
+
179
+ ## Contributing
180
+
181
+ We welcome your contributions. See [CONTRIBUTING.md](https://github.com/JelleZijlstra/pycroscope/blob/master/CONTRIBUTING.md)
182
+ for how to get started.
183
+
184
+ ## Documentation
185
+
186
+ Documentation is available on [GitHub](https://github.com/JelleZijlstra/pycroscope/tree/master/docs).
@@ -0,0 +1,140 @@
1
+ # pycroscope
2
+
3
+ Pycroscope is a semi-static type checker for Python code. Like a static type checker (e.g., mypy or pyright), it
4
+ detects type errors in your code so bugs can be found before they reach production. Unlike such tools, however,
5
+ it imports the modules it type checks, enabling pycroscope to understand many dynamic constructs that other type
6
+ checkers will reject. This property also makes it possible to extend pycroscope with plugins that interact directly
7
+ with your code.
8
+
9
+ Pycroscope is a friendly fork of [pyanalyze](https://github.com/quora/pyanalyze).
10
+
11
+ ## Usage
12
+
13
+ You can install pycroscope with:
14
+
15
+ ```bash
16
+ $ pip install pycroscope
17
+ ```
18
+
19
+ Once it is installed, you can run pycroscope on a Python file or package as follows:
20
+
21
+ ```bash
22
+ $ python -m pycroscope file.py
23
+ $ python -m pycroscope package/
24
+ ```
25
+
26
+ But note that this will try to import all Python files it is passed. If you have scripts that perform operations without `if __name__ == "__main__":` blocks, pycroscope may end up executing them.
27
+
28
+ In order to run successfully, pycroscope needs to be able to import the code it checks. To make this work you may have to manually adjust Python's import path using the `$PYTHONPATH` environment variable.
29
+
30
+ For quick experimentation, you can also use the `-c` option to directly type check a piece of code:
31
+
32
+ ```
33
+ $ python -m pycroscope -c 'import typing; typing.reveal_type(1)'
34
+ Runtime type is 'int'
35
+
36
+ Revealed type is 'Literal[1]' (code: reveal_type)
37
+ In <code> at line 1
38
+ 1: import typing; typing.reveal_type(1)
39
+ ^
40
+ ```
41
+
42
+ ### Configuration
43
+
44
+ Pycroscope has a number of command-line options, which you can see by running `python -m pycroscope --help`. Important ones include `-f`, which runs an interactive prompt that lets you examine and fix each error found by pycroscope, and `--enable`/`--disable`, which enable and disable specific error codes.
45
+
46
+ Configuration through a `pyproject.toml` file is also supported. See
47
+ [the documentation](https://pycroscope.readthedocs.io/en/latest/configuration.html) for
48
+ details.
49
+
50
+ ### Extending pycroscope
51
+
52
+ One of the main ways to extend pycroscope is by providing a specification for a particular function. This allows you to run arbitrary code that inspects the arguments to the function and raises errors if something is wrong.
53
+
54
+ As an example, suppose your codebase contains a function `database.run_query()` that takes as an argument a SQL string, like this:
55
+
56
+ ```python
57
+ database.run_query("SELECT answer, question FROM content")
58
+ ```
59
+
60
+ You want to detect when a call to `run_query()` contains syntactically invalid SQL or refers to a non-existent table or column. You could set that up with code like this:
61
+
62
+ ```python
63
+ from pycroscope.error_code import ErrorCode
64
+ from pycroscope.signature import CallContext, Signature, SigParameter
65
+ from pycroscope.value import KnownValue, TypedValue, AnyValue, AnySource, Value
66
+
67
+ from database import run_query, parse_sql
68
+
69
+
70
+ def run_query_impl(ctx: CallContext) -> Value:
71
+ sql = ctx.vars["sql"]
72
+ if not isinstance(sql, KnownValue) or not isinstance(sql.val, str):
73
+ ctx.show_error(
74
+ "Argument to run_query() must be a string literal",
75
+ ErrorCode.incompatible_call,
76
+ )
77
+ return AnyValue(AnySource.error)
78
+
79
+ try:
80
+ parsed = parse_sql(sql)
81
+ except ValueError as e:
82
+ ctx.show_error(
83
+ f"Invalid sql passed to run_query(): {e}",
84
+ ErrorCode.incompatible_call,
85
+ )
86
+ return AnyValue(AnySource.error)
87
+
88
+ # check that the parsed SQL is valid...
89
+
90
+ # pycroscope will use this as the inferred return type for the function
91
+ return TypedValue(list)
92
+
93
+
94
+ # in pyproject.toml, set:
95
+ # known_signatures = ["<module>.get_known_argspecs"]
96
+ def get_known_argspecs(arg_spec_cache):
97
+ return {
98
+ # This infers the parameter types and names from the function signature
99
+ run_query: arg_spec_cache.get_argspec(
100
+ run_query, impl=run_query_impl
101
+ ),
102
+ # You can also write the signature manually
103
+ run_query: Signature.make(
104
+ [SigParameter("sql", annotation=TypedValue(str))],
105
+ callable=run_query,
106
+ impl=run_query_impl,
107
+ ),
108
+ }
109
+ ```
110
+
111
+ ### Supported features
112
+
113
+ Pycroscope generally aims to implement [the Python typing spec](https://typing.readthedocs.io/en/latest/spec/index.html),
114
+ but support for some features is incomplete. See [the documentation](https://pycroscope.readthedocs.io/en/latest/)
115
+ for details.
116
+
117
+ ### Ignoring errors
118
+
119
+ Sometimes pycroscope gets things wrong and you need to ignore an error it emits. This can be done as follows:
120
+
121
+ - Add `# static analysis: ignore` on a line by itself before the line that generates the error.
122
+ - Add `# static analysis: ignore` at the end of the line that generates the error.
123
+ - Add `# static analysis: ignore` at the top of the file; this will ignore errors in the entire file.
124
+
125
+ You can add an error code, like `# static analysis: ignore[undefined_name]`, to ignore only a specific error code. This does not work for whole-file ignores. If the `bare_ignore` error code is turned on, pycroscope will emit an error if you don't specify an error code on an ignore comment.
126
+
127
+ Pycroscope does not currently support the standard `# type: ignore` comment syntax.
128
+
129
+ ### Python version support
130
+
131
+ Pycroscope supports all versions of Python that have not reached end-of-life. Because it imports the code it checks, you have to run it using the same version of Python you use to run your code.
132
+
133
+ ## Contributing
134
+
135
+ We welcome your contributions. See [CONTRIBUTING.md](https://github.com/JelleZijlstra/pycroscope/blob/master/CONTRIBUTING.md)
136
+ for how to get started.
137
+
138
+ ## Documentation
139
+
140
+ Documentation is available on [GitHub](https://github.com/JelleZijlstra/pycroscope/tree/master/docs).
@@ -0,0 +1,73 @@
1
+ """
2
+
3
+ pycroscope is a package for Python static analysis.
4
+
5
+ """
6
+
7
+ # ignore unused import errors
8
+ # flake8: noqa
9
+
10
+ from . import (
11
+ analysis_lib,
12
+ annotated_types,
13
+ annotations,
14
+ arg_spec,
15
+ ast_annotator,
16
+ asynq_checker,
17
+ boolability,
18
+ checker,
19
+ error_code,
20
+ extensions,
21
+ find_unused,
22
+ functions,
23
+ implementation,
24
+ name_check_visitor,
25
+ node_visitor,
26
+ options,
27
+ patma,
28
+ predicates,
29
+ reexport,
30
+ safe,
31
+ runtime,
32
+ shared_options,
33
+ signature,
34
+ stacked_scopes,
35
+ suggested_type,
36
+ tests,
37
+ type_object,
38
+ typeshed,
39
+ typevar,
40
+ value,
41
+ yield_checker,
42
+ )
43
+ from .find_unused import used as used
44
+ from .value import assert_is_value as assert_is_value, dump_value as dump_value
45
+
46
+ # Exposed as APIs
47
+ used(ast_annotator)
48
+ used(assert_is_value)
49
+ used(dump_value)
50
+ used(extensions.LiteralOnly)
51
+ used(extensions.NoAny)
52
+ used(extensions.overload)
53
+ used(extensions.evaluated)
54
+ used(extensions.is_provided)
55
+ used(extensions.is_keyword)
56
+ used(extensions.is_positional)
57
+ used(extensions.is_of_type)
58
+ used(extensions.show_error)
59
+ used(extensions.has_extra_keys)
60
+ used(extensions.EnumName)
61
+ used(extensions.ValidRegex)
62
+ used(value.UNRESOLVED_VALUE) # keeping it around for now just in case
63
+ used(reexport)
64
+ used(patma)
65
+ used(checker)
66
+ used(suggested_type)
67
+ used(options)
68
+ used(shared_options)
69
+ used(functions)
70
+ used(predicates)
71
+ used(typevar)
72
+ used(annotated_types)
73
+ used(runtime)
@@ -0,0 +1,16 @@
1
+ import os
2
+ import sys
3
+
4
+ from pycroscope.name_check_visitor import NameCheckVisitor
5
+
6
+
7
+ def main() -> None:
8
+ if os.name == "nt":
9
+ # Enable ANSI color codes for Windows cmd using this strange workaround
10
+ # ( see https://github.com/python/cpython/issues/74261 )
11
+ os.system("")
12
+ sys.exit(NameCheckVisitor.main())
13
+
14
+
15
+ if __name__ == "__main__":
16
+ main()