8000 gh-119933 : Improve ``SyntaxError`` message for invalid type parameters expressions by picnixz · Pull Request #119976 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 27 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6279686
fix ``SyntaxError`` for invalid type parameters expressions
picnixz Jun 3, 2024
7075bc6
blurb
picnixz Jun 3, 2024
74d330e
improve doc & naming
picnixz Jun 3, 2024
1ff9183
visual improvements of some test
picnixz Jun 3, 2024
234200c
Merge branch 'main' into fix-119933
picnixz Jun 3, 2024
fa7f02f
simplify the flow
picnixz Jun 3, 2024
b77cebb
Merge branch 'main' into fix-119933
picnixz Jun 4, 2024
7482db6
address review
picnixz Jun 4, 2024
2edf665
address review
picnixz Jun 4, 2024
845c7a6
use the same declaration order across files
picnixz Jun 4, 2024
bbfdcb7
Use enumeration members instead of `.value`.
picnixz Jun 5, 2024
f1208c7
export `SymbolTableType` enumeration
picnixz Jun 5, 2024
e533e4d
update NEWS
picnixz Jun 5, 2024
4cf0bf8
simplify `ste_scope_info` creation
picnixz Jun 5, 2024
740c8f2
improve documentation for `TypeVariableBlock`
picnixz Jun 5, 2024
3e329df
update documentation
picnixz Jun 5, 2024
18bc1f3
fixup
picnixz Jun 5, 2024
dd8d461
Merge branch 'main' into fix-119933
picnixz Jun 5, 2024
2ca6fd9
blurb
picnixz Jun 5, 2024
278d1fc
fixup
picnixz Jun 5, 2024
3f6a03e
Merge branch 'main' into fix-119933
picnixz Jun 12, 2024
3667349
Merge branch 'main' into fix-119933
picnixz Jun 12, 2024
82a37b6
Move SyntaxError's NEWS into "Core and Builtins"
picnixz Jun 13, 2024
cf45f40
improve documentation wording
picnixz Jun 13, 2024
e96f6b4
Update Python/symtable.c
picnixz Jun 17, 2024
705a76f
Update Doc/library/symtable.rst
picnixz Jun 17, 2024
abad81b
address review
picnixz Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address review
- 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
  • Loading branch information
picnixz committed Jun 4, 2024
commit 2edf665e3a6b80a5f6c94da6765640608183c6d5
33 changes: 22 additions & 11 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)

import weakref
from enum import StrEnum

__all__ = ["symtable", "SymbolTable", "Class", "Function", "Symbol"]

Expand Down Expand Up @@ -43,6 +44,16 @@ def __call__(self, table, filename):
_newSymbolTable = SymbolTableFactory()


class SymbolTableType(StrEnum):
MODULE = "module"
FUNCTION = "function"
CLASS = "class"
ANNOTATION = "annotation"
TYPE_VARIABLE = "type variable"
TYPE_ALIAS = "type alias"
TYPE_PARAMETERS = "type parameters"


class SymbolTable:

def __init__(self, raw_table, filename):
Expand All @@ -66,23 +77,23 @@ def __repr__(self):
def get_type(self):
"""Return the type of the symbol table.

The values returned are 'class', 'module', 'function',
'annotation', 'TypeVar bound', 'type alias', and 'type parameter'.
The values returned are one of the values in
the ``SymbolTableType`` enumeration.
"""
if self._table.type == _symtable.TYPE_MODULE:
return "module"
return SymbolTableType.MODULE.value
if self._table.type == _symtable.TYPE_FUNCTION:
return "function"
return SymbolTableType.FUNCTION.value
if self._table.type == _symtable.TYPE_CLASS:
return "class"
return SymbolTableType.CLASS.value
if self._table.type == _symtable.TYPE_ANNOTATION:
return "annotation"
if self._table.type == _symtable.TYPE_TYPE_VAR_BOUND:
return "TypeVar bound"
return SymbolTableType.ANNOTATION.value
if self._table.type == _symtable.TYPE_TYPE_VARIABLE:
return SymbolTableType.TYPE_VARIABLE.value
if self._table.type == _symtable.TYPE_TYPE_ALIAS:
return "type alias"
if self._table.type == _symtable.TYPE_TYPE_PARAM:
return "type parameter"
return SymbolTableType.TYPE_ALIAS.value
if self._table.type == _symtable.TYPE_TYPE_PARAMETERS:
return SymbolTableType.TYPE_PARAMETERS.value
assert False, f"unexpected type: {self._table.type}"

def get_id(self):
Expand Down
12 changes: 7 additions & 5 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def namespace_test(): pass
def generic_spam[T](a):
pass

class GenericMine[T: int]:
class GenericMine[T: int, U: (int, str) = int]:
pass
"""

Expand Down Expand Up @@ -78,6 +78,7 @@ class SymtableTest(unittest.TestCase):
GenericMine = find_block(top, "GenericMine")
GenericMine_inner = find_block(GenericMine, "GenericMine")
T = find_block(GenericMine, "T")
U = find_block(GenericMine, "U")

def test_type(self):
self.assertEqual(self.top.get_type(), "module")
Expand All @@ -87,13 +88,14 @@ def test_type(self):
self.assertEqual(self.internal.get_type(), "function")
self.assertEqual(self.foo.get_type(), "function")
self.assertEqual(self.Alias.get_type(), "type alias")
self.assertEqual(self.GenericAlias.get_type(), "type parameter")
self.assertEqual(self.GenericAlias.get_type(), "type parameters")
self.assertEqual(self.GenericAlias_inner.get_type(), "type alias")
self.assertEqual(self.generic_spam.get_type(), "type parameter")
self.assertEqual(self.generic_spam.get_type(), "type parameters")
self.assertEqual(self.generic_spam_inner.get_type(), "function")
self.assertEqual(self.GenericMine.get_type(), "type parameter")
self.assertEqual(self.GenericMine.get_type(), "type parameters")
self.assertEqual(self.GenericMine_inner.get_type(), "class")
self.assertEqual(self.T.get_type(), "TypeVar bound")
self.assertEqual(self.T.get_type(), "type variable")
self.assertEqual(self.U.get_type(), "type variable")

def test_id(self):
self.assertGreater(self.top.get_id(), 0)
Expand Down
4 changes: 2 additions & 2 deletions Modules/symtablemodule.c
Original file line number Diff line number Diff line change
Expand U 629C p @@ -90,11 +90,11 @@ symtable_init_constants(PyObject *m)
return -1;
if (PyModule_AddIntConstant(m, "TYPE_ANNOTATION", AnnotationBlock) < 0)
return -1;
if (PyModule_AddIntConstant(m, "TYPE_TYPE_VAR_BOUND", TypeVariableBlock) < 0)
if (PyModule_AddIntConstant(m, "TYPE_TYPE_VARIABLE", TypeVariableBlock) < 0)
return -1;
if (PyModule_AddIntConstant(m, "TYPE_TYPE_ALIAS", TypeAliasBlock) < 0)
return -1;
if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAM", TypeParametersBlock) < 0)
if (PyModule_AddIntConstant(m, "TYPE_TYPE_PARAMETERS", TypeParametersBlock) < 0)
return -1;

if (PyModule_AddIntMacro(m, LOCAL) < 0) return -1;
Expand Down
0