-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-119933 : Improve SyntaxError
message for invalid type parameters expressions
#119976
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
Conversation
SyntaxError
for invalid type parameters expressionsSyntaxError
message for invalid type parameters expressions
After some sleep, I suggest renaming
def foo[T: int, *Ts = *tuple[int], **P = [int, str]](): pass I would say that class GenericMine[T: int]:
pass The symbol table's type for The renaming would affect the current
We could also emit a symbol table for type parameters without any bound, constraint or default and the renaming would make better sense. By the way, >>> import symtable
>>> s = symtable.symtable("class A[T]: pass", "?", "exec")
>>> s.get_children()[0].lookup('T')
<symbol 'T': LOCAL, DEF_LOCAL> where I would have expected T to be have the |
Thanks! I agree with renaming For My biggest issue with changing these names is backwards compatibility in the symtable module. We explicitly document a set of strings at https://docs.python.org/3/library/symtable.html#symtable.SymbolTable.get_type. We can add to the set of strings for 3.14 and probably for 3.13, but for 3.12 it should stay the same. |
Yes, that's what I had in mind (actually, I wrote Since we are anyway using full names, I could also suggest using
3.13 should be fine (I guess) because it's still a pre-release so... but would it make sense to instead return enumeration constants that are publicly accessible instead of strings like that..? at least we wouldn't have the issue in the future if the grammar is extended/changed and we could probably make the attribute deprecated more easily ? |
Yes, I think using an enum would make sense and it could be made backwards-compatible by using StrEnum. |
- improve comments - rename 'TypeVarBoundBlock' -> 'TypeVariableBlock' - rename 'TypeParamBlock' -> 'TypeParametersBlock' - rename 'ste_description' -> 'ste_context_info' - only set 'ste_context_info' just before visiting the actual expression
- rename '_symtable.TYPE_TYPE_VAR_BOUND' to '_symtable.TYPE_TYPE_VARIABLE' - rename '_symtable.TYPE_TYPE_PARAM' to '_symtable.TYPE_TYPE_PARAMETERS' - add string enumeration for symbol table type
@JelleZijlstra I've updated the proposal. By the way, I've only done that for what I've added but I'll likely re-order the case so that they match the enumeration. I put |
Misc/NEWS.d/next/Library/2024-06-03-13-48-44.gh-issue-119933.Kc0HG5.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/Library/2024-06-03-13-48-44.gh-issue-119933.Kc0HG5.rst
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General direction looks fine to me. I won't have time soon to do a detailed review but if @JelleZijlstra is happy with it, that's good enough for me.
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Thanks @picnixz for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Sorry, @picnixz and @JelleZijlstra, I could not cleanly backport this to
|
I am doing the backport |
…ype parameters expressions (pythonGH-119976) (cherry picked from commit 4bf17c3) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
GH-120641 is a backport of this pull request to the 3.13 branch. |
…ameters expressions (python#119976) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…ameters expressions (python#119976) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
…ameters expressions (python#119976) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
cc @JelleZijlstra
I added a bunch of tests but feel free to tell me if there are more cases to check. It took me a bit of time to observe that entering the context was handled inside
symtable_visit_type_param_bound_or_default
and not upon calling it.Tell me if I need a What's New entry by the way or if the NEWS entry should be improved.