File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed
crates/red_knot_python_semantic Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,11 @@ reveal_type(type(1)) # revealed: Literal[int]
26
26
But a three-argument call to type creates a dynamic instance of the ` type ` class:
27
27
28
28
``` py
29
+ class Base : ...
30
+
29
31
reveal_type(type (" Foo" , (), {})) # revealed: type
32
+
33
+ reveal_type(type (" Foo" , (Base,), {" attr" : 1 })) # revealed: type
30
34
```
31
35
32
36
Other numbers of arguments are invalid
@@ -38,3 +42,21 @@ type("Foo", ())
38
42
# error: [no-matching-overload] "No overload of class `type` matches arguments"
39
43
type (" Foo" , (), {}, weird_other_arg = 42 )
40
44
```
45
+
46
+ The following calls are also invalid, due to incorrect argument types:
47
+
48
+ ``` py
49
+ class Base : ...
50
+
51
+ # error: [no-matching-overload] "No overload of class `type` matches arguments"
52
+ type (b " Foo" , (), {})
53
+
54
+ # TODO : this should be an error
55
+ type (" Foo" , str , {})
56
+
57
+ # TODO : this should be an error
58
+ type (" Foo" , (1 , 2 ), {})
59
+
60
+ # TODO : this should be an error
61
+ type (" Foo" , (Base,), {b " attr" : 1 })
62
+ ```
Original file line number Diff line number Diff line change @@ -76,6 +76,9 @@ No narrowing should occur if `type` is used to dynamically create a class:
76
76
77
77
``` py
78
78
def _ (x : str | int ):
79
+ # The following diagnostic is valid, since the three-argument form of `type`
80
+ # can only be called with `str` as the first argument.
81
+ # error: [no-matching-overload] "No overload of class `type` matches arguments"
79
82
if type (x, (), {}) is str :
80
83
reveal_type(x) # revealed: str | int
81
84
else :
Original file line number Diff line number Diff line change @@ -2893,11 +2893,13 @@ impl<'db> Type<'db> {
2893
2893
) ,
2894
2894
Signature :: new (
2895
2895
Parameters :: new ( [
2896
- Parameter :: positional_only ( Some ( Name :: new_static ( "o " ) ) )
2897
- . with_annotated_type ( Type :: any ( ) ) ,
2896
+ Parameter :: positional_only ( Some ( Name :: new_static ( "name " ) ) )
2897
+ . with_annotated_type ( KnownClass :: Str . to_instance ( db ) ) ,
2898
2898
Parameter :: positional_only ( Some ( Name :: new_static ( "bases" ) ) )
2899
+ // TODO: Should be tuple[type, ...] once we have support for homogenous tuples
2899
2900
. with_annotated_type ( Type :: any ( ) ) ,
2900
2901
Parameter :: positional_only ( Some ( Name :: new_static ( "dict" ) ) )
2902
+ // TODO: Should be `dict[str, Any]` once we have support for generics
2901
2903
. with_annotated_type ( Type :: any ( ) ) ,
2902
2904
] ) ,
2903
2905
Some ( KnownClass :: Type . to_instance ( db) ) ,
You can’t perform that action at this time.
0 commit comments