Skip to content

Commit dd29f7f

Browse files
committed
ignore type aliases in pyrefly report #3150
1 parent 7c41594 commit dd29f7f

3 files changed

Lines changed: 184 additions & 3 deletions

File tree

pyrefly/lib/commands/report.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,20 @@ impl ReportArgs {
507507
_ => continue,
508508
};
509509
match binding {
510-
BindingExport::AnnotatedForward(annot_idx, _) => {
510+
BindingExport::AnnotatedForward(annot_idx, key_idx) => {
511+
// IMPLICIT: type aliases are type-level constructs with 0 slots.
512+
if matches!(
513+
bindings.get(*key_idx),
514+
Binding::TypeAlias(_) | Binding::TypeAliasRef(_)
515+
) {
516+
variables.push(Variable {
517+
name: qualified_name,
518+
annotation: None,
519+
slots: SlotCounts::default(),
520+
location,
521+
});
522+
continue;
523+
}
511524
let annotation_text = match bindings.get(*annot_idx) {
512525
BindingAnnotation::AnnotateExpr(_, expr, _) => {
513526
Some(module.code_at(expr.range()).to_owned())
@@ -531,8 +544,12 @@ impl ReportArgs {
531544
match bindings.get(*idx) {
532545
// Skip injected implicit globals
533546
Binding::Global(_) => {}
534-
// IMPLICIT: special type forms have 0 slots
535-
Binding::TypeVar(_) | Binding::ParamSpec(_) | Binding::TypeVarTuple(_) => {
547+
// IMPLICIT: special type forms and type aliases have 0 slots
548+
Binding::TypeVar(_)
549+
| Binding::ParamSpec(_)
550+
| Binding::TypeVarTuple(_)
551+
| Binding::TypeAlias(_)
552+
| Binding::TypeAliasRef(_) => {
536553
variables.push(Variable {
537554
name: qualified_name,
538555
annotation: None,
@@ -2248,4 +2265,12 @@ mod tests {
22482265
let report = build_module_report_for_test("partial_any.py");
22492266
compare_snapshot("partial_any.expected.json", &report);
22502267
}
2268+
2269+
/// Type aliases (explicit `TypeAlias`, bare assignments, PEP 695, TypeAliasType)
2270+
/// are type-level constructs and should have 0 typable slots.
2271+
#[test]
2272+
fn test_report_type_aliases() {
2273+
let report = build_module_report_for_test("type_aliases.py");
2274+
compare_snapshot("type_aliases.expected.json", &report);
2275+
}
22512276
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
{
2+
"name": "test",
3+
"names": [
4+
"test.T",
5+
"test.Alias1",
6+
"test.Alias2",
7+
"test.Alias3",
8+
"test.x",
9+
"test.y",
10+
"test.some_func",
11+
"test.SomeClass"
12+
],
13+
"line_count": 30,
14+
"symbol_reports": [
15+
{
16+
"kind": "attr",
17+
"name": "test.T",
18+
"n_typable": 0,
19+
"n_typed": 0,
20+
"n_any": 0,
21+
"n_untyped": 0,
22+
"location": {
23+
"line": 8,
24+
"column": 1
25+
}
26+
},
27+
{
28+
"kind": "attr",
29+
"name": "test.Alias1",
30+
"n_typable": 0,
31+
"n_typed": 0,
32+
"n_any": 0,
33+
"n_untyped": 0,
34+
"location": {
35+
"line": 11,
36+
"column": 1
37+
}
38+
},
39+
{
40+
"kind": "attr",
41+
"name": "test.Alias2",
42+
"n_typable": 0,
43+
"n_typed": 0,
44+
"n_any": 0,
45+
"n_untyped": 0,
46+
"location": {
47+
"line": 14,
48+
"column": 1
49+
}
50+
},
51+
{
52+
"kind": "attr",
53+
"name": "test.Alias3",
54+
"n_typable": 0,
55+
"n_typed": 0,
56+
"n_any": 0,
57+
"n_untyped": 0,
58+
"location": {
59+
"line": 17,
60+
"column": 1
61+
}
62+
},
63+
{
64+
"kind": "attr",
65+
"name": "test.x",
66+
"n_typable": 1,
67+
"n_typed": 1,
68+
"n_any": 0,
69+
"n_untyped": 0,
70+
"location": {
71+
"line": 20,
72+
"column": 1
73+
}
74+
},
75+
{
76+
"kind": "attr",
77+
"name": "test.y",
78+
"n_typable": 1,
79+
"n_typed": 1,
80+
"n_any": 0,
81+
"n_untyped": 0,
82+
"location": {
83+
"line": 21,
84+
"column": 1
85+
}
86+
},
87+
{
88+
"kind": "function",
89+
"name": "test.some_func",
90+
"n_typable": 1,
91+
"n_typed": 1,
92+
"n_any": 0,
93+
"n_untyped": 0,
94+
"location": {
95+
"line": 24,
96+
"column": 1
97+
}
98+
},
99+
{
100+
"kind": "class",
101+
"name": "test.SomeClass",
102+
"n_typable": 0,
103+
"n_typed": 0,
104+
"n_any": 0,
105+
"n_untyped": 0,
106+
"location": {
107+
"line": 28,
108+
"column": 1
109+
}
110+
}
111+
],
112+
"type_ignores": [],
113+
"n_typable": 3,
114+
"n_typed": 3,
115+
"n_any": 0,
116+
"n_untyped": 0,
117+
"coverage": 100.0,
118+
"strict_coverage": 100.0,
119+
"n_functions": 1,
120+
"n_methods": 0,
121+
"n_function_params": 0,
122+
"n_method_params": 0,
123+
"n_classes": 1,
124+
"n_attrs": 6,
125+
"n_properties": 0,
126+
"n_type_ignores": 0
127+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from typing import TypeAlias, TypeAliasType, TypeVar
7+
8+
T = TypeVar("T")
9+
10+
# Implicit type alias (bare assignment with recognizable type RHS)
11+
Alias1 = list[int]
12+
13+
# Explicit TypeAlias annotation (legacy form)
14+
Alias2: TypeAlias = int | str
15+
16+
# TypeAliasType call (runtime equivalent of PEP 695)
17+
Alias3 = TypeAliasType("Alias3", int)
18+
19+
# Regular typed variable for baseline comparison
20+
x: int = 42
21+
y: type[int] = int
22+
23+
24+
def some_func() -> None:
25+
pass
26+
27+
28+
class SomeClass:
29+
my_field = 42

0 commit comments

Comments
 (0)