Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Understand other legacy typing aliases #14998

Merged
merged 9 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,59 @@
The `typing` module has various aliases to other stdlib classes. These are a legacy feature, but
still need to be supported by a type checker.

## Currently unsupported
## Correspondence

Support for most of these symbols is currently a TODO:
All of the following symbols can be mapped one-to-one with the actual type:

```py
import typing

def f(
a: typing.List,
b: typing.List[int],
c: typing.Dict,
d: typing.Dict[int, str],
e: typing.DefaultDict,
f: typing.DefaultDict[str, int],
g: typing.Set,
h: typing.Set[int],
i: typing.FrozenSet,
j: typing.FrozenSet[str],
k: typing.OrderedDict,
l: typing.OrderedDict[int, str],
m: typing.Counter,
n: typing.Counter[int],
list_bare: typing.List,
list_parametrized: typing.List[int],
dict_bare: typing.Dict,
dict_parametrized: typing.Dict[int, str],
set_bare: typing.Set,
set_parametrized: typing.Set[int],
frozen_set_bare: typing.FrozenSet,
frozen_set_parametrized: typing.FrozenSet[str],
chain_map_bare: typing.ChainMap,
chain_map_parametrized: typing.ChainMap[int],
counter_bare: typing.Counter,
counter_parametrized: typing.Counter[int],
default_dict_bare: typing.DefaultDict,
default_dict_parametrized: typing.DefaultDict[str, int],
deque_bare: typing.Deque,
deque_parametrized: typing.Deque[str],
ordered_dict_bare: typing.OrderedDict,
ordered_dict_parametrized: typing.OrderedDict[int, str],
):
reveal_type(a) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(b) # revealed: @Todo(typing.List alias)
reveal_type(c) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(d) # revealed: @Todo(typing.Dict alias)
reveal_type(e) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(f) # revealed: @Todo(typing.DefaultDict[] alias)
reveal_type(g) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(h) # revealed: @Todo(typing.Set alias)
reveal_type(i) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(j) # revealed: @Todo(typing.FrozenSet alias)
reveal_type(k) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(l) # revealed: @Todo(typing.OrderedDict alias)
reveal_type(m) # revealed: @Todo(Unsupported or invalid type in a type expression)
reveal_type(n) # revealed: @Todo(typing.Counter[] alias)
reveal_type(list_bare) # revealed: list
reveal_type(list_parametrized) # revealed: list

reveal_type(dict_bare) # revealed: dict
reveal_type(dict_parametrized) # revealed: dict

reveal_type(set_bare) # revealed: set
reveal_type(set_parametrized) # revealed: set

reveal_type(frozen_set_bare) # revealed: frozenset
reveal_type(frozen_set_parametrized) # revealed: frozenset

reveal_type(chain_map_bare) # revealed: ChainMap
reveal_type(chain_map_parametrized) # revealed: ChainMap

reveal_type(counter_bare) # revealed: Counter
reveal_type(counter_parametrized) # revealed: Counter

reveal_type(default_dict_bare) # revealed: defaultdict
reveal_type(default_dict_parametrized) # revealed: defaultdict

reveal_type(deque_bare) # revealed: deque
reveal_type(deque_parametrized) # revealed: deque

reveal_type(ordered_dict_bare) # revealed: OrderedDict
reveal_type(ordered_dict_parametrized) # revealed: OrderedDict
```

