Skip to content

Commit e4083ae

Browse files
committed
more tests and fixes
1 parent 9f2aae0 commit e4083ae

File tree

8 files changed

+16
-21
lines changed

8 files changed

+16
-21
lines changed

crates/red_knot_python_semantic/resources/mdtest/annotations/stdlib_typing_aliases.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ reveal_type(ListSubclass.__mro__)
7575

7676
class DictSubclass(typing.Dict): ...
7777

78-
# TODO: should have `Generic`, should not have `Unknown`
79-
# revealed: tuple[Literal[DictSubclass], Literal[dict], Unknown, Literal[object]]
78+
# TODO
79+
# revealed: tuple[Literal[DictSubclass], Literal[dict], Literal[MutableMapping], Literal[Mapping], Literal[Collection], Literal[Iterable], Literal[Container], @Todo(`Protocol[]` subscript), @Todo(`Generic[]` subscript), Literal[object]]
8080
reveal_type(DictSubclass.__mro__)
8181

8282
class SetSubclass(typing.Set): ...
@@ -95,8 +95,8 @@ reveal_type(FrozenSetSubclass.__mro__)
9595

9696
class ChainMapSubclass(typing.ChainMap): ...
9797

98-
# TODO: Should be (ChainMapSubclass, ChainMap, MutableMapping, Mapping, Collection, Sized, Iterable, Container, Generic, object)
99-
# revealed: tuple[Literal[ChainMapSubclass], Literal[ChainMap], Unknown, Literal[object]]
98+
# TODO
99+
# revealed: tuple[Literal[ChainMapSubclass], Literal[ChainMap], Literal[MutableMapping], Literal[Mapping], Literal[Collection], Literal[Iterable], Literal[Container], @Todo(`Protocol[]` subscript), @Todo(`Generic[]` subscript), Literal[object]]
100100
reveal_type(ChainMapSubclass.__mro__)
101101

102102
class CounterSubclass(typing.Counter): ...

crates/red_knot_python_semantic/resources/mdtest/annotations/unsupported_special_forms.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class Foo:
4141
One thing that is supported is error messages for using special forms in type expressions.
4242

4343
```py
44-
from typing_extensions import Unpack, TypeGuard, TypeIs, Concatenate, ParamSpec
44+
from typing_extensions import Unpack, TypeGuard, TypeIs, Concatenate, ParamSpec, Generic
4545

4646
def _(
4747
a: Unpack, # error: [invalid-type-form] "`typing.Unpack` requires exactly one argument when used in a type expression"
4848
b: TypeGuard, # error: [invalid-type-form] "`typing.TypeGuard` requires exactly one argument when used in a type expression"
4949
c: TypeIs, # error: [invalid-type-form] "`typing.TypeIs` requires exactly one argument when used in a type expression"
5050
d: Concatenate, # error: [invalid-type-form] "`typing.Concatenate` requires at least two arguments when used in a type expression"
5151
e: ParamSpec,
52+
f: Generic # error: [invalid-type-form] "`typing.Generic` is not allowed in type expressions"
5253
) -> None:
5354
reveal_type(a) # revealed: Unknown
5455
reveal_type(b) # revealed: Unknown
@@ -65,14 +66,15 @@ You can't inherit from most of these. `typing.Callable` is an exception.
6566

6667
```py
6768
from typing import Callable
68-
from typing_extensions import Self, Unpack, TypeGuard, TypeIs, Concatenate
69+
from typing_extensions import Self, Unpack, TypeGuard, TypeIs, Concatenate, Generic
6970

7071
class A(Self): ... # error: [invalid-base]
7172
class B(Unpack): ... # error: [invalid-base]
7273
class C(TypeGuard): ... # error: [invalid-base]
7374
class D(TypeIs): ... # error: [invalid-base]
7475
class E(Concatenate): ... # error: [invalid-base]
7576
class F(Callable): ...
77+
class G(Generic): ... # TODO should emit an error here
7678

