mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 12:16:43 +00:00
[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.
This commit is contained in:
parent
d8106d38a0
commit
2e7ab00d51
2 changed files with 2 additions and 4 deletions
|
|
@ -22,11 +22,8 @@ f(1)
|
|||
```py
|
||||
MyNone = None
|
||||
|
||||
# TODO: this should not be an error
|
||||
# error: [invalid-type-form] "Variable of type `None` is not allowed in a type expression"
|
||||
def g(x: MyNone):
|
||||
# TODO: this should be `None`
|
||||
reveal_type(x) # revealed: Unknown
|
||||
reveal_type(x) # revealed: None
|
||||
|
||||
g(None)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -6600,6 +6600,7 @@ impl<'db> Type<'db> {
|
|||
Type::Dynamic(_) => Ok(*self),
|
||||
|
||||
Type::NominalInstance(instance) => match instance.known_class(db) {
|
||||
Some(KnownClass::NoneType) => Ok(Type::none(db)),
|
||||
Some(KnownClass::TypeVar) => Ok(todo_type!(
|
||||
"Support for `typing.TypeVar` instances in type expressions"
|
||||
)),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue