-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using typeddict for more precise typing of kwargs #10576
Allow using typeddict for more precise typing of kwargs #10576
Conversation
@JukkaL @ilevkivskyi @hauntsaninja @ethanhs @JelleZijlstra what do you think about this? |
@JukkaL @ilevkivskyi @hauntsaninja @ethanhs @JelleZijlstra any thoughts on this one? Is this something you would be interested in merging? I am wondering if this approach makes sense given that there are some plans on improving typing for tensors in the future (not sure if those would be related with this issue)? |
This is a useful feature, so thank you for the PR and driving this forward. However, (and speaking only for myself here), adding new constructs to the typing system is probably best done by starting a thread on the typing-sig mailing list, so that other type checkers and users of static typing can weigh in. That (plus writing a PEP) would also be the path to getting this into |
Looks like the author of this PR has a draft PEP for this here: https://github.com/franekmagiera/peps/blob/more-precise-kwargs-typing/pep-9999.rst Very exciting, wish you luck @franekmagiera ! |
@rattrayalex thanks, still need to describe the runtime behavior in more detail, recently it's been hard to find time but I hope to have the draft ready soon |
@franekmagiera Are you still working on this? Many things seem to have changed: the PEP specifies |
@ilevkivskyi Not directly on this PR but I'm working on a PEP draft:
I think this PR could be closed, I agree that this approach will not work fully. |
OK, I see. Please feel free to propose another PR according to latest version of the PEP. Alternatively I can try implementing this myself. |
Description
Closes #4441
Added
Expand
special form tomypy_extensions
python/mypy_extensions#22Changes happen during semantic analysis. Firstly, semantic analyzer determines if
kwargs
are typed usingExpand
. If they are, type analyzer'svisit_callable_type
expands the keyword arguments using theTypedDict
provided inExpand
.Semantic analyzer adds the
kwargs
variable to local symbol table that has explicitTypedDict
type provided inExpand
. Then function definition is modified.Test Plan
Added a new file
test-data/unit/check-expand.test
that tests the changes.I think overall it's a reasonable solution, but I have some doubts about the
Expand
special form inmypy_extensions
, I use_SpecialForm
fromtyping
module and this may not work on all python versions. I will appreciate any hints on how to resolve this.Another thing - should a note appear when
**kwargs
are typed using aTypedDict
withtotal=False
to encourage the use ofkwargs.get(...)
instead ofkwargs[...]
?