7779
reveal_type(F.__mro__) # revealed: tuple[Literal[F], @Todo(Support for Callable as a base class), Literal[object]]
7880
```

crates/red_knot_python_semantic/resources/mdtest/generics/classes.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ from typing import Generic, TypeVar
4040

4141
T = TypeVar("T")
4242

43-
# TODO: no error
44-
# error: [invalid-base]
4543
class C(Generic[T]): ...
4644
```
4745

4846
A class that inherits from a generic class, and fills its type parameters with typevars, is generic.
4947

5048
```py
51-
class D(C[T]): ...
49+
# TODO: no error
50+
class D(C[T]): ... # error: [non-subscriptable]
5251
```
5352

5453
(Examples `E` and `F` from above do not have analogues in the legacy syntax.)

crates/red_knot_python_semantic/resources/mdtest/generics/scoping.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ from typing import TypeVar, Generic
132132
T = TypeVar("T")
133133
S = TypeVar("S")
134134

135-
# TODO: no error
136-
# error: [invalid-base]
137135
class Legacy(Generic[T]):
138136
def m(self, x: T, y: S) -> S:
139137
return y
@@ -172,8 +170,6 @@ def f(x: T) -> None:
172170
# TODO: error
173171
y: list[S] = []
174172

175-
# TODO: no error
176-
# error: [invalid-base]
177173
class C(Generic[T]):
178174
# TODO: error
179175
x: list[S] = []

crates/red_knot_python_semantic/resources/mdtest/protocols.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ class Fine(Protocol, object): ...
127127

128128
reveal_type(Fine.__mro__) # revealed: tuple[Literal[Fine], typing.Protocol, typing.Generic, Literal[object]]
129129

130-
# TODO: should not error
131-
class StillFine(Protocol, Generic[T], object): ... # error: [invalid-base]
130+
class StillFine(Protocol, Generic[T], object): ...
132131
class EvenThis[T](Protocol, object): ...
133132
```
134133

crates/red_knot_python_semantic/src/types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use itertools::Either;
2-
use subclass_of::SubclassOfInner;
32

43
use std::slice::Iter;
54
use std::str::FromStr;
@@ -27,7 +26,7 @@ pub(crate) use self::infer::{
2726
};
2827
pub(crate) use self::narrow::KnownConstraintFunction;
2928
pub(crate) use self::signatures::{CallableSignature, Signature, Signatures};
30-
pub(crate) use self::subclass_of::SubclassOfType;
29+
pub(crate) use self::subclass_of::{SubclassOfInner, SubclassOfType};
3130
use crate::module_name::ModuleName;
3231
use crate::module_resolver::{file_to_module, resolve_module, KnownModule};
3332
use crate::semantic_index::ast_ids::HasScopedExpressionId;

crates/red_knot_python_semantic/src/types/class.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,7 @@ impl<'db> KnownInstanceType<'db> {
25052505
"Counter" => Self::Counter,
25062506
"ChainMap" => Self::ChainMap,
25072507
"OrderedDict" => Self::OrderedDict,
2508+
"Generic" => Self::Generic,
25082509
"Protocol" => Self::Protocol,
25092510
"Optional" => Self::Optional,
25102511
"Union" => Self::Union,

crates/red_knot_python_semantic/src/types/display.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ use crate::types::class::{ClassType, GenericAlias, GenericClass};
1010
use crate::types::generics::{GenericContext, Specialization};
1111
use crate::types::signatures::{Parameter, Parameters, Signature};
1212
use crate::types::{
13-
InstanceType, IntersectionType, KnownClass, MethodWrapperKind, StringLiteralType, Type,
14-
TypeVarBoundOrConstraints, TypeVarInstance, UnionType, WrapperDescriptorKind,
13+
InstanceType, IntersectionType, KnownClass, MethodWrapperKind, StringLiteralType,
14+
SubclassOfInner, Type, TypeVarBoundOrConstraints, TypeVarInstance, UnionType,
15+
WrapperDescriptorKind,
1516
};
1617
use crate::Db;
1718
use rustc_hash::FxHashMap;
1819

19-
use super::subclass_of::SubclassOfInner;
20-
2120
impl<'db> Type<'db> {
2221
pub fn display(&self, db: &'db dyn Db) -> DisplayType {
2322
DisplayType { ty: self, db }

0 commit comments

Comments
 (0)