File tree Expand file tree Collapse file tree 2 files changed +22
-9
lines changed
crates/red_knot_python_semantic Expand file tree Collapse file tree 2 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,18 @@ def variable(x: int):
55
55
reveal_type(x** x) # revealed: @Todo(return type of overloaded function)
56
56
```
57
57
58
+ If the second argument is <0, a ` float ` is returned at runtime. If the first
59
+ argument is <0 but the second argument is >=0, an ` int ` is still returned:
60
+
61
+ ``` py
62
+ reveal_type(1 ** 0 ) # revealed: Literal[1]
63
+ reveal_type(0 ** 1 ) # revealed: Literal[0]
64
+ reveal_type(0 ** 0 ) # revealed: Literal[1]
65
+ reveal_type((- 1 ) ** 2 ) # revealed: Literal[1]
66
+ reveal_type(2 ** (- 1 )) # revealed: float
67
+ reveal_type((- 1 ) ** (- 1 )) # revealed: float
68
+ ```
69
+
58
70
## Division by Zero
59
71
60
72
This error is really outside the current Python type system, because e.g. ` int.__truediv__ ` and
Original file line number Diff line number Diff line change @@ -4615,16 +4615,17 @@ impl<'db> TypeInferenceBuilder<'db> {
4615
4615
. unwrap_or_else ( || KnownClass :: Int . to_instance ( self . db ( ) ) ) ,
4616
4616
) ,
4617
4617
4618
- ( Type :: IntLiteral ( n) , Type :: IntLiteral ( m) , ast:: Operator :: Pow ) => {
4619
- let m = u32:: try_from ( m) ;
4620
- Some ( match m {
4621
- Ok ( m) => n
4622
- . checked_pow ( m)
4618
+ ( Type :: IntLiteral ( n) , Type :: IntLiteral ( m) , ast:: Operator :: Pow ) => Some ( {
4619
+ if m < 0 {
4620
+ KnownClass :: Float . to_instance ( self . db ( ) )
4621
+ } else {
4622
+ u32:: try_from ( m)
4623
+ . ok ( )
4624
+ . and_then ( |m| n. checked_pow ( m) )
4623
4625
. map ( Type :: IntLiteral )
4624
- . unwrap_or_else ( || KnownClass :: Int . to_instance ( self . db ( ) ) ) ,
4625
- Err ( _) => KnownClass :: Int . to_instance ( self . db ( ) ) ,
4626
- } )
4627
- }
4626
+ . unwrap_or_else ( || KnownClass :: Int . to_instance ( self . db ( ) ) )
4627
+ }
4628
+ } ) ,
4628
4629
4629
4630
( Type :: BytesLiteral ( lhs) , Type :: BytesLiteral ( rhs) , ast:: Operator :: Add ) => {
4630
4631
let bytes = [ & * * lhs. value ( self . db ( ) ) , & * * rhs. value ( self . db ( ) ) ] . concat ( ) ;
You can’t perform that action at this time.
0 commit comments