Skip to content

Commit a498d65

Browse files
authored
Merge pull request #1288 from bact/rm-type-analysis-data
Do not store type hint analysis in GitHub
2 parents 3292086 + 2736522 commit a498d65

15 files changed

Lines changed: 100 additions & 1895 deletions

.github/copilot-instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
configuration. Communicate this to the users during code review.
1313
See <https://www.nltk.org/py-modindex.html>.
1414
- [ ] The type information analyzer at
15-
<https://github.com/PyThaiNLP/pythainlp/blob/dev/build_tools/analysis/type_hint_analyzer.py>
15+
<https://github.com/PyThaiNLP/pythainlp/blob/dev/build_tools/analysis/type-analyzer.py>
1616
can generate information about annotation completeness of
1717
variables, functions, methods, type aliases, decorators, and
1818
classes in the PyThaiNLP repo.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,7 @@ logs/
129129
# Temp files
130130
*.tmp
131131
*.temp
132+
133+
# Type annotation analyzer
134+
build_tools/output/*.csv
135+
build_tools/output/*.json

build_tools/analysis/README.md

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
# Code Analysis Tools
1+
# Code analysis tools
22

33
This directory contains tools for analyzing the PyThaiNLP codebase.
44

5-
## Type Hint Analysis
5+
## Type annotation analysis
66

77
### Overview
88

9-
The type hint analysis system provides comprehensive coverage analysis of type annotations across the entire PyThaiNLP codebase. It follows the [Python typing documentation's type completeness guidelines](https://typing.python.org/en/latest/guides/libraries.html#type-completeness) to assess the quality and completeness of type hints.
9+
The type annotation analysis system provides comprehensive coverage analysis
10+
of type annotations across the entire PyThaiNLP codebase.
11+
It follows the
12+
[Python typing documentation's type completeness guidelines][type-completeness]
13+
to estimate the quality and completeness of type annotations.
14+
15+
[type-completeness]: https://typing.python.org/en/latest/guides/libraries.html#type-completeness
1016

1117
### Scripts
1218

13-
#### type_hint_analyzer.py
19+
#### type-analyzer.py
1420

15-
Main script that performs comprehensive type hint coverage analysis using Python's Abstract Syntax Tree (AST) module.
21+
Main script that performs comprehensive type annotation coverage analysis
22+
using Python's Abstract Syntax Tree (AST) module.
1623

1724
**What it analyzes:**
1825

@@ -28,7 +35,8 @@ Main script that performs comprehensive type hint coverage analysis using Python
2835

2936
**Implementation Details:**
3037

31-
The analyzer uses Python's `ast` module to parse and traverse the syntax tree of each Python file. Key components:
38+
The analyzer uses Python's `ast` module to parse and traverse the syntax tree
39+
of each Python file. Key components:
3240

3341
- `TypeHintAnalyzer` class: Custom `ast.NodeVisitor` that visits each node
3442
- `visit_FunctionDef()`: Analyzes function/method signatures
@@ -37,6 +45,7 @@ The analyzer uses Python's `ast` module to parse and traverse the syntax tree of
3745
- `visit_Assign()`: Handles non-annotated assignments for comparison
3846

3947
The analyzer distinguishes between:
48+
4049
- Module-level scope (top of file)
4150
- Class-level scope (inside class definition)
4251
- Function-level scope (inside function/method)
@@ -49,31 +58,32 @@ The analyzer distinguishes between:
4958

5059
**Usage:**
5160

61+
For best results, delete all previous data files (`*.csv`, `*.json`)
62+
and clear the cache for mypy and any other static type checkers
63+
before running the analyzer.
64+
5265
```bash
5366
# Run from the build_tools/analysis directory (default output: ./output)
54-
python3 type_hint_analyzer.py
67+
python type-analyzer.py
5568

5669
# Or specify a custom output directory
57-
python3 type_hint_analyzer.py --output-dir /path/to/output
70+
python type-analyzer.py --output-dir /path/to/output
5871

5972
# Get help
60-
python3 type_hint_analyzer.py --help
73+
python type-analyzer.py --help
6174
```
6275

63-
**Performance:**
76+
Enable more accurate static analysis by installing optional dependencies
77+
(and their type stubs, if available) for enhanced type discovery.
6478

65-
- Analyzes ~190 Python files in pythainlp/ directory
66-
- Processes ~720 functions/methods and ~960 variables
67-
- Typical runtime: 2-3 minutes (including mypy analysis)
68-
- Mypy timeout: 60 seconds per submodule
79+
#### gen-csv.py
6980

70-
#### generate_csv.py
71-
72-
Converts the JSON output from type_hint_analyzer.py into CSV files for easy analysis in spreadsheet applications or data analysis tools.
81+
Converts the JSON output from type-analyzer.py into CSV files for
82+
easy analysis in spreadsheet applications or data analysis tools.
7383

7484
**Prerequisites:**
7585

76-
- Must run `type_hint_analyzer.py` first to generate the JSON data
86+
- Must run `type-analyzer.py` first to generate the JSON data
7787

7888
**Output:**
7989

@@ -88,6 +98,7 @@ Converts the JSON output from type_hint_analyzer.py into CSV files for easy anal
8898
**CSV Schema:**
8999

90100
Functions CSV files include:
101+
91102
- Function Name (qualified name)
92103
- Submodule
93104
- Scope (public/private)
@@ -101,6 +112,7 @@ Functions CSV files include:
101112
- Line Number
102113

103114
Variables CSV files include:
115+
104116
- Variable Name (qualified name)
105117
- Submodule
106118
- Parent Class (for class/instance variables)
@@ -112,28 +124,28 @@ Variables CSV files include:
112124

113125
```bash
114126
# Run from the build_tools/analysis directory (uses ./output by default)
115-
python3 generate_csv.py
127+
python gen-csv.py
116128

117129
# Or specify custom paths
118-
python3 generate_csv.py --input /path/to/input.json --output-dir /path/to/output
130+
python gen-csv.py --input /path/to/input.json --output-dir /path/to/output
119131

120132
# Get help
121-
python3 generate_csv.py --help
133+
python gen-csv.py --help
122134
```
123135

124136
### Complete Workflow
125137

126-
To perform a full type hint analysis:
138+
To perform a full type annotation analysis:
127139

128140
```bash
129141
# Navigate to the analysis directory
130142
cd build_tools/analysis
131143

132144
# 1. Run the analyzer (outputs to ./output by default)
133-
python3 type_hint_analyzer.py
145+
python type-analyzer.py
134146

135147
# 2. Generate CSV files (reads from ./output by default)
136-
python3 generate_csv.py
148+
python gen-csv.py
137149

138150
# 3. Review the results
139151
ls -la output/
@@ -142,9 +154,9 @@ cat output/submodule_summary.csv
142154

143155
**Example Output:**
144156

145-
```
157+
```text
146158
================================================================================
147-
TYPE HINT COVERAGE ANALYSIS FOR PYTHAINLP
159+
TYPE ANNOTATION COVERAGE ANALYSIS FOR PYTHAINLP
148160
================================================================================
149161
150162
Repository root: /path/to/pythainlp
@@ -203,9 +215,10 @@ The workflow provides continuous monitoring of type hint coverage as the codebas
203215
### Type Completeness Standards
204216

205217
This analyzer follows the type completeness guidelines from the Python typing documentation:
206-
https://typing.python.org/en/latest/guides/libraries.html#type-completeness
218+
<https://typing.python.org/en/latest/guides/libraries.html#type-completeness>
207219

208220
The analysis covers:
221+
209222
- All function and method signatures (parameters and return types)
210223
- Class variables (class-level attributes)
211224
- Instance variables (instance attributes)
@@ -215,17 +228,18 @@ The analysis covers:
215228

216229
**Type Completeness Criteria:**
217230

218-
According to PEP 561 and the Python typing documentation, a library is considered to have complete type hints when:
231+
According to PEP 561 and the Python typing documentation,
232+
a library is considered to have complete type hints when:
219233

220234
1. All exported functions, methods, and classes have type annotations
221235
2. All public module-level variables have type annotations
222236
3. All class and instance variables in exported classes have type annotations
223237
4. Generic types are properly parameterized
224238
5. The library passes type checking with mypy in strict mode
225239

226-
### Analysis Categories
240+
### Analysis categories
227241

228-
**Type Hint Status:**
242+
**Type hint status:**
229243

230244
- **Complete:** All parameters and return value have type hints (for functions), or variable has type annotation (for variables)
231245
- **Incomplete:** Some parameters or return value missing type hints (for functions only)
@@ -254,7 +268,7 @@ According to PEP 561 and the Python typing documentation, a library is considere
254268
- Modern syntax: `MyType: TypeAlias = dict[str, int]`
255269
- Also detects `typing.TypeAlias` and `typing_extensions.TypeAlias`
256270

257-
**Priority Levels:**
271+
**Priority levels:**
258272

259273
Functions are assigned priority based on visibility and usage patterns:
260274

@@ -269,7 +283,7 @@ Functions are assigned priority based on visibility and usage patterns:
269283
- Internal implementation details
270284
- Lower priority for type hint coverage
271285

272-
**Test Suites:**
286+
**Test suites:**
273287

274288
The analyzer maps functions to test categories based on their dependencies:
275289

@@ -281,14 +295,13 @@ The analyzer maps functions to test categories based on their dependencies:
281295

282296
This mapping helps understand which functions are tested and their dependency requirements.
283297

284-
### Output Files
298+
### Output files
285299

286300
All analysis outputs are stored in:
287301

288302
- `build_tools/analysis/output/` - JSON and CSV data files
289-
- `TYPE_HINT_ANALYSIS.md` - Main analysis report (repository root)
290303

291-
**JSON Structure:**
304+
**JSON structure:**
292305

293306
```json
294307
{
@@ -329,36 +342,37 @@ All analysis outputs are stored in:
329342
}
330343
```
331344

332-
### Code Review and Quality
345+
### Code review and quality
333346

334-
**Documentation Coverage:**
347+
**Documentation coverage:**
335348

336349
The analyzer codebase maintains high documentation standards:
350+
337351
- 95.7% docstring coverage (22 of 23 functions/methods)
338352
- All public functions have comprehensive docstrings
339353
- Docstrings follow reStructuredText format for Sphinx compatibility
340354

341-
**Code Quality:**
355+
**Code quality:**
342356

343357
- Follows Ruff linting standards
344358
- Type hints on all function signatures
345359
- Clear separation of concerns with dedicated helper methods
346360
- Proper exception handling for file I/O and subprocess calls
347361

348-
**Key Design Decisions:**
362+
**Key Design decisions:**
349363

350364
1. **AST-based Analysis**: Uses Python's `ast` module rather than runtime inspection
351365
- Pros: No need to import/execute code, faster, safer
352366
- Cons: Cannot detect dynamically generated code
353-
367+
354368
2. **Stateful Visitor Pattern**: Tracks context (class, function, module level)
355369
- Enables accurate classification of variables
356370
- Distinguishes between local, instance, class, and module variables
357-
371+
358372
3. **Reference Counting**: Simple text-based search for usage patterns
359373
- Fast and implementation-agnostic
360374
- Trade-off: May have false positives (comments, strings)
361-
375+
362376
4. **Priority System**: Heuristic-based prioritization
363377
- Helps focus improvement efforts on most impactful areas
364378
- Based on visibility (public/private) and usage frequency
@@ -411,15 +425,32 @@ The analyzer codebase maintains high documentation standards:
411425

412426
If you'd like to improve the type hint analyzer:
413427

414-
1. The main implementation is in `type_hint_analyzer.py`
415-
2. The CSV generator is in `generate_csv.py`
428+
1. The main implementation is in `type-analyzer.py`
429+
2. The CSV generator is in `gen-csv.py`
416430
3. Both scripts follow PyThaiNLP coding standards
417431
4. Run Ruff before submitting changes: `ruff check build_tools/analysis/`
418432
5. Ensure all docstrings are complete and follow reStructuredText format
419433
6. Test changes by running the analyzer on the full repository
420434

421435
For questions or suggestions, please open an issue in the PyThaiNLP repository.
422436

437+
## Understanding variable type annotations
438+
439+
### What should be annotated
440+
441+
According to Python typing best practices and type checking tools like mypy,
442+
type annotations should be added to:
443+
444+
1. **First assignment** of a variable
445+
2. **Variables where type isn't obvious** from the assigned value
446+
3. **Class and instance variables** (on first assignment only)
447+
448+
### What should not be annotated
449+
450+
1. **Reassignments** - Adding type annotations to reassignments causes `no-redef` errors
451+
2. **Dictionary subscript operations** - Cannot annotate `dict[key] = value` operations
452+
3. **Variables with obvious literal types** - Optional, but generally omitted for simple cases
453+
423454
## Future Tools
424455

425456
This directory can be extended with additional analysis tools:

0 commit comments

Comments
 (0)