Skip to content

Commit

Permalink
[red-knot] Add typing.Any as a spelling for the Any type (#14742)
Browse files Browse the repository at this point in the history
We already had a representation for the Any type, which we would use
e.g. for expressions without type annotations. We now recognize
`typing.Any` as a way to refer to this type explicitly. Like other
special forms, this is tracked correctly through aliasing, and isn't
confused with local definitions that happen to have the same name.

Closes #14544
  • Loading branch information
dcreager authored Dec 4, 2024
1 parent 948549f commit 8b23086
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Any

## Annotation

`typing.Any` is a way to name the Any type.

```py
from typing import Any

x: Any = 1
x = "foo"

def f():
reveal_type(x) # revealed: Any
```

## Aliased to a different name

If you alias `typing.Any` to another name, we still recognize that as a spelling of the Any type.

```py
from typing import Any as RenamedAny

x: RenamedAny = 1
x = "foo"

def f():
reveal_type(x) # revealed: Any
```

## Shadowed class

If you define your own class named `Any`, using that in a type expression refers to your class, and
isn't a spelling of the Any type.

```py
class Any:
pass

x: Any

def f():
reveal_type(x) # revealed: Any

# This verifies that we're not accidentally seeing typing.Any, since str is assignable
# to that but not to our locally defined class.
y: Any = "not an Any" # error: [invalid-assignment]
```

## Subclass

The spec allows you to define subclasses of `Any`.

TODO: Handle assignments correctly. `Subclass` has an unknown superclass, which might be `int`. The
assignment to `x` should not be allowed, even when the unknown superclass is `int`. The assignment
to `y` should be allowed, since `Subclass` might have `int` as a superclass, and is therefore
assignable to `int`.

```py
from typing import Any

class Subclass(Any):
pass

reveal_type(Subclass.__mro__) # revealed: tuple[Literal[Subclass], Any, Literal[object]]

x: Subclass = 1 # error: [invalid-assignment]
# TODO: no diagnostic
y: int = Subclass() # error: [invalid-assignment]

def f() -> Subclass:
pass

reveal_type(f()) # revealed: Subclass
```
14 changes: 13 additions & 1 deletion crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ impl<'db> Type<'db> {
Type::Never
}
Type::KnownInstance(KnownInstanceType::LiteralString) => Type::LiteralString,
Type::KnownInstance(KnownInstanceType::Any) => Type::Any,
_ => todo_type!(),
}
}
Expand Down Expand Up @@ -1902,6 +1903,8 @@ pub enum KnownInstanceType<'db> {
NoReturn,
/// The symbol `typing.Never` available since 3.11 (which can also be found as `typing_extensions.Never`)
Never,
/// The symbol `typing.Any` (which can also be found as `typing_extensions.Any`)
Any,
/// A single instance of `typing.TypeVar`
TypeVar(TypeVarInstance<'db>),
/// A single instance of `typing.TypeAliasType` (PEP 695 type alias)
Expand All @@ -1919,6 +1922,7 @@ impl<'db> KnownInstanceType<'db> {
Self::TypeVar(_) => "TypeVar",
Self::NoReturn => "NoReturn",
Self::Never => "Never",
Self::Any => "Any",
Self::TypeAliasType(_) => "TypeAliasType",
}
}
Expand All @@ -1933,6 +1937,7 @@ impl<'db> KnownInstanceType<'db> {
| Self::Union
| Self::NoReturn
| Self::Never
| Self::Any
| Self::TypeAliasType(_) => Truthiness::AlwaysTrue,
}
}
Expand All @@ -1946,6 +1951,7 @@ impl<'db> KnownInstanceType<'db> {
Self::Union => "typing.Union",
Self::NoReturn => "typing.NoReturn",
Self::Never => "typing.Never",
Self::Any => "typing.Any",
Self::TypeVar(typevar) => typevar.name(db),
Self::TypeAliasType(_) => "typing.TypeAliasType",
}
Expand All @@ -1960,6 +1966,7 @@ impl<'db> KnownInstanceType<'db> {
Self::Union => KnownClass::SpecialForm,
Self::NoReturn => KnownClass::SpecialForm,
Self::Never => KnownClass::SpecialForm,
Self::Any => KnownClass::Object,
Self::TypeVar(_) => KnownClass::TypeVar,
Self::TypeAliasType(_) => KnownClass::TypeAliasType,
}
Expand All @@ -1979,6 +1986,7 @@ impl<'db> KnownInstanceType<'db> {
return None;
}
match (module.name().as_str(), instance_name) {
("typing", "Any") => Some(Self::Any),
("typing" | "typing_extensions", "Literal") => Some(Self::Literal),
("typing" | "typing_extensions", "LiteralString") => Some(Self::LiteralString),
("typing" | "typing_extensions", "Optional") => Some(Self::Optional),
Expand Down Expand Up @@ -2647,7 +2655,11 @@ impl<'db> Class<'db> {
pub fn is_subclass_of(self, db: &'db dyn Db, other: Class) -> bool {
// `is_subclass_of` is checking the subtype relation, in which gradual types do not
// participate, so we should not return `True` if we find `Any/Unknown` in the MRO.
self.iter_mro(db).contains(&ClassBase::Class(other))
self.is_subclass_of_base(db, other)
}

fn is_subclass_of_base(self, db: &'db dyn Db, other: impl Into<ClassBase<'db>>) -> bool {
self.iter_mro(db).contains(&other.into())
}

/// Return the explicit `metaclass` of this class, if one is defined.
Expand Down
10 changes: 9 additions & 1 deletion crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ impl<'db> TypeInferenceBuilder<'db> {
let value_ty = self.expression_ty(value);
let name_ast_id = name.scoped_expression_id(self.db, self.scope());

let target_ty = match assignment.target() {
let mut target_ty = match assignment.target() {
TargetKind::Sequence(unpack) => {
let unpacked = infer_unpack_types(self.db, unpack);
// Only copy the diagnostics if this is the first assignment to avoid duplicating the
Expand All @@ -1674,6 +1674,13 @@ impl<'db> TypeInferenceBuilder<'db> {
TargetKind::Name => value_ty,
};

if let Some(known_instance) = file_to_module(self.db, definition.file(self.db))
.as_ref()
.and_then(|module| KnownInstanceType::try_from_module_and_symbol(module, &name.id))
{
target_ty = Type::KnownInstance(known_instance);
}

self.store_expression_type(name, target_ty);
self.add_binding(name.into(), definition, target_ty);
}
Expand Down Expand Up @@ -4653,6 +4660,7 @@ impl<'db> TypeInferenceBuilder<'db> {
);
Type::Unknown
}
KnownInstanceType::Any => Type::Any,
}
}

Expand Down
7 changes: 7 additions & 0 deletions crates/red_knot_python_semantic/src/types/mro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ impl<'db> ClassBase<'db> {
| KnownInstanceType::NoReturn
| KnownInstanceType::Never
| KnownInstanceType::Optional => None,
KnownInstanceType::Any => Some(Self::Any),
},
}
}
Expand Down Expand Up @@ -406,6 +407,12 @@ impl<'db> ClassBase<'db> {
}
}

impl<'db> From<Class<'db>> for ClassBase<'db> {
fn from(value: Class<'db>) -> Self {
ClassBase::Class(value)
}
}

impl<'db> From<ClassBase<'db>> for Type<'db> {
fn from(value: ClassBase<'db>) -> Self {
match value {
Expand Down

0 comments on commit 8b23086

Please sign in to comment.