- Notifications
You must be signed in to change notification settings - Fork263
-
See here:python/mypy#6131 |
BetaWas this translation helpful?Give feedback.
All reactions
👍 4
Replies: 4 comments 20 replies
-
When you say "default types", am I correct in assuming that you want a way to represent that "the TypedDict is allowed to contain additional keys beyond what is specified, but they must be of this type"? There was a recent discussion about this in the typing sig related to this topic: |
BetaWas this translation helpful?Give feedback.
All reactions
-
+1 for the language server. That’s a good reason.
Yes, I understand and thank you for your help. I’m just trying to understand things. Can you please explain what PEP 692 is for then? In what cases should this be used/is it necessary? |
BetaWas this translation helpful?Give feedback.
All reactions
-
@erictraut Thank you again for your help (and Happy Father's Day as well). From our conversation, I’m inclined to believe you are against PEP 692, and I understand why. Ironically, part of its motivation states it is useful for:
even though you showed the additional type hinting is a rather less than useful **kwargs: **KwargsTypedDict Ultimately, I see now that PEP 692 is a convenience rather than a necessity. It should be up to the purview of the coder to choose whether or not to use it. To that end, I will recommend an additional note be added to the PEP that recommends against its usage wherever possible as it tends to abstract away useful type hinting information rather than add to it. In all cases, it would seem better to simply type out the information rather than hide it behind a TypedDict. |
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.
-
On the contrary, I'm supportive of PEP 692. In fact, I implemented support for the mechanism in pyright when Franek was working on the initial draft of the PEP. The facility described in PEP 692 has utility, but it is applicable only for certain use cases. It is not a replacement for the existing, well-established mechanisms in the type system for describing function signatures with keyword parameters. PEP 692 is useful in cases where an API accommodates optional keyword-only parameters when no default values are provided for absent keywords. The draft PEP shows some examples. Here's anotherdiscussion in the mypy issue tracker that provides some examples where it might be useful. Note that most functions written in Python, even if they use @adam-grant-hendry, the fact that the draft PEP 692 created so much confusion for you leaves me a bit worried. For reference, are you relatively new to static typing in Python, or have you been using typed Python for a long time? If it confuses other Python developers to the same degree, it has the potential of being a net negative to the type system. @franekmagiera, perhaps it's worth adding some language in the PEP (and maybe an example) that clarifies where the new mechanism shouldnot be used? |
BetaWas this translation helpful?Give feedback.
All reactions
👍 3
-
Not terribly new, no.I have over 15 years experience with Python and C++. The concepts of generics and variadics are not foreign to me and I know type hinting was first proposed by Guido back in 2015, so it has been around a while. However, for the sake of argument, I will consider myself relatively new for now. From a beginner's standpoint, I think your fear that PEP 692 might be abused is warranted on at least three points (hopefully my notes might help you and others with some of the wording in the documentation):
@staticmethoddefkeyClick(*args,**kwargs):# Implementation goes here... that one could type hint it with the actual intended arguments @staticmethoddefkeyClick(widget:QWidget,key:Qt.Key|str,modifier:Qt.KeyboardModifiers= ...,delay:int= ...)->None: ... without using I feel I read PEP 484 very well, but I couldn't gather that that was possible. Furthermore, I still get from
even if my stub is type hinted to the latter, so it would still not pass
@staticmethoddefkeyClick(*args,**kwargs):# Implementation goes here... would have to change to @staticmethoddefkeyClick(widget:QWidget,key:Qt.Key|str,modifier:Qt.KeyboardModifiers= ...,delay:int= ...)->None:# Implementation goes here... which could be a problem for 3rd party libraries if the original maintainers do not want to refactor signatures, but want contributors to develop type hints directly within the code.
I am fully in support of this! In addition, it would also be useful if |
BetaWas this translation helpful?Give feedback.
All reactions
-
That's an interesting discussion. I agree that in the example you've been discussing using an "explicit" signature is the best tool for the job. As you've noticed in point 2 that is not always possible without refactoring. I think PEP 692 would still bring some value - realistically not everyone will want to refactor, in that case I'd much rather see That said, it is definitely worth including the conclusions from this thread in the draft! |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@erictraut This is correct w.r.t.@rra 's original request inpython/mypy/6131. @fellnerse and I seem to be discussing defaultvalues. |
BetaWas this translation helpful?Give feedback.
All reactions
-
One can make a dict with an additionalProperty type today in vscode per:https://stackoverflow.com/questions/73558897/in-python-how-can-i-make-a-dict-that-has-type-hints-for-know-keys-and-a-default/73559006?noredirect=1#comment129898346_73559006 |
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.
-
Currently it seems neither TypedDict+Unpack nor ParamSpec nor Protocol support (proper) forwarding of function parameter annotations. They almost do it but then fall short in differents ways. One then needs to resort to using explicit parameter classes. |
BetaWas this translation helpful?Give feedback.