- Notifications
You must be signed in to change notification settings - Fork263
-
So, for this code: classCollection:defmake(self,type_:Type[T],**attrs)->T: ... How can I annotate |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 2 comments 15 replies
-
You can use a fromtypingimportCallableclassCollection:defmake[**P,T](self,type_:Callable[P,T],*args:P.args,**kwargs:P.kwargs )->T: ... Here's the same thing using older (pre-3.12) syntax: fromtypingimportCallable,ParamSpec,TypeVarP=ParamSpec("P")T=TypeVar("T")classCollection:defmake(self,type_:Callable[P,T],*args:P.args,**kwargs:P.kwargs)->T: ... |
BetaWas this translation helpful?Give feedback.
All reactions
-
Sorry, not following this, could you provide a code example of what you mean?
This sounds pretty sub-optimal :-/ What's pytype? That's not a phrase I've heard before... |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
The spec for defallowed[**P](self,*args:P.args,**kwargs:P.kwargs): ...defpositional_only_prepend_allowed[**P](self,arg:int,/,*args:P.args,**kwargs:P.kwargs): ...defno_kwargs_disallowed[**P](self,*args:P.args): ...defno_args_disallowed[**P](self,**kwargs:P.kwargs): ...defmiddle_extra_kwarg_disallowed[**P](self,*args:P.args,my_new_kwarg:int=0,**kwargs:P.kwargs): ... With upper bounds on
It's a Python type checker made by Google. It performs very deep type inference and allows a lot of things other type checkers would disallow because their inference is too shallow to ensure type safety without an explicit type annotation. So it can be useful in code that doesn't make heavy use of type annotations, since it will still be able to detect a lot of problems anyways. |
BetaWas this translation helpful?Give feedback.
All reactions
-
The irony's not lost on me that some of the things that have made Python so powerful over the last two or three decades end up being the things that are hardest to express in static typing... Thanks for the pytype pointer; is this one, right?https://github.com/google/pytype |
BetaWas this translation helpful?Give feedback.
All reactions
-
Correct |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Hmm, unfortunately, pytype seems to trip up onmuch simpler stuff that mypy is happy with :-/ |
BetaWas this translation helpful?Give feedback.
All reactions
-
#1500 seems to be the same ballpark as this. |
BetaWas this translation helpful?Give feedback.
All reactions
-
That sounds more like a proxy type to me, which has been requested many times before, e.g.#802 but we don't have anything like that yet. There definitely are some open questions for proxy types, like should a proxy be allowed to stand-in for the real type, what about partial proxies that don't proxy everything etc. It would definitely need a PEP to explore and answer some of those questions, so we have a concrete proposal that can be implemented in a type checker as a proof of concept. |
BetaWas this translation helpful?Give feedback.