@@ -1468,7 +1468,7 @@ impl<'db> Type<'db> {
1468
1468
} ;
1469
1469
1470
1470
if let Ok ( Type :: BooleanLiteral ( bool_val) ) = bool_method
1471
- . call_bound ( db, instance_ty, & CallArguments :: positional ( [ ] ) )
1471
+ . try_call_bound ( db, instance_ty, & CallArguments :: positional ( [ ] ) )
1472
1472
. map ( |outcome| outcome. return_type ( db) )
1473
1473
{
1474
1474
bool_val. into ( )
@@ -1541,20 +1541,23 @@ impl<'db> Type<'db> {
1541
1541
return usize_len. try_into ( ) . ok ( ) . map ( Type :: IntLiteral ) ;
1542
1542
}
1543
1543
1544
- let return_ty = match self . call_dunder ( db, "__len__" , & CallArguments :: positional ( [ * self ] ) ) {
1545
- Ok ( outcome) | Err ( CallDunderError :: PossiblyUnbound ( outcome) ) => outcome. return_type ( db) ,
1544
+ let return_ty =
1545
+ match self . try_call_dunder ( db, "__len__" , & CallArguments :: positional ( [ * self ] ) ) {
1546
+ Ok ( outcome) | Err ( CallDunderError :: PossiblyUnbound ( outcome) ) => {
1547
+ outcome. return_type ( db)
1548
+ }
1546
1549
1547
- // TODO: emit a diagnostic
1548
- Err ( err) => err. return_type ( db) ?,
1549
- } ;
1550
+ // TODO: emit a diagnostic
1551
+ Err ( err) => err. return_type ( db) ?,
1552
+ } ;
1550
1553
1551
1554
non_negative_int_literal ( db, return_ty)
1552
1555
}
1553
1556
1554
1557
/// Calls `self`
1555
1558
///
1556
1559
/// Returns `Ok` if the call with the given arguments is successful and `Err` otherwise.
1557
- fn call (
1560
+ fn try_call (
1558
1561
self ,
1559
1562
db : & ' db dyn Db ,
1560
1563
arguments : & CallArguments < ' _ , ' db > ,
@@ -1672,7 +1675,7 @@ impl<'db> Type<'db> {
1672
1675
1673
1676
instance_ty @ Type :: Instance ( _) => {
1674
1677
instance_ty
1675
- . call_dunder ( db, "__call__" , & arguments. with_self ( instance_ty) )
1678
+ . try_call_dunder ( db, "__call__" , & arguments. with_self ( instance_ty) )
1676
1679
. map_err ( |err| match err {
1677
1680
CallDunderError :: Call ( CallError :: NotCallable { .. } ) => {
1678
1681
// Turn "`<type of illegal '__call__'>` not callable" into
@@ -1712,7 +1715,7 @@ impl<'db> Type<'db> {
1712
1715
Type :: Dynamic ( _) => Ok ( CallOutcome :: Single ( CallBinding :: from_return_type ( self ) ) ) ,
1713
1716
1714
1717
Type :: Union ( union) => {
1715
- CallOutcome :: try_call_union ( db, union, |element| element. call ( db, arguments) )
1718
+ CallOutcome :: try_call_union ( db, union, |element| element. try_call ( db, arguments) )
1716
1719
}
1717
1720
1718
1721
Type :: Intersection ( _) => Ok ( CallOutcome :: Single ( CallBinding :: from_return_type (
@@ -1731,7 +1734,7 @@ impl<'db> Type<'db> {
1731
1734
/// `receiver_ty` must be `Type::Instance(_)` or `Type::ClassLiteral`.
1732
1735
///
1733
1736
/// TODO: handle `super()` objects properly
1734
- fn call_bound (
1737
+ fn try_call_bound (
1735
1738
self ,
1736
1739
db : & ' db dyn Db ,
1737
1740
receiver_ty : & Type < ' db > ,
@@ -1743,16 +1746,16 @@ impl<'db> Type<'db> {
1743
1746
Type :: FunctionLiteral ( ..) => {
1744
1747
// Functions are always descriptors, so this would effectively call
1745
1748
// the function with the instance as the first argument
1746
- self . call ( db, & arguments. with_self ( * receiver_ty) )
1749
+ self . try_call ( db, & arguments. with_self ( * receiver_ty) )
1747
1750
}
1748
1751
1749
1752
Type :: Instance ( _) | Type :: ClassLiteral ( _) => {
1750
1753
// TODO descriptor protocol. For now, assume non-descriptor and call without `self` argument.
1751
- self . call ( db, arguments)
1754
+ self . try_call ( db, arguments)
1752
1755
}
1753
1756
1754
1757
Type :: Union ( union) => CallOutcome :: try_call_union ( db, union, |element| {
1755
- element. call_bound ( db, receiver_ty, arguments)
1758
+ element. try_call_bound ( db, receiver_ty, arguments)
1756
1759
} ) ,
1757
1760
1758
1761
Type :: Intersection ( _) => Ok ( CallOutcome :: Single ( CallBinding :: from_return_type (
@@ -1769,16 +1772,16 @@ impl<'db> Type<'db> {
1769
1772
}
1770
1773
1771
1774
/// Look up a dunder method on the meta type of `self` and call it.
1772
- fn call_dunder (
1775
+ fn try_call_dunder (
1773
1776
self ,
1774
1777
db : & ' db dyn Db ,
1775
1778
name : & str ,
1776
1779
arguments : & CallArguments < ' _ , ' db > ,
1777
1780
) -> Result < CallOutcome < ' db > , CallDunderError < ' db > > {
1778
1781
match self . to_meta_type ( db) . member ( db, name) {
1779
- Symbol :: Type ( callable_ty, Boundness :: Bound ) => Ok ( callable_ty. call ( db, arguments) ?) ,
1782
+ Symbol :: Type ( callable_ty, Boundness :: Bound ) => Ok ( callable_ty. try_call ( db, arguments) ?) ,
1780
1783
Symbol :: Type ( callable_ty, Boundness :: PossiblyUnbound ) => {
1781
- let call = callable_ty. call ( db, arguments) ?;
1784
+ let call = callable_ty. try_call ( db, arguments) ?;
1782
1785
Err ( CallDunderError :: PossiblyUnbound ( call) )
1783
1786
}
1784
1787
Symbol :: Unbound => Err ( CallDunderError :: MethodNotAvailable ) ,
@@ -1801,12 +1804,12 @@ impl<'db> Type<'db> {
1801
1804
}
1802
1805
1803
1806
let dunder_iter_result =
1804
- self . call_dunder ( db, "__iter__" , & CallArguments :: positional ( [ self ] ) ) ;
1807
+ self . try_call_dunder ( db, "__iter__" , & CallArguments :: positional ( [ self ] ) ) ;
1805
1808
match & dunder_iter_result {
1806
1809
Ok ( outcome) | Err ( CallDunderError :: PossiblyUnbound ( outcome) ) => {
1807
1810
let iterator_ty = outcome. return_type ( db) ;
1808
1811
1809
- return match iterator_ty. call_dunder (
1812
+ return match iterator_ty. try_call_dunder (
1810
1813
db,
1811
1814
"__next__" ,
1812
1815
& CallArguments :: positional ( [ iterator_ty] ) ,
@@ -1855,7 +1858,7 @@ impl<'db> Type<'db> {
1855
1858
//
1856
1859
// TODO(Alex) this is only valid if the `__getitem__` method is annotated as
1857
1860
// accepting `int` or `SupportsIndex`
1858
- match self . call_dunder (
1861
+ match self . try_call_dunder (
1859
1862
db,
1860
1863
"__getitem__" ,
1861
1864
& CallArguments :: positional ( [ self , KnownClass :: Int . to_instance ( db) ] ) ,
@@ -2693,52 +2696,8 @@ pub enum KnownInstanceType<'db> {
2693
2696
}
2694
2697
2695
2698
impl < ' db > KnownInstanceType < ' db > {
2696
- pub const fn as_str ( self ) -> & ' static str {
2697
- match self {
2698
- Self :: Annotated => "Annotated" ,
2699
- Self :: Literal => "Literal" ,
2700
- Self :: LiteralString => "LiteralString" ,
2701
- Self :: Optional => "Optional" ,
2702
- Self :: Union => "Union" ,
2703
- Self :: TypeVar ( _) => "TypeVar" ,
2704
- Self :: NoReturn => "NoReturn" ,
2705
- Self :: Never => "Never" ,
2706
- Self :: Any => "Any" ,
2707
- Self :: Tuple => "Tuple" ,
2708
- Self :: Type => "Type" ,
2709
- Self :: TypeAliasType ( _) => "TypeAliasType" ,
2710
- Self :: TypingSelf => "Self" ,
2711
- Self :: Final => "Final" ,
2712
- Self :: ClassVar => "ClassVar" ,
2713
- Self :: Callable => "Callable" ,
2714
- Self :: Concatenate => "Concatenate" ,
2715
- Self :: Unpack => "Unpack" ,
2716
- Self :: Required => "Required" ,
2717
- Self :: NotRequired => "NotRequired" ,
2718
- Self :: TypeAlias => "TypeAlias" ,
2719
- Self :: TypeGuard => "TypeGuard" ,
2720
- Self :: TypeIs => "TypeIs" ,
2721
- Self :: List => "List" ,
2722
- Self :: Dict => "Dict" ,
2723
- Self :: DefaultDict => "DefaultDict" ,
2724
- Self :: Set => "Set" ,
2725
- Self :: FrozenSet => "FrozenSet" ,
2726
- Self :: Counter => "Counter" ,
2727
- Self :: Deque => "Deque" ,
2728
- Self :: ChainMap => "ChainMap" ,
2729
- Self :: OrderedDict => "OrderedDict" ,
2730
- Self :: ReadOnly => "ReadOnly" ,
2731
- Self :: Unknown => "Unknown" ,
2732
- Self :: AlwaysTruthy => "AlwaysTruthy" ,
2733
- Self :: AlwaysFalsy => "AlwaysFalsy" ,
2734
- Self :: Not => "Not" ,
2735
- Self :: Intersection => "Intersection" ,
2736
- Self :: TypeOf => "TypeOf" ,
2737
- }
2738
- }
2739
-
2740
2699
/// Evaluate the known instance in boolean context
2741
- pub const fn bool ( self ) -> Truthiness {
2700
+ pub ( crate ) const fn bool ( self ) -> Truthiness {
2742
2701
match self {
2743
2702
Self :: Annotated
2744
2703
| Self :: Literal
@@ -2783,7 +2742,7 @@ impl<'db> KnownInstanceType<'db> {
2783
2742
}
2784
2743
2785
2744
/// Return the repr of the symbol at runtime
2786
- pub fn repr ( self , db : & ' db dyn Db ) -> & ' db str {
2745
+ pub ( crate ) fn repr ( self , db : & ' db dyn Db ) -> & ' db str {
2787
2746
match self {
2788
2747
Self :: Annotated => "typing.Annotated" ,
2789
2748
Self :: Literal => "typing.Literal" ,
@@ -2828,7 +2787,7 @@ impl<'db> KnownInstanceType<'db> {
2828
2787
}
2829
2788
2830
2789
/// Return the [`KnownClass`] which this symbol is an instance of
2831
- pub const fn class ( self ) -> KnownClass {
2790
+ pub ( crate ) const fn class ( self ) -> KnownClass {
2832
2791
match self {
2833
2792
Self :: Annotated => KnownClass :: SpecialForm ,
2834
2793
Self :: Literal => KnownClass :: SpecialForm ,
@@ -2877,16 +2836,20 @@ impl<'db> KnownInstanceType<'db> {
2877
2836
/// For example, the symbol `typing.Literal` is an instance of `typing._SpecialForm`,
2878
2837
/// so `KnownInstanceType::Literal.instance_fallback(db)`
2879
2838
/// returns `Type::Instance(InstanceType { class: <typing._SpecialForm> })`.
2880
- pub fn instance_fallback ( self , db : & dyn Db ) -> Type {
2839
+ pub ( crate ) fn instance_fallback ( self , db : & dyn Db ) -> Type {
2881
2840
self . class ( ) . to_instance ( db)
2882
2841
}
2883
2842
2884
2843
/// Return `true` if this symbol is an instance of `class`.
2885
- pub fn is_instance_of ( self , db : & ' db dyn Db , class : Class < ' db > ) -> bool {
2844
+ pub ( crate ) fn is_instance_of ( self , db : & ' db dyn Db , class : Class < ' db > ) -> bool {
2886
2845
self . class ( ) . is_subclass_of ( db, class)
2887
2846
}
2888
2847
2889
- pub fn try_from_file_and_name ( db : & ' db dyn Db , file : File , symbol_name : & str ) -> Option < Self > {
2848
+ pub ( crate ) fn try_from_file_and_name (
2849
+ db : & ' db dyn Db ,
2850
+ file : File ,
2851
+ symbol_name : & str ,
2852
+ ) -> Option < Self > {
2890
2853
let candidate = match symbol_name {
2891
2854
"Any" => Self :: Any ,
2892
2855
"ClassVar" => Self :: ClassVar ,
@@ -2937,7 +2900,7 @@ impl<'db> KnownInstanceType<'db> {
2937
2900
///
2938
2901
/// Most variants can only exist in one module, which is the same as `self.class().canonical_module()`.
2939
2902
/// Some variants could validly be defined in either `typing` or `typing_extensions`, however.
2940
- pub fn check_module ( self , module : KnownModule ) -> bool {
2903
+ pub ( crate ) fn check_module ( self , module : KnownModule ) -> bool {
2941
2904
match self {
2942
2905
Self :: Any
2943
2906
| Self :: ClassVar
@@ -3668,7 +3631,7 @@ impl<'db> Class<'db> {
3668
3631
// TODO: Other keyword arguments?
3669
3632
let arguments = CallArguments :: positional ( [ name, bases, namespace] ) ;
3670
3633
3671
- let return_ty_result = match metaclass. call ( db, & arguments) {
3634
+ let return_ty_result = match metaclass. try_call ( db, & arguments) {
3672
3635
Ok ( outcome) => Ok ( outcome. return_type ( db) ) ,
3673
3636
3674
3637
Err ( CallError :: NotCallable { not_callable_ty } ) => Err ( MetaclassError {
0 commit comments