1313from typing import (
1414 TYPE_CHECKING ,
1515 Any ,
16- Generic ,
1716 Literal ,
18- ParamSpec ,
19- TypeVar ,
2017 cast ,
2118 get_args ,
2219 get_origin ,
@@ -88,13 +85,8 @@ def get_logger() -> logging.Logger:
8885}
8986
9087
91- R = TypeVar ('R' )
92- P = ParamSpec ('P' )
93- CC = Callable [P , R ] # need to give it a name, if inlined into bound=, mypy runs in a bug
94- PathProvider = PathIsh | Callable [P , PathIsh ]
95- HashFunction = Callable [P , SourceHash ]
96-
97- F = TypeVar ('F' , bound = CC )
88+ type PathProvider [** P ] = PathIsh | Callable [P , PathIsh ]
89+ type HashFunction [** P ] = Callable [P , SourceHash ]
9890
9991
10092def default_hash (* args , ** kwargs ) -> SourceHash :
@@ -109,9 +101,9 @@ def mtime_hash(path: Path, *args, **kwargs) -> SourceHash:
109101 return default_hash (f'{ path } .{ mt } ' , * args , ** kwargs )
110102
111103
112- Failure = str
113- Kind = Literal ['single' , 'multiple' ]
114- Inferred = tuple [Kind , type [Any ]]
104+ Failure = str # deliberately not a type =, used in type checks
105+ type Kind = Literal ['single' , 'multiple' ]
106+ type Inferred = tuple [Kind , type [Any ]]
115107
116108
117109def infer_return_type (func ) -> Failure | Inferred :
@@ -275,13 +267,13 @@ def cachew_error(e: Exception, *, logger: logging.Logger) -> None:
275267
276268# using cachew_impl here just to use different signatures during type checking (see below)
277269@doublewrap
278- def cachew_impl (
270+ def cachew_impl [ ** P ] (
279271 func = None , # TODO should probably type it after switch to python 3.10/proper paramspec
280- cache_path : PathProvider [P ] | None = use_default_path ,
272+ cache_path : PathProvider [P ] | None = use_default_path , # ty: ignore[too-many-positional-arguments] # see https://github.com/astral-sh/ty/issues/157
281273 * ,
282274 force_file : bool = False ,
283275 cls : type | tuple [Kind , type ] | None = None ,
284- depends_on : HashFunction [P ] = default_hash ,
276+ depends_on : HashFunction [P ] = default_hash , # ty: ignore[too-many-positional-arguments]
285277 logger : logging .Logger | None = None ,
286278 chunk_by : int = 100 ,
287279 # NOTE: allowed values for chunk_by depend on the system.
@@ -402,7 +394,9 @@ def process(self, msg, kwargs):
402394 else :
403395 assert use_kind is not None
404396 if (use_kind , use_cls ) != inference_res :
405- logger .warning (f"inferred type { inference_res } mismatches explicitly specified type { (use_kind , use_cls )} " )
397+ logger .warning (
398+ f"inferred type { inference_res } mismatches explicitly specified type { (use_kind , use_cls )} "
399+ )
406400 # TODO not sure if should be more serious error...
407401
408402 if use_kind == 'single' :
@@ -447,18 +441,18 @@ def binder(*args, **kwargs):
447441 # we need two versions due to @doublewrap
448442 # this is when we just annotate as @cachew without any args
449443 @overload
450- def cachew (fun : F ) -> F : ...
444+ def cachew [ F : Callable ] (fun : F ) -> F : ...
451445
452446 # NOTE: we won't really be able to make sure the args of cache_path are the same as args of the wrapped function
453447 # because when cachew() is called, we don't know anything about the wrapped function yet
454448 # but at least it works for checking that cachew_path and depdns_on have the same args :shrug:
455449 @overload
456- def cachew (
457- cache_path : PathProvider [P ] | None = ...,
450+ def cachew [ F , ** P ] (
451+ cache_path : PathProvider [P ] | None = ..., # ty: ignore[too-many-positional-arguments]
458452 * ,
459453 force_file : bool = ...,
460454 cls : type | tuple [Kind , type ] | None = ...,
461- depends_on : HashFunction [P ] = ...,
455+ depends_on : HashFunction [P ] = ..., # ty: ignore[too-many-positional-arguments]
462456 logger : logging .Logger | None = ...,
463457 chunk_by : int = ...,
464458 synthetic_key : str | None = ...,
@@ -546,7 +540,9 @@ def _module_is_disabled(module_name: str, logger: logging.Logger) -> bool:
546540 disabled_modules = _parse_disabled_modules (logger )
547541 for pat in disabled_modules :
548542 if _matches_disabled_module (module_name , pat ):
549- logger .debug (f"caching disabled for { module_name } (matched '{ pat } ' from 'CACHEW_DISABLE={ os .environ ['CACHEW_DISABLE' ]} )'" )
543+ logger .debug (
544+ f"caching disabled for { module_name } (matched '{ pat } ' from 'CACHEW_DISABLE={ os .environ ['CACHEW_DISABLE' ]} )'"
545+ )
550546 return True
551547 return False
552548
@@ -560,13 +556,13 @@ def _module_is_disabled(module_name: str, logger: logging.Logger) -> bool:
560556
561557
562558@dataclass
563- class Context ( Generic [ P ]) :
559+ class Context [ ** P ] :
564560 # fmt: off
565561 func : Callable
566- cache_path : PathProvider [P ]
562+ cache_path : PathProvider [P ] # ty: ignore[too-many-positional-arguments]
567563 force_file : bool
568564 cls_ : type
569- depends_on : HashFunction [P ]
565+ depends_on : HashFunction [P ] # ty: ignore[too-many-positional-arguments]
570566 logger : logging .Logger
571567 chunk_by : int
572568 synthetic_key : str | None
@@ -605,9 +601,9 @@ def composite_hash(self, *args, **kwargs) -> dict[str, Any]:
605601 # fmt: on
606602
607603
608- def cachew_wrapper (
604+ def cachew_wrapper [ ** P ] (
609605 * args ,
610- _cachew_context : Context [P ],
606+ _cachew_context : Context [P ], # ty: ignore[too-many-positional-arguments]
611607 ** kwargs ,
612608):
613609 C = _cachew_context
0 commit comments