Skip to content

Commit bb6f05b

Browse files
committed
Include same scope
1 parent 8c4b4a2 commit bb6f05b

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ type Redeclared = int
153153
type Redeclared = str
154154
```
155155

156+
```py
157+
from random import random
158+
159+
flag = random() > 0.5
160+
if flag:
161+
type BranchRedeclared = int
162+
else:
163+
# error: [invalid-type-alias] "Type alias `BranchRedeclared` is already defined in this scope"
164+
type BranchRedeclared = str
165+
```
166+
156167
## Generic type aliases
157168

158169
```py

crates/ty_python_semantic/resources/mdtest/snapshots/pep695_type_aliases.…_-_PEP_695_type_aliases_-_Redeclared_aliases_(7e8460432a17eeae).snap

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/pep695_type_aliases.md
1313
## mdtest_snippet.py
1414

1515
```
16-
1 | type Redeclared = int
17-
2 | # error: [invalid-type-alias] "Type alias `Redeclared` is already defined in this scope"
18-
3 | type Redeclared = str
16+
1 | type Redeclared = int
17+
2 | # error: [invalid-type-alias] "Type alias `Redeclared` is already defined in this scope"
18+
3 | type Redeclared = str
19+
4 | from random import random
20+
5 |
21+
6 | flag = random() > 0.5
22+
7 | if flag:
23+
8 | type BranchRedeclared = int
24+
9 | else:
25+
10 | # error: [invalid-type-alias] "Type alias `BranchRedeclared` is already defined in this scope"
26+
11 | type BranchRedeclared = str
1927
```
2028

2129
# Diagnostics
@@ -29,7 +37,25 @@ error[invalid-type-alias]: Type alias `Redeclared` is already defined in this sc
2937
2 | # error: [invalid-type-alias] "Type alias `Redeclared` is already defined in this scope"
3038
3 | type Redeclared = str
3139
| ^^^^^^^^^^
40+
4 | from random import random
3241
|
3342
info: rule `invalid-type-alias` is enabled by default
3443

3544
```
45+
46+
```
47+
error[invalid-type-alias]: Type alias `BranchRedeclared` is already defined in this scope
48+
--> src/mdtest_snippet.py:8:10
49+
|
50+
6 | flag = random() > 0.5
51+
7 | if flag:
52+
8 | type BranchRedeclared = int
53+
| ---------------- `BranchRedeclared` previously defined here
54+
9 | else:
55+
10 | # error: [invalid-type-alias] "Type alias `BranchRedeclared` is already defined in this scope"
56+
11 | type BranchRedeclared = str
57+
| ^^^^^^^^^^^^^^^^
58+
|
59+
info: rule `invalid-type-alias` is enabled by default
60+
61+
```

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,15 +1510,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
15101510
let db = self.db();
15111511
let scope = definition.scope(db);
15121512
let use_def = self.index.use_def_map(scope.file_scope_id(db));
1513-
let use_id = type_alias_name.scoped_use_id(db, scope);
15141513

1514+
// Type alias redeclarations are a scope-level property, so we need all earlier
1515+
// definitions for this symbol in the scope, not just the bindings reachable here.
15151516
let Some(previous_definition) = use_def
1516-
.bindings_at_use(use_id)
1517-
.filter_map(|binding| binding.binding.definition())
1518-
.filter(|definition| definition.scope(db) == scope)
1519-
.filter(|definition| {
1517+
.all_definitions_with_usage()
1518+
.filter_map(|(_, state, _)| state.definition())
1519+
.filter(|previous_definition| previous_definition.place(db) == definition.place(db))
1520+
.filter(|previous_definition| {
15201521
matches!(
1521-
definition.kind(db),
1522+
previous_definition.kind(db),
15221523
DefinitionKind::TypeAlias(previous_type_alias)
15231524
if previous_type_alias
15241525
.node(self.module())

0 commit comments

Comments
 (0)