11from __future__ import annotations
2- # mypy: disable-error-code="empty-body"
32
4- from functools import cache
53from collections .abc import Callable
6- from typing import (
7- reveal_type ,
8- final ,
9- assert_type ,
10- TYPE_CHECKING ,
11- overload ,
12- override ,
13- Any ,
14- Self ,
15- TypeVar ,
16- ParamSpec ,
17- Generic ,
18- )
194from dataclasses import dataclass
205
6+ # mypy: disable-error-code="empty-body"
7+ from functools import cache
8+ from typing import TYPE_CHECKING , Any , Generic , ParamSpec , Self , TypeVar , assert_type , final , overload , override , reveal_type
9+
2110P = ParamSpec ("P" )
2211R = TypeVar ("R" )
2312
13+
2414@cache
25- def cached_fn (arg : int , arg2 : str ) -> int :
26- ...
15+ def cached_fn (arg : int , arg2 : str ) -> int : ...
16+
2717
2818@dataclass
2919class MemberVarCached (Generic [P , R ]):
3020 member_callable : Callable [P , R ]
3121
22+
3223vc = MemberVarCached (cached_fn )
3324vc .member_callable (1 , "" )
3425assert_type (vc .member_callable (1 , "" ), int )
3526
3627if TYPE_CHECKING :
3728 # type errors - correct
38- vc .member_callable (1 , 1 ) # pyright: ignore[reportArgumentType]
39- vc .member_callable (1 ) # pyright: ignore[reportCallIssue]
40- vc .member_callable ("1" ) # pyright: ignore[reportCallIssue]
29+ vc .member_callable (1 , 1 ) # pyright: ignore[reportArgumentType]
30+ vc .member_callable (1 ) # pyright: ignore[reportCallIssue]
31+ vc .member_callable ("1" ) # pyright: ignore[reportCallIssue]
32+
4133
4234class CFnCls :
4335 @cache
@@ -74,7 +66,6 @@ def cls_fn_explicit_positional_only(_cls, arg: int, /) -> int:
7466 print ("class fn called" )
7567 return arg
7668
77-
7869 @staticmethod
7970 @cache
8071 def st_fn (arg : int ) -> int :
@@ -99,9 +90,11 @@ def prp_fn(self) -> int:
9990 print ("property fn called" )
10091 return 1
10192
93+
10294class CFnSubCls (CFnCls ):
10395 pass
10496
97+
10598cfn_inst = CFnCls ()
10699cfn_inst .fn (1 )
107100cfn_inst .fn .__wrapped__ (cfn_inst , 1 )
@@ -139,33 +132,37 @@ class CFnSubCls(CFnCls):
139132CFnCls .cls_fn .cache_clear ()
140133if TYPE_CHECKING :
141134 # type errors - correct
142- CFnCls ().fn (1 , 1 ) # type: ignore[call-arg,misc] # pyright: ignore[reportCallIssue]
143- CFnCls ().fn .__wrapped__ (CFnCls (), 1 , 1 ) # type: ignore[call-arg] # pyright: ignore[reportCallIssue]
144- CFnCls .fn (arg = 1 ) # type: ignore[call-arg] # pyright: ignore[reportCallIssue]
145- CFnCls .st_fn (1 , 1 ) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
146- CFnCls .cls_fn (1 , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
147- CFnCls .cls_fn_positional_only (CFnCls , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
148- CFnCls ().cls_fn_positional_only (CFnCls , 1 ) # type: ignore[call-arg,arg-type,misc] # pyright: ignore[reportCallIssue]
149- CFnCls .cls_fn_explicit_positional_only (CFnCls , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
150- CFnCls ().cls_fn_explicit_positional_only (CFnCls , 1 ) # type: ignore[call-arg,arg-type,misc] # pyright: ignore[reportCallIssue]
135+ CFnCls ().fn (1 , 1 ) # type: ignore[call-arg,misc] # pyright: ignore[reportCallIssue]
136+ CFnCls ().fn .__wrapped__ (CFnCls (), 1 , 1 ) # type: ignore[call-arg] # pyright: ignore[reportCallIssue]
137+ CFnCls .fn (arg = 1 ) # type: ignore[call-arg] # pyright: ignore[reportCallIssue]
138+ CFnCls .st_fn (1 , 1 ) # type: ignore[call-overload] # pyright: ignore[reportCallIssue]
139+ CFnCls .cls_fn (1 , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
140+ CFnCls .cls_fn_positional_only (CFnCls , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
141+ CFnCls ().cls_fn_positional_only (CFnCls , 1 ) # type: ignore[call-arg,arg-type,misc] # pyright: ignore[reportCallIssue]
142+ CFnCls .cls_fn_explicit_positional_only (CFnCls , 1 ) # type: ignore[arg-type] # pyright: ignore[reportCallIssue]
143+ CFnCls ().cls_fn_explicit_positional_only (CFnCls , 1 ) # type: ignore[call-arg,arg-type,misc] # pyright: ignore[reportCallIssue]
151144
152145from types import MethodType
153146
147+
154148@cache
155149def fn (arg : int ) -> int :
156150 return arg
157151
152+
158153@cache
159154def df_fn (arg : int , darg : str = "default" ):
160155 print ("default fn called" )
161156 return darg
157+
158+
162159df_fn (1 )
163160
164161fn (1 )
165162assert_type (fn (1 ), int )
166163if TYPE_CHECKING :
167164 # type error - correct
168- fn (1 , 2 ) # pyright: ignore[reportCallIssue]
165+ fn (1 , 2 ) # pyright: ignore[reportCallIssue]
169166fn .cache_clear ()
170167
171168# @overload
@@ -196,50 +193,53 @@ class Unhashable:
196193 def __eq__ (self , value : object ) -> bool :
197194 return False
198195
196+
199197@cache
200198def no_cache (arg : Unhashable , arg2 : int ) -> None :
201199 pass
202200
201+
203202if TYPE_CHECKING :
204203 # This is not correctly rejected.
205- no_cache (Unhashable (), 2 ) # pyright: ignore[reportArgumentType]
204+ no_cache (Unhashable (), 2 ) # pyright: ignore[reportArgumentType]
205+
206206
207207class MemberVarBound (Generic [P , R ]):
208208 @cache
209- def equals (self , other : Self ) -> bool :
210- ...
209+ def equals (self , other : Self ) -> bool : ...
210+
211211 member_fn : Callable [P , R ]
212212
213+
213214def set_member (lhs : MemberVarBound [..., Any ], rhs : MemberVarBound [..., Any ]) -> None :
214215 lhs .member_fn = rhs .equals
215216 lhs .member_fn (rhs )
216217
217218 if TYPE_CHECKING :
218- lhs .member_fn () # pyright: ignore[reportCallIssue]
219+ lhs .member_fn () # pyright: ignore[reportCallIssue]
219220
220221
221222from abc import ABCMeta , abstractmethod
222223
224+
223225class CustomABC (metaclass = ABCMeta ):
224226
225227 @abstractmethod
226- def foo (self , arg : int ) -> int :
227- ...
228+ def foo (self , arg : int ) -> int : ...
229+
228230
229231@final
230232class ABCConcrete (CustomABC ):
231233 @override
232- def foo (self , arg : int ) -> int :
233- ...
234+ def foo (self , arg : int ) -> int : ...
234235
235236 @cache
236- def abc_fn (self , arg : str ) -> str :
237- ...
237+ def abc_fn (self , arg : str ) -> str : ...
238238
239239 @classmethod
240240 @cache
241- def abc_cm (cls , arg : str ) -> str :
242- ...
241+ def abc_cm (cls , arg : str ) -> str : ...
242+
243243
244244ABCConcrete ().abc_fn ("1" )
245245ABCConcrete .abc_fn (ABCConcrete (), "1" )
0 commit comments