## Inheritance
Expand All @@ -49,35 +65,59 @@ The aliases can be inherited from. Some of these are still partially or wholly T
```py
import typing

class A(typing.Dict): ...
####################
### Built-ins

class _List(typing.List): ...
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved

# TODO: should have `Generic`, should not have `Unknown`
reveal_type(A.__mro__) # revealed: tuple[Literal[A], Literal[dict], Unknown, Literal[object]]
reveal_type(_List.__mro__) # revealed: tuple[Literal[_List], Literal[list], Unknown, Literal[object]]

class B(typing.List): ...
class _Dict(typing.Dict): ...

# TODO: should have `Generic`, should not have `Unknown`
reveal_type(B.__mro__) # revealed: tuple[Literal[B], Literal[list], Unknown, Literal[object]]
reveal_type(_Dict.__mro__) # revealed: tuple[Literal[_Dict], Literal[dict], Unknown, Literal[object]]

class C(typing.Set): ...
class _Set(typing.Set): ...

# TODO: should have `Generic`, should not have `Unknown`
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Literal[set], Unknown, Literal[object]]
reveal_type(_Set.__mro__) # revealed: tuple[Literal[_Set], Literal[set], Unknown, Literal[object]]

class D(typing.FrozenSet): ...
class _FrozenSet(typing.FrozenSet): ...

# TODO: should have `Generic`, should not have `Unknown`
reveal_type(D.__mro__) # revealed: tuple[Literal[D], Literal[frozenset], Unknown, Literal[object]]
reveal_type(_FrozenSet.__mro__) # revealed: tuple[Literal[_FrozenSet], Literal[frozenset], Unknown, Literal[object]]

####################
### `collections`

class _ChainMap(typing.ChainMap): ...

# TODO: Should be (_ChainMap, ChainMap, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
# revealed: tuple[Literal[_ChainMap], Literal[ChainMap], Unknown, Literal[object]]
reveal_type(_ChainMap.__mro__)

class _Counter(typing.Counter): ...

# TODO: Should be (_Counter, Counter, dict, Generic, object)
# revealed: tuple[Literal[_Counter], Literal[Counter], Unknown, Literal[object]]
reveal_type(_Counter.__mro__)

class E(typing.DefaultDict): ...
class _DefaultDict(typing.DefaultDict): ...

reveal_type(E.__mro__) # revealed: tuple[Literal[E], @Todo(Support for more typing aliases as base classes), Literal[object]]
# TODO: Should be (_DefaultDict, defaultdict, dict, Generic, object)
# revealed: tuple[Literal[_DefaultDict], Literal[defaultdict], Unknown, Literal[object]]
reveal_type(_DefaultDict.__mro__)

class F(typing.OrderedDict): ...
class _Deque(typing.Deque): ...

reveal_type(F.__mro__) # revealed: tuple[Literal[F], @Todo(Support for more typing aliases as base classes), Literal[object]]
# TODO: Should be (_Deque, deque, Generic, object)
# revealed: tuple[Literal[_Deque], Literal[deque], Unknown, Literal[object]]
reveal_type(_Deque.__mro__)

class G(typing.Counter): ...
class _OrderedDict(typing.OrderedDict): ...

reveal_type(G.__mro__) # revealed: tuple[Literal[G], @Todo(Support for more typing aliases as base classes), Literal[object]]
# TODO: Should be (_OrderedDict, OrderedDict, dict, Generic, object)
# revealed: tuple[Literal[_OrderedDict], Literal[OrderedDict], Unknown, Literal[object]]
reveal_type(_OrderedDict.__mro__)
```
2 changes: 2 additions & 0 deletions crates/red_knot_python_semantic/src/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) enum CoreStdlibModule {
Sys,
#[allow(dead_code)]
Abc, // currently only used in tests
Collections,
}

impl CoreStdlibModule {
Expand All @@ -29,6 +30,7 @@ impl CoreStdlibModule {
Self::TypingExtensions => "typing_extensions",
Self::Sys => "sys",
Self::Abc => "abc",
Self::Collections => "collections",
}
}

Expand Down
85 changes: 76 additions & 9 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,11 @@ impl<'db> Type<'db> {
| KnownClass::ModuleType
| KnownClass::FunctionType
| KnownClass::SpecialForm
| KnownClass::ChainMap
| KnownClass::Counter
| KnownClass::DefaultDict
| KnownClass::Deque
| KnownClass::OrderedDict
| KnownClass::StdlibAlias
| KnownClass::TypeVar,
) => false,
Expand Down Expand Up @@ -1892,6 +1897,28 @@ impl<'db> Type<'db> {
// We treat `typing.Type` exactly the same as `builtins.type`:
Type::KnownInstance(KnownInstanceType::Type) => Ok(KnownClass::Type.to_instance(db)),
Type::KnownInstance(KnownInstanceType::Tuple) => Ok(KnownClass::Tuple.to_instance(db)),

// Legacy `typing` aliases
Type::KnownInstance(KnownInstanceType::List) => Ok(KnownClass::List.to_instance(db)),
Type::KnownInstance(KnownInstanceType::Dict) => Ok(KnownClass::Dict.to_instance(db)),
Type::KnownInstance(KnownInstanceType::Set) => Ok(KnownClass::Set.to_instance(db)),
Type::KnownInstance(KnownInstanceType::FrozenSet) => {
Ok(KnownClass::FrozenSet.to_instance(db))
}
Type::KnownInstance(KnownInstanceType::ChainMap) => {
Ok(KnownClass::ChainMap.to_instance(db))
}
Type::KnownInstance(KnownInstanceType::Counter) => {
Ok(KnownClass::Counter.to_instance(db))
}
Type::KnownInstance(KnownInstanceType::DefaultDict) => {
Ok(KnownClass::DefaultDict.to_instance(db))
}
Type::KnownInstance(KnownInstanceType::Deque) => Ok(KnownClass::Deque.to_instance(db)),
Type::KnownInstance(KnownInstanceType::OrderedDict) => {
Ok(KnownClass::OrderedDict.to_instance(db))
}

Type::Union(union) => {
let mut builder = UnionBuilder::new(db);
let mut invalid_expressions = smallvec::SmallVec::default();
Expand Down Expand Up @@ -2153,6 +2180,12 @@ pub enum KnownClass {
TypeVar,
TypeAliasType,
NoDefaultType,
// Collections
ChainMap,
Counter,
DefaultDict,
Deque,
OrderedDict,
// sys
VersionInfo,
}
Expand Down Expand Up @@ -2183,6 +2216,11 @@ impl<'db> KnownClass {
Self::TypeVar => "TypeVar",
Self::TypeAliasType => "TypeAliasType",
Self::NoDefaultType => "_NoDefaultType",
Self::ChainMap => "ChainMap",
Self::Counter => "Counter",
Self::DefaultDict => "defaultdict",
Self::Deque => "deque",
Self::OrderedDict => "OrderedDict",
// For example, `typing.List` is defined as `List = _Alias()` in typeshed
Self::StdlibAlias => "_Alias",
// This is the name the type of `sys.version_info` has in typeshed,
Expand Down Expand Up @@ -2246,6 +2284,11 @@ impl<'db> KnownClass {
CoreStdlibModule::TypingExtensions
}
}
Self::ChainMap
| Self::Counter
| Self::DefaultDict
| Self::Deque
| Self::OrderedDict => CoreStdlibModule::Collections,
}
}

