Skip to content

Commit 2e7ab00

Browse files
authored
[ty] Allow values of type None in type expressions (#21263)
## Summary Allow values of type `None` in type expressions. The [typing spec](https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions) could be more explicit on whether this is actually allowed or not, but it seems relatively harmless and does help in some use cases like: ```py try: from module import MyClass except ImportError: MyClass = None # ty: ignore def f(m: MyClass): pass ``` ## Test Plan Updated tests, ecosystem check.
1 parent d8106d3 commit 2e7ab00

File tree

2 files changed

+2
-4
lines changed

2 files changed

+2
-4
lines changed

crates/ty_python_semantic/resources/mdtest/implicit_type_aliases.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ f(1)
2222
```py
2323
MyNone = None
2424

25-
# TODO: this should not be an error
26-
# error: [invalid-type-form] "Variable of type `None` is not allowed in a type expression"
2725
def g(x: MyNone):
28-
# TODO: this should be `None`
29-
reveal_type(x) # revealed: Unknown
26+
reveal_type(x) # revealed: None
3027

3128
g(None)
3229
```

crates/ty_python_semantic/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6600,6 +6600,7 @@ impl<'db> Type<'db> {
66006600
Type::Dynamic(_) => Ok(*self),
66016601

66026602
Type::NominalInstance(instance) => match instance.known_class(db) {
6603+
Some(KnownClass::NoneType) => Ok(Type::none(db)),
66036604
Some(KnownClass::TypeVar) => Ok(todo_type!(
66046605
"Support for `typing.TypeVar` instances in type expressions"
66056606
)),

0 commit comments

Comments
 (0)