Skip to content

Commit e5019b3

Browse files
authored
[ty] Support type[None] in type expressions (#22892)
1 parent 1986973 commit e5019b3

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

crates/ty_python_semantic/resources/mdtest/type_of/basic.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ def f(a: type[BasicUser | Union[ProUser, A.B.C]], b: type[Union[BasicUser | Unio
132132
reveal_type(b) # revealed: type[BasicUser | ProUser | C | str]
133133
```
134134

135+
## Special case for `None`
136+
137+
The typing conformance suite contains this test case. It's debatable whether it's correct to do so,
138+
since the spec states that:
139+
140+
> The value corresponding to `type[C]` must be an actual class object that’s a subtype of `C`, not a
141+
> special form or other kind of type.
142+
143+
However, for now we support this annotation in the way the conformance suite expects, until and
144+
unless the spec is amended.
145+
146+
```py
147+
import types
148+
149+
def f(x: type[None]):
150+
reveal_type(x) # revealed: <class 'NoneType'>
151+
152+
f(type(None))
153+
f(None.__class__)
154+
f(types.NoneType)
155+
f(None) # error: [invalid-argument-type]
156+
```
157+
135158
## Illegal parameters
136159

137160
```py

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
778778
}
779779
Type::unknown()
780780
}
781+
ast::Expr::NoneLiteral(_) => {
782+
self.infer_expression(slice, TypeContext::default());
783+
KnownClass::NoneType.to_subclass_of(self.db())
784+
}
781785
ast::Expr::Subscript(
782786
subscript @ ast::ExprSubscript {
783787
value,

0 commit comments

Comments
 (0)