Expand Down Expand Up @@ -2273,6 +2316,11 @@ impl<'db> KnownClass {
| Self::ModuleType
| Self::FunctionType
| Self::SpecialForm
| Self::ChainMap
| Self::Counter
| Self::DefaultDict
| Self::Deque
| Self::OrderedDict
| Self::StdlibAlias
| Self::BaseException
| Self::BaseExceptionGroup
Expand Down Expand Up @@ -2305,6 +2353,11 @@ impl<'db> KnownClass {
"ModuleType" => Self::ModuleType,
"FunctionType" => Self::FunctionType,
"TypeAliasType" => Self::TypeAliasType,
"ChainMap" => Self::ChainMap,
"Counter" => Self::Counter,
"defaultdict" => Self::DefaultDict,
"deque" => Self::Deque,
"OrderedDict" => Self::OrderedDict,
"_Alias" => Self::StdlibAlias,
"_SpecialForm" => Self::SpecialForm,
"_NoDefaultType" => Self::NoDefaultType,
Expand Down Expand Up @@ -2336,6 +2389,11 @@ impl<'db> KnownClass {
| Self::Dict
| Self::Slice
| Self::GenericAlias
| Self::ChainMap
| Self::Counter
| Self::DefaultDict
| Self::Deque
| Self::OrderedDict
| Self::StdlibAlias // no equivalent class exists in typing_extensions, nor ever will
| Self::ModuleType
| Self::VersionInfo
Expand Down Expand Up @@ -2371,6 +2429,24 @@ pub enum KnownInstanceType<'db> {
Any,
/// The symbol `typing.Tuple` (which can also be found as `typing_extensions.Tuple`)
Tuple,
/// The symbol `typing.List` (which can also be found as `typing_extensions.List`)
List,
/// The symbol `typing.Dict` (which can also be found as `typing_extensions.Dict`)
Dict,
/// The symbol `typing.Set` (which can also be found as `typing_extensions.Set`)
Set,
/// The symbol `typing.FrozenSet` (which can also be found as `typing_extensions.FrozenSet`)
FrozenSet,
/// The symbol `typing.ChainMap` (which can also be found as `typing_extensions.ChainMap`)
ChainMap,
/// The symbol `typing.Counter` (which can also be found as `typing_extensions.Counter`)
Counter,
/// The symbol `typing.DefaultDict` (which can also be found as `typing_extensions.DefaultDict`)
DefaultDict,
/// The symbol `typing.Deque` (which can also be found as `typing_extensions.Deque`)
Deque,
/// The symbol `typing.OrderedDict` (which can also be found as `typing_extensions.OrderedDict`)
OrderedDict,
/// The symbol `typing.Type` (which can also be found as `typing_extensions.Type`)
Type,
/// A single instance of `typing.TypeVar`
Expand All @@ -2391,15 +2467,6 @@ pub enum KnownInstanceType<'db> {
TypeAlias,
TypeGuard,
TypeIs,
List,
Dict,
DefaultDict,
Set,
FrozenSet,
Counter,
Deque,
ChainMap,
OrderedDict,
ReadOnly,
// TODO: fill this enum out with more special forms, etc.
}
Expand Down
22 changes: 16 additions & 6 deletions crates/red_knot_python_semantic/src/types/class_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,22 @@ impl<'db> ClassBase<'db> {
KnownInstanceType::FrozenSet => {
Self::try_from_ty(db, KnownClass::FrozenSet.to_class_literal(db))
}
KnownInstanceType::Callable
| KnownInstanceType::ChainMap
| KnownInstanceType::Counter
| KnownInstanceType::DefaultDict
| KnownInstanceType::Deque
| KnownInstanceType::OrderedDict => Self::try_from_ty(
KnownInstanceType::ChainMap => {
Self::try_from_ty(db, KnownClass::ChainMap.to_class_literal(db))
}
KnownInstanceType::Counter => {
Self::try_from_ty(db, KnownClass::Counter.to_class_literal(db))
}
KnownInstanceType::DefaultDict => {
Self::try_from_ty(db, KnownClass::DefaultDict.to_class_literal(db))
}
KnownInstanceType::Deque => {
Self::try_from_ty(db, KnownClass::Deque.to_class_literal(db))
}
KnownInstanceType::OrderedDict => {
Self::try_from_ty(db, KnownClass::OrderedDict.to_class_literal(db))
}
KnownInstanceType::Callable => Self::try_from_ty(
db,
todo_type!("Support for more typing aliases as base classes"),
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved
),
Expand Down
Loading
Loading