@@ -62,11 +62,11 @@ use crate::types::mro::MroErrorKind;
62
62
use crate :: types:: unpacker:: { UnpackResult , Unpacker } ;
63
63
use crate :: types:: {
64
64
bindings_ty, builtins_symbol, declarations_ty, global_symbol, symbol, todo_type,
65
- typing_extensions_symbol, Boundness , Class , ClassLiteralType , FunctionType , InstanceType ,
66
- IntersectionBuilder , IntersectionType , IterationOutcome , KnownClass , KnownFunction ,
67
- KnownInstanceType , MetaclassCandidate , MetaclassErrorKind , SliceLiteralType , Symbol ,
68
- Truthiness , TupleType , Type , TypeAliasType , TypeArrayDisplay , TypeVarBoundOrConstraints ,
69
- TypeVarInstance , UnionBuilder , UnionType ,
65
+ typing_extensions_symbol, Boundness , CallDunderResult , Class , ClassLiteralType , FunctionType ,
66
+ InstanceType , IntersectionBuilder , IntersectionType , IterationOutcome , KnownClass ,
67
+ KnownFunction , KnownInstanceType , MetaclassCandidate , MetaclassErrorKind , SliceLiteralType ,
68
+ Symbol , Truthiness , TupleType , Type , TypeAliasType , TypeArrayDisplay ,
69
+ TypeVarBoundOrConstraints , TypeVarInstance , UnionBuilder , UnionType ,
70
70
} ;
71
71
use crate :: unpack:: Unpack ;
72
72
use crate :: util:: subscript:: { PyIndex , PySlice } ;
@@ -3201,6 +3201,11 @@ impl<'db> TypeInferenceBuilder<'db> {
3201
3201
let operand_type = self . infer_expression ( operand) ;
3202
3202
3203
3203
match ( op, operand_type) {
3204
+ ( _, Type :: Any ) => Type :: Any ,
3205
+ ( _, Type :: Todo ( _) ) => operand_type,
3206
+ ( _, Type :: Never ) => Type :: Never ,
3207
+ ( _, Type :: Unknown ) => Type :: Unknown ,
3208
+
3204
3209
( UnaryOp :: UAdd , Type :: IntLiteral ( value) ) => Type :: IntLiteral ( value) ,
3205
3210
( UnaryOp :: USub , Type :: IntLiteral ( value) ) => Type :: IntLiteral ( -value) ,
3206
3211
( UnaryOp :: Invert , Type :: IntLiteral ( value) ) => Type :: IntLiteral ( !value) ,
@@ -3210,11 +3215,23 @@ impl<'db> TypeInferenceBuilder<'db> {
3210
3215
( UnaryOp :: Invert , Type :: BooleanLiteral ( bool) ) => Type :: IntLiteral ( !i64:: from ( bool) ) ,
3211
3216
3212
3217
( UnaryOp :: Not , ty) => ty. bool ( self . db ( ) ) . negate ( ) . into_type ( self . db ( ) ) ,
3213
- ( _, Type :: Any ) => Type :: Any ,
3214
- ( _, Type :: Unknown ) => Type :: Unknown ,
3215
3218
(
3216
3219
op @ ( UnaryOp :: UAdd | UnaryOp :: USub | UnaryOp :: Invert ) ,
3217
- Type :: Instance ( InstanceType { class } ) ,
3220
+ Type :: FunctionLiteral ( _)
3221
+ | Type :: ModuleLiteral ( _)
3222
+ | Type :: ClassLiteral ( _)
3223
+ | Type :: SubclassOf ( _)
3224
+ | Type :: Instance ( _)
3225
+ | Type :: KnownInstance ( _)
3226
+ | Type :: Union ( _)
3227
+ | Type :: Intersection ( _)
3228
+ | Type :: AlwaysTruthy
3229
+ | Type :: AlwaysFalsy
3230
+ | Type :: StringLiteral ( _)
3231
+ | Type :: LiteralString
3232
+ | Type :: BytesLiteral ( _)
3233
+ | Type :: SliceLiteral ( _)
3234
+ | Type :: Tuple ( _) ,
3218
3235
) => {
3219
3236
let unary_dunder_method = match op {
3220
3237
UnaryOp :: Invert => "__invert__" ,
@@ -3225,11 +3242,10 @@ impl<'db> TypeInferenceBuilder<'db> {
3225
3242
}
3226
3243
} ;
3227
3244
3228
- if let Symbol :: Type ( class_member, _) =
3229
- class. class_member ( self . db ( ) , unary_dunder_method)
3245
+ if let CallDunderResult :: CallOutcome ( call)
3246
+ | CallDunderResult :: PossiblyUnbound ( call) =
3247
+ operand_type. call_dunder ( self . db ( ) , unary_dunder_method, & [ operand_type] )
3230
3248
{
3231
- let call = class_member. call ( self . db ( ) , & [ operand_type] ) ;
3232
-
3233
3249
match call. return_ty_result ( & self . context , AnyNodeRef :: ExprUnaryOp ( unary) ) {
3234
3250
Ok ( t) => t,
3235
3251
Err ( e) => {
@@ -3257,7 +3273,6 @@ impl<'db> TypeInferenceBuilder<'db> {
3257
3273
Type :: Unknown
3258
3274
}
3259
3275
}
3260
- _ => todo_type ! ( ) , // TODO other unary op types
3261
3276
}
3262
3277
}
3263
3278
0 commit comments