coconut-develop 3.2.0.post0.dev14__py2.py3-none-any.whl → 3.2.0.post0.dev16__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- coconut/_pyparsing.py +2 -0
- coconut/api.py +3 -3
- coconut/api.pyi +7 -7
- coconut/compiler/compiler.py +141 -76
- coconut/compiler/grammar.py +7 -2
- coconut/constants.py +2 -0
- coconut/root.py +1 -1
- coconut/tests/src/cocotest/agnostic/primary_1.coco +5 -5
- coconut/tests/src/extras.coco +175 -145
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/METADATA +2 -2
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/RECORD +16 -16
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/entry_points.txt +4 -4
- {coconut_develop-3.2.0.post0.dev14.data → coconut_develop-3.2.0.post0.dev16.data}/data/share/jupyter/kernels/coconut/kernel.json +0 -0
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/WHEEL +0 -0
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/licenses/LICENSE.txt +0 -0
- {coconut_develop-3.2.0.post0.dev14.dist-info → coconut_develop-3.2.0.post0.dev16.dist-info}/top_level.txt +0 -0
coconut/_pyparsing.py
CHANGED
|
@@ -45,6 +45,7 @@ from coconut.constants import (
|
|
|
45
45
|
default_incremental_cache_size,
|
|
46
46
|
never_clear_incremental_cache,
|
|
47
47
|
warn_on_multiline_regex,
|
|
48
|
+
warn_on_undefined_ParseResults_name,
|
|
48
49
|
num_displayed_timing_items,
|
|
49
50
|
use_pyparsing_cache_file,
|
|
50
51
|
use_line_by_line_parser,
|
|
@@ -274,6 +275,7 @@ if DEVELOP:
|
|
|
274
275
|
_pyparsing._enable_all_warnings()
|
|
275
276
|
_pyparsing.__diag__.warn_name_set_on_empty_Forward = False
|
|
276
277
|
_pyparsing.__diag__.warn_on_incremental_multiline_regex = warn_on_multiline_regex
|
|
278
|
+
_pyparsing.__diag__.warn_on_undefined_ParseResults_name = warn_on_undefined_ParseResults_name
|
|
277
279
|
|
|
278
280
|
if MODERN_PYPARSING and use_left_recursion_if_available:
|
|
279
281
|
ParserElement.enable_left_recursion()
|
coconut/api.py
CHANGED
|
@@ -53,10 +53,10 @@ from coconut.constants import (
|
|
|
53
53
|
GLOBAL_STATE = None
|
|
54
54
|
|
|
55
55
|
|
|
56
|
-
def get_state(state=
|
|
57
|
-
"""Get a Coconut state object;
|
|
56
|
+
def get_state(state=True):
|
|
57
|
+
"""Get a Coconut state object; True gets a new state, False gets the global state."""
|
|
58
58
|
global GLOBAL_STATE
|
|
59
|
-
if state is None:
|
|
59
|
+
if state is None or state is True:
|
|
60
60
|
return Command()
|
|
61
61
|
elif state is False:
|
|
62
62
|
if GLOBAL_STATE is None:
|
coconut/api.pyi
CHANGED
|
@@ -38,7 +38,7 @@ class CoconutException(Exception):
|
|
|
38
38
|
GLOBAL_STATE: Optional[Command] = None
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
def get_state(state: Optional[Command] = None) -> Command:
|
|
41
|
+
def get_state(state: Optional[Command] | bool = None) -> Command:
|
|
42
42
|
"""Get a Coconut state object; None gets a new state, False gets the global state."""
|
|
43
43
|
...
|
|
44
44
|
|
|
@@ -46,7 +46,7 @@ def get_state(state: Optional[Command] = None) -> Command:
|
|
|
46
46
|
def cmd(
|
|
47
47
|
args: Text | bytes | Iterable,
|
|
48
48
|
*,
|
|
49
|
-
state: Command |
|
|
49
|
+
state: Optional[Command] | bool = ...,
|
|
50
50
|
argv: Iterable[Text] | None = None,
|
|
51
51
|
interact: bool = False,
|
|
52
52
|
default_target: Text | None = None,
|
|
@@ -81,7 +81,7 @@ def setup(
|
|
|
81
81
|
no_tco: bool = False,
|
|
82
82
|
no_wrap: bool = False,
|
|
83
83
|
pure: bool = False,
|
|
84
|
-
state: Optional[Command] = ...,
|
|
84
|
+
state: Optional[Command] | bool = ...,
|
|
85
85
|
) -> None:
|
|
86
86
|
"""Set up the given state object."""
|
|
87
87
|
...
|
|
@@ -91,7 +91,7 @@ def warm_up(
|
|
|
91
91
|
streamline: bool = False,
|
|
92
92
|
enable_incremental_mode: bool = False,
|
|
93
93
|
*,
|
|
94
|
-
state: Optional[Command] = ...,
|
|
94
|
+
state: Optional[Command] | bool = ...,
|
|
95
95
|
) -> None:
|
|
96
96
|
"""Warm up the given state object."""
|
|
97
97
|
...
|
|
@@ -103,7 +103,7 @@ PARSERS: Dict[Text, Callable] = ...
|
|
|
103
103
|
def parse(
|
|
104
104
|
code: Text,
|
|
105
105
|
mode: Text = ...,
|
|
106
|
-
state: Optional[Command] = ...,
|
|
106
|
+
state: Optional[Command] | bool = ...,
|
|
107
107
|
keep_internal_state: Optional[bool] = None,
|
|
108
108
|
) -> Text:
|
|
109
109
|
"""Compile Coconut code."""
|
|
@@ -114,7 +114,7 @@ def coconut_exec(
|
|
|
114
114
|
expression: Text,
|
|
115
115
|
globals: Optional[Dict[Text, Any]] = None,
|
|
116
116
|
locals: Optional[Dict[Text, Any]] = None,
|
|
117
|
-
state: Optional[Command] = ...,
|
|
117
|
+
state: Optional[Command] | bool = ...,
|
|
118
118
|
keep_internal_state: Optional[bool] = None,
|
|
119
119
|
) -> None:
|
|
120
120
|
"""Compile and evaluate Coconut code."""
|
|
@@ -125,7 +125,7 @@ def coconut_eval(
|
|
|
125
125
|
expression: Text,
|
|
126
126
|
globals: Optional[Dict[Text, Any]] = None,
|
|
127
127
|
locals: Optional[Dict[Text, Any]] = None,
|
|
128
|
-
state: Optional[Command] = ...,
|
|
128
|
+
state: Optional[Command] | bool = ...,
|
|
129
129
|
keep_internal_state: Optional[bool] = None,
|
|
130
130
|
) -> Any:
|
|
131
131
|
"""Compile and evaluate Coconut code."""
|
coconut/compiler/compiler.py
CHANGED
|
@@ -632,20 +632,19 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
632
632
|
temp_var_counts = None
|
|
633
633
|
operators = None
|
|
634
634
|
|
|
635
|
-
def get_empty_scope(self):
|
|
635
|
+
def get_empty_scope(self, inner=False):
|
|
636
636
|
"""Get an empty scope for the parsing_context."""
|
|
637
|
+
parent = self.current_parsing_context("scope")
|
|
638
|
+
if parent is not None and parent["all_vars"] is None:
|
|
639
|
+
inner = True
|
|
637
640
|
return {
|
|
638
641
|
"final_vars": {},
|
|
639
642
|
"pure_vars": {},
|
|
643
|
+
"all_vars": None if inner else set(),
|
|
644
|
+
"parent": parent,
|
|
645
|
+
"callbacks": [],
|
|
640
646
|
}
|
|
641
647
|
|
|
642
|
-
def init_parsing_context(self):
|
|
643
|
-
"""Initialize parsing context."""
|
|
644
|
-
self.parsing_context = defaultdict(list)
|
|
645
|
-
# initialize module-level scopes
|
|
646
|
-
self.parsing_context["scope"].append(self.get_empty_scope())
|
|
647
|
-
return self.parsing_context
|
|
648
|
-
|
|
649
648
|
def reset(self, keep_state=False, filename=None):
|
|
650
649
|
"""Reset references.
|
|
651
650
|
|
|
@@ -680,7 +679,7 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
680
679
|
self.shown_warnings = set()
|
|
681
680
|
if not keep_state:
|
|
682
681
|
self.computation_graph_caches = defaultdict(staledict)
|
|
683
|
-
self.
|
|
682
|
+
self.parsing_context = defaultdict(list)
|
|
684
683
|
|
|
685
684
|
@contextmanager
|
|
686
685
|
def inner_environment(self, ln=None):
|
|
@@ -698,11 +697,13 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
698
697
|
num_lines, self.num_lines = self.num_lines, 0
|
|
699
698
|
remaining_original, self.remaining_original = self.remaining_original, None
|
|
700
699
|
shown_warnings, self.shown_warnings = self.shown_warnings, set()
|
|
701
|
-
parsing_context = self.parsing_context
|
|
702
|
-
self.init_parsing_context()
|
|
700
|
+
parsing_context, self.parsing_context = self.parsing_context, defaultdict(list)
|
|
703
701
|
try:
|
|
704
|
-
with
|
|
705
|
-
|
|
702
|
+
with self.add_to_parsing_context({
|
|
703
|
+
"scope": self.get_empty_scope(inner=True),
|
|
704
|
+
}):
|
|
705
|
+
with ComputationNode.using_overrides():
|
|
706
|
+
yield
|
|
706
707
|
finally:
|
|
707
708
|
self.outer_ln = outer_ln
|
|
708
709
|
self.line_numbers = line_numbers
|
|
@@ -890,20 +891,32 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
890
891
|
|
|
891
892
|
# name handlers
|
|
892
893
|
cls.refname <<= attach(cls.name_ref, cls.method("name_handle"))
|
|
893
|
-
cls.nonfinal_setname <<= attach(
|
|
894
|
+
cls.nonfinal_setname <<= attach(
|
|
895
|
+
cls.name_ref,
|
|
896
|
+
cls.method("name_handle", assign=True),
|
|
897
|
+
)
|
|
894
898
|
cls.final_setname <<= attach(
|
|
895
899
|
cls.final_setname_ref,
|
|
896
900
|
cls.method("name_handle", assign=True, is_final=True),
|
|
897
901
|
greedy=True,
|
|
898
902
|
)
|
|
903
|
+
cls.nonfinal_outer_setname <<= attach(
|
|
904
|
+
cls.name_ref,
|
|
905
|
+
cls.method("name_handle", assign=True, outer_setname=True),
|
|
906
|
+
)
|
|
907
|
+
cls.final_outer_setname <<= attach(
|
|
908
|
+
cls.final_setname_ref,
|
|
909
|
+
cls.method("name_handle", assign=True, outer_setname=True, is_final=True),
|
|
910
|
+
greedy=True,
|
|
911
|
+
)
|
|
899
912
|
cls.nonfinal_classname <<= attach(
|
|
900
913
|
cls.name_ref,
|
|
901
|
-
cls.method("name_handle", assign=True, classname=True),
|
|
914
|
+
cls.method("name_handle", assign=True, outer_setname=True, classname=True),
|
|
902
915
|
greedy=True,
|
|
903
916
|
)
|
|
904
917
|
cls.final_classname <<= attach(
|
|
905
918
|
cls.final_setname_ref,
|
|
906
|
-
cls.method("name_handle", assign=True, classname=True, is_final=True),
|
|
919
|
+
cls.method("name_handle", assign=True, outer_setname=True, classname=True, is_final=True),
|
|
907
920
|
greedy=True,
|
|
908
921
|
)
|
|
909
922
|
cls.expr_setname <<= attach(
|
|
@@ -1509,8 +1522,8 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
1509
1522
|
"""Acquire the lock and reset the parser."""
|
|
1510
1523
|
filename = None if codepath is None else os.path.basename(codepath)
|
|
1511
1524
|
with self.lock:
|
|
1512
|
-
self.reset(keep_state, filename)
|
|
1513
1525
|
Compiler.current_compiler = self
|
|
1526
|
+
self.reset(keep_state, filename)
|
|
1514
1527
|
yield
|
|
1515
1528
|
|
|
1516
1529
|
def streamline(self, grammars, inputstring=None, force=False, inner=False):
|
|
@@ -1541,10 +1554,6 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
1541
1554
|
if info["imported"] and not info["referenced"]:
|
|
1542
1555
|
for loc in info["imported"]:
|
|
1543
1556
|
self.strict_err_or_warn("found unused import " + repr(self.reformat(name, ignore_errors=True)), original, loc, noqa_able=True, endpoint=False)
|
|
1544
|
-
if not self.star_import: # only check for undefined names when there are no * imports
|
|
1545
|
-
if name not in all_builtins and info["referenced"] and not (info["assigned"] or info["imported"]):
|
|
1546
|
-
for loc in info["referenced"]:
|
|
1547
|
-
self.strict_err_or_warn("found undefined name " + repr(self.reformat(name, ignore_errors=True)), original, loc, noqa_able=True, endpoint=False)
|
|
1548
1557
|
|
|
1549
1558
|
def pickle_cache(self, original, cache_path, include_incremental=True):
|
|
1550
1559
|
"""Pickle the pyparsing cache for original to cache_path."""
|
|
@@ -1824,11 +1833,15 @@ class Compiler(Grammar, pickleable_obj):
|
|
|
1824
1833
|
with logger.gather_parsing_stats():
|
|
1825
1834
|
try:
|
|
1826
1835
|
pre_procd = self.pre(inputstring, keep_state=keep_state, **preargs)
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1836
|
+
# handle module-level scope
|
|
1837
|
+
with self.add_to_parsing_context({
|
|
1838
|
+
"scope": self.get_empty_scope(inner=keep_state),
|
|
1839
|
+
}):
|
|
1840
|
+
if isinstance(parser, tuple):
|
|
1841
|
+
init_parser, line_parser = parser
|
|
1842
|
+
parsed = self.parse_line_by_line(init_parser, line_parser, pre_procd)
|
|
1843
|
+
else:
|
|
1844
|
+
parsed = parse(parser, pre_procd, inner=False)
|
|
1832
1845
|
out = self.post(parsed, keep_state=keep_state, **postargs)
|
|
1833
1846
|
except ParseBaseException as err:
|
|
1834
1847
|
raise self.make_parse_err(err)
|
|
@@ -4058,7 +4071,7 @@ else:
|
|
|
4058
4071
|
type_ignore=self.type_ignore_comment(),
|
|
4059
4072
|
)
|
|
4060
4073
|
|
|
4061
|
-
def
|
|
4074
|
+
def make_import_stmt(self, imp_from, imp, imp_as, raw=False, lazy=False):
|
|
4062
4075
|
"""Generate an import statement."""
|
|
4063
4076
|
if not raw and imp != "*":
|
|
4064
4077
|
module_path = (imp if imp_from is None else imp_from).split(".", 1)
|
|
@@ -4075,7 +4088,7 @@ else:
|
|
|
4075
4088
|
raise _coconut.ImportError(_coconut.str(_coconut_imp_err))
|
|
4076
4089
|
""",
|
|
4077
4090
|
).format(
|
|
4078
|
-
raw_import=self.
|
|
4091
|
+
raw_import=self.make_import_stmt(imp_from, imp, imp_as, raw=True),
|
|
4079
4092
|
imp_name=imp_as if imp_as is not None else imp,
|
|
4080
4093
|
imp_lookup=".".join([existing_imp] + module_path[1:] + ([imp] if imp_from is not None else [])),
|
|
4081
4094
|
)
|
|
@@ -4121,14 +4134,14 @@ else:
|
|
|
4121
4134
|
|
|
4122
4135
|
if imp_as is not None and "." in imp_as:
|
|
4123
4136
|
import_as_var = self.get_temp_var("import", loc)
|
|
4124
|
-
out.append(self.
|
|
4137
|
+
out.append(self.make_import_stmt(imp_from, imp, import_as_var, lazy=lazy))
|
|
4125
4138
|
fake_mods = imp_as.split(".")
|
|
4126
4139
|
for i in range(1, len(fake_mods)):
|
|
4127
4140
|
mod_name = ".".join(fake_mods[:i])
|
|
4128
4141
|
out.append(self.ensure_module_or_create_fake(mod_name))
|
|
4129
4142
|
out.append(".".join(fake_mods) + " = " + import_as_var)
|
|
4130
4143
|
else:
|
|
4131
|
-
out.append(self.
|
|
4144
|
+
out.append(self.make_import_stmt(imp_from, imp, imp_as, lazy=lazy))
|
|
4132
4145
|
|
|
4133
4146
|
if type_ignore:
|
|
4134
4147
|
for i, line in enumerate(out):
|
|
@@ -4579,7 +4592,7 @@ def {name}({match_func_paramdef}):
|
|
|
4579
4592
|
|
|
4580
4593
|
self.add_code_before[name] = self.decoratable_funcdef_stmt_handle(original, loc, [decorators, funcdef], is_async, is_stmt_lambda=True)
|
|
4581
4594
|
|
|
4582
|
-
return self.
|
|
4595
|
+
return self.handle_expr_scope_closure(name, loc)
|
|
4583
4596
|
|
|
4584
4597
|
def match_comp_expr_handle(self, original, loc, tokens, dict_val=None):
|
|
4585
4598
|
"""Build a match comprehension by creating a temp match function.
|
|
@@ -4619,26 +4632,32 @@ def {func_name}({iter_var}):
|
|
|
4619
4632
|
|
|
4620
4633
|
self.add_code_before[func_name] = self.decoratable_funcdef_stmt_handle(original, loc, [funcdef], is_stmt_lambda=True)
|
|
4621
4634
|
|
|
4622
|
-
func_expr = self.
|
|
4635
|
+
func_expr = self.handle_expr_scope_closure(func_name, loc)
|
|
4623
4636
|
|
|
4624
4637
|
if dict_val is not None:
|
|
4625
4638
|
return "_coconut.dict((" + func_expr + "(" + iter_var + ") for " + iter_var + " in " + iterable + "))"
|
|
4626
4639
|
else:
|
|
4627
4640
|
return func_expr + "(" + iter_var + ") for " + iter_var + " in " + iterable
|
|
4628
4641
|
|
|
4629
|
-
def
|
|
4642
|
+
def get_parent_expr_setnames(self):
|
|
4643
|
+
"""Get all expr_setnames in parent contexts, but not the current context."""
|
|
4644
|
+
expr_setname_context = self.current_parsing_context("expr_setnames")
|
|
4645
|
+
parent_setnames = set()
|
|
4646
|
+
if expr_setname_context is not None:
|
|
4647
|
+
parent_context = expr_setname_context["parent"]
|
|
4648
|
+
while parent_context:
|
|
4649
|
+
parent_setnames |= parent_context["new_names"]
|
|
4650
|
+
parent_context = parent_context["parent"]
|
|
4651
|
+
return parent_setnames
|
|
4652
|
+
|
|
4653
|
+
def handle_expr_scope_closure(self, name, loc):
|
|
4630
4654
|
"""Extracts the definition of a name to a separate function that closes on all local expr setnames."""
|
|
4631
4655
|
expr_setname_context = self.current_parsing_context("expr_setnames")
|
|
4632
4656
|
if expr_setname_context is None:
|
|
4633
4657
|
return name
|
|
4634
4658
|
else:
|
|
4635
4659
|
builder_name = self.get_temp_var("lambda_builder", loc)
|
|
4636
|
-
|
|
4637
|
-
parent_context = expr_setname_context["parent"]
|
|
4638
|
-
parent_setnames = set()
|
|
4639
|
-
while parent_context:
|
|
4640
|
-
parent_setnames |= parent_context["new_names"]
|
|
4641
|
-
parent_context = parent_context["parent"]
|
|
4660
|
+
parent_setnames = self.get_parent_expr_setnames()
|
|
4642
4661
|
|
|
4643
4662
|
def stmt_lambdef_callback():
|
|
4644
4663
|
expr_setnames = parent_setnames | expr_setname_context["new_names"]
|
|
@@ -5284,7 +5303,7 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5284
5303
|
return default
|
|
5285
5304
|
|
|
5286
5305
|
@contextmanager
|
|
5287
|
-
def add_to_parsing_context(self, name_obj_dict
|
|
5306
|
+
def add_to_parsing_context(self, name_obj_dict):
|
|
5288
5307
|
"""Put all the given objects on their respective parsing context stacks."""
|
|
5289
5308
|
for name, obj in name_obj_dict.items():
|
|
5290
5309
|
self.parsing_context[name].append(obj)
|
|
@@ -5293,11 +5312,9 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5293
5312
|
finally:
|
|
5294
5313
|
for name in name_obj_dict:
|
|
5295
5314
|
popped_ctx = self.parsing_context[name].pop()
|
|
5296
|
-
if
|
|
5297
|
-
|
|
5298
|
-
|
|
5299
|
-
for callback in popped_ctx[callbacks_key]:
|
|
5300
|
-
callback()
|
|
5315
|
+
if "callbacks" in popped_ctx:
|
|
5316
|
+
for callback in popped_ctx["callbacks"]:
|
|
5317
|
+
callback()
|
|
5301
5318
|
|
|
5302
5319
|
def funcname_typeparams_handle(self, tokens):
|
|
5303
5320
|
"""Handle function names with type parameters."""
|
|
@@ -5565,16 +5582,17 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5565
5582
|
"callbacks": [],
|
|
5566
5583
|
"loc": loc,
|
|
5567
5584
|
}},
|
|
5568
|
-
callbacks_keys_dict={"expr_setnames": "callbacks"},
|
|
5569
5585
|
):
|
|
5570
5586
|
yield
|
|
5571
5587
|
|
|
5572
|
-
def name_handle(self, original, loc, tokens, assign=False,
|
|
5588
|
+
def name_handle(self, original, loc, tokens, assign=False, outer_setname=False, expr_setname=False, classname=False, is_final=False):
|
|
5573
5589
|
"""Handle the given base name."""
|
|
5574
|
-
if
|
|
5575
|
-
internal_assert(
|
|
5590
|
+
if classname:
|
|
5591
|
+
internal_assert(outer_setname and not expr_setname, "classname should always imply outer_setname", tokens)
|
|
5592
|
+
if outer_setname or expr_setname:
|
|
5593
|
+
internal_assert(assign, "classname/funcname/expr_setname should always imply assign", tokens)
|
|
5576
5594
|
if is_final:
|
|
5577
|
-
internal_assert(assign and not expr_setname, "only setnames should ever be final",
|
|
5595
|
+
internal_assert(assign and not expr_setname, "only setnames should ever be final", tokens)
|
|
5578
5596
|
|
|
5579
5597
|
name, = tokens
|
|
5580
5598
|
|
|
@@ -5595,24 +5613,6 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5595
5613
|
always_wrap=is_greedy,
|
|
5596
5614
|
)
|
|
5597
5615
|
|
|
5598
|
-
# register non-mid-expression variable assignments inside of where statements for later mangling
|
|
5599
|
-
if assign and not expr_setname:
|
|
5600
|
-
where_context = self.current_parsing_context("where")
|
|
5601
|
-
if where_context is not None:
|
|
5602
|
-
where_assigns = where_context["assigns"]
|
|
5603
|
-
if where_assigns is not None:
|
|
5604
|
-
where_assigns.add(name)
|
|
5605
|
-
|
|
5606
|
-
if classname:
|
|
5607
|
-
cls_context = self.current_parsing_context("class")
|
|
5608
|
-
self.internal_assert(cls_context is not None, original, loc, "found classname outside of class", tokens)
|
|
5609
|
-
cls_context["name"] = name
|
|
5610
|
-
|
|
5611
|
-
if expr_setname:
|
|
5612
|
-
expr_setnames_context = self.current_parsing_context("expr_setnames")
|
|
5613
|
-
self.internal_assert(expr_setnames_context is not None, original, loc, "found expr_setname outside of has_expr_setname_manage", tokens)
|
|
5614
|
-
expr_setnames_context["new_names"].add(name)
|
|
5615
|
-
|
|
5616
5616
|
if not escaped:
|
|
5617
5617
|
typevar_info = self.current_parsing_context("typevars")
|
|
5618
5618
|
if typevar_info is not None:
|
|
@@ -5633,18 +5633,45 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5633
5633
|
# note that this is the one case where we return early that isn't an error
|
|
5634
5634
|
return typevars[name]
|
|
5635
5635
|
|
|
5636
|
+
# register non-mid-expression variable assignments inside of where statements for later mangling
|
|
5637
|
+
if assign and not expr_setname:
|
|
5638
|
+
where_context = self.current_parsing_context("where")
|
|
5639
|
+
if where_context is not None:
|
|
5640
|
+
where_assigns = where_context["assigns"]
|
|
5641
|
+
if where_assigns is not None:
|
|
5642
|
+
where_assigns.add(name)
|
|
5643
|
+
|
|
5644
|
+
cls_context = self.current_parsing_context("class")
|
|
5645
|
+
expr_setnames_context = self.current_parsing_context("expr_setnames")
|
|
5646
|
+
|
|
5647
|
+
if classname:
|
|
5648
|
+
self.internal_assert(cls_context is not None, original, loc, "found classname outside of class", tokens)
|
|
5649
|
+
cls_context["name"] = name
|
|
5650
|
+
|
|
5651
|
+
if expr_setname:
|
|
5652
|
+
self.internal_assert(expr_setnames_context is not None, original, loc, "found expr_setname outside of has_expr_setname_manage", tokens)
|
|
5653
|
+
expr_setnames_context["new_names"].add(name)
|
|
5654
|
+
|
|
5655
|
+
scope = self.current_parsing_context("scope")
|
|
5656
|
+
self.internal_assert(scope is not None, original, loc, "no scope context")
|
|
5657
|
+
|
|
5636
5658
|
# track assigned/referenced early so that even if we return early with an
|
|
5637
5659
|
# error, the name is marked as used (e.g. for unused import checking)
|
|
5638
5660
|
if assign:
|
|
5639
5661
|
is_new = loc not in self.name_info[name]["assigned"]
|
|
5640
5662
|
self.name_info[name]["assigned"].add(loc)
|
|
5663
|
+
if (
|
|
5664
|
+
outer_setname
|
|
5665
|
+
and scope["parent"] is not None
|
|
5666
|
+
and scope["parent"]["all_vars"] is not None
|
|
5667
|
+
):
|
|
5668
|
+
scope["parent"]["all_vars"].add(name)
|
|
5669
|
+
elif not expr_setname and scope["all_vars"] is not None:
|
|
5670
|
+
scope["all_vars"].add(name)
|
|
5641
5671
|
else:
|
|
5642
5672
|
is_new = loc not in self.name_info[name]["referenced"]
|
|
5643
5673
|
self.name_info[name]["referenced"].add(loc)
|
|
5644
5674
|
|
|
5645
|
-
scope = self.current_parsing_context("scope")
|
|
5646
|
-
self.internal_assert(scope is not None, original, loc, "no scope context")
|
|
5647
|
-
|
|
5648
5675
|
# final variable checking (setting final_vars happens at the end)
|
|
5649
5676
|
final_vars = scope["final_vars"]
|
|
5650
5677
|
if (
|
|
@@ -5693,7 +5720,8 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5693
5720
|
return err
|
|
5694
5721
|
|
|
5695
5722
|
is_class_attr = (
|
|
5696
|
-
|
|
5723
|
+
assign
|
|
5724
|
+
and cls_context
|
|
5697
5725
|
and not self.in_method
|
|
5698
5726
|
and (
|
|
5699
5727
|
# for classnames, we need special handling for nested classes
|
|
@@ -5712,14 +5740,14 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5712
5740
|
if (
|
|
5713
5741
|
assign
|
|
5714
5742
|
and not escaped
|
|
5715
|
-
# if we're creating a class attribute, then we're not actually shadowing anything
|
|
5716
|
-
and not is_class_attr
|
|
5717
5743
|
# if we're not using the computation graph, then name is handled
|
|
5718
5744
|
# greedily, which means this might be an invalid parse, in which
|
|
5719
5745
|
# case we can't be sure this is actually shadowing a builtin;
|
|
5720
5746
|
# BUT if we're on strict mode, then it's an actual error, rather
|
|
5721
5747
|
# than a warning, which means we can just wrap it
|
|
5722
5748
|
and safe_to_show_warnings
|
|
5749
|
+
# if we're creating a class attribute, then we're not actually shadowing anything
|
|
5750
|
+
and not is_class_attr
|
|
5723
5751
|
):
|
|
5724
5752
|
if name in all_builtins:
|
|
5725
5753
|
err = self.strict_err_or_warn(
|
|
@@ -5735,7 +5763,7 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5735
5763
|
# the import statement itself, not an assignment shadowing an import
|
|
5736
5764
|
if any(not same_line(original, loc, imp_loc) for imp_loc in self.name_info[name]["imported"]):
|
|
5737
5765
|
err = self.strict_err_or_warn(
|
|
5738
|
-
"assignment shadows imported
|
|
5766
|
+
"assignment shadows imported '{name}' (use explicit '\\{name}' syntax when purposefully redefining imported names)".format(name=name),
|
|
5739
5767
|
original,
|
|
5740
5768
|
loc,
|
|
5741
5769
|
raise_err_func=local_raise_or_wrap_error,
|
|
@@ -5743,6 +5771,25 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5743
5771
|
if err is not None:
|
|
5744
5772
|
return err
|
|
5745
5773
|
|
|
5774
|
+
# undefined name checking
|
|
5775
|
+
if (
|
|
5776
|
+
not assign
|
|
5777
|
+
and not escaped
|
|
5778
|
+
and safe_to_show_warnings
|
|
5779
|
+
and not self.star_import
|
|
5780
|
+
and scope["all_vars"] is not None
|
|
5781
|
+
and name not in all_builtins
|
|
5782
|
+
and name != wildcard
|
|
5783
|
+
and (expr_setnames_context is None or (
|
|
5784
|
+
name not in expr_setnames_context["new_names"]
|
|
5785
|
+
and name not in self.get_parent_expr_setnames()
|
|
5786
|
+
))
|
|
5787
|
+
):
|
|
5788
|
+
parent_scope = scope
|
|
5789
|
+
while parent_scope["parent"] is not None:
|
|
5790
|
+
parent_scope = parent_scope["parent"]
|
|
5791
|
+
parent_scope["callbacks"].append(partial(self.check_undefined_name, original, loc, name, scope, self.outer_ln))
|
|
5792
|
+
|
|
5746
5793
|
# only mark as final after all checks pass
|
|
5747
5794
|
if is_final:
|
|
5748
5795
|
final_vars[name] = loc
|
|
@@ -5772,7 +5819,6 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5772
5819
|
return "_coconut_exec"
|
|
5773
5820
|
elif not assign and name in super_names and not self.target.startswith("3"):
|
|
5774
5821
|
if self.in_method:
|
|
5775
|
-
cls_context = self.current_parsing_context("class")
|
|
5776
5822
|
enclosing_cls = cls_context["name_prefix"] + cls_context["name"]
|
|
5777
5823
|
return self.add_code_before_marker_with_replacement(name, "__class__ = " + enclosing_cls + "\n", add_spaces=False)
|
|
5778
5824
|
else:
|
|
@@ -5787,6 +5833,25 @@ class {protocol_var}({tokens}, _coconut.typing.Protocol): pass
|
|
|
5787
5833
|
else:
|
|
5788
5834
|
return name
|
|
5789
5835
|
|
|
5836
|
+
def check_undefined_name(self, original, loc, name, scope, outer_ln):
|
|
5837
|
+
"""Run a deferred check for name being undefined in the scope chain.
|
|
5838
|
+
|
|
5839
|
+
We need outer_ln=self.outer_ln to be put in the partial so we know what
|
|
5840
|
+
the outer_ln was when this check was set up."""
|
|
5841
|
+
current_scope = scope
|
|
5842
|
+
while current_scope is not None:
|
|
5843
|
+
if current_scope["all_vars"] is None or name in current_scope["all_vars"]:
|
|
5844
|
+
return
|
|
5845
|
+
current_scope = current_scope["parent"]
|
|
5846
|
+
self.strict_err_or_warn(
|
|
5847
|
+
"found undefined name '{name}' (use explicit '\\{name}' syntax to bypass)".format(name=name),
|
|
5848
|
+
original,
|
|
5849
|
+
loc,
|
|
5850
|
+
ln=outer_ln,
|
|
5851
|
+
noqa_able=True,
|
|
5852
|
+
endpoint=False,
|
|
5853
|
+
)
|
|
5854
|
+
|
|
5790
5855
|
# end: MANAGERS
|
|
5791
5856
|
# -----------------------------------------------------------------------------------------------------------------------
|
|
5792
5857
|
# CHECKING HANDLERS:
|
coconut/compiler/grammar.py
CHANGED
|
@@ -861,18 +861,23 @@ class Grammar(object):
|
|
|
861
861
|
expr_setname = Forward()
|
|
862
862
|
final_setname = Forward()
|
|
863
863
|
nonfinal_setname = Forward()
|
|
864
|
+
# classname and outer_setname define their vars in the outer scope
|
|
864
865
|
final_classname = Forward()
|
|
865
866
|
nonfinal_classname = Forward()
|
|
867
|
+
final_outer_setname = Forward()
|
|
868
|
+
nonfinal_outer_setname = Forward()
|
|
866
869
|
|
|
867
870
|
name_ref = combine(Optional(backslash) + base_name)
|
|
868
871
|
final_setname_ref = keyword("final").suppress() + name_ref
|
|
869
872
|
setname = final_setname | nonfinal_setname
|
|
870
873
|
classname = final_classname | nonfinal_classname
|
|
874
|
+
outer_setname = final_outer_setname | nonfinal_outer_setname
|
|
871
875
|
unsafe_name = combine(Optional(backslash.suppress()) + base_name)
|
|
872
876
|
|
|
873
877
|
# use unsafe_name for dotted components since name should only be used for base names
|
|
874
878
|
dotted_refname = condense(refname + ZeroOrMore(dot + unsafe_name))
|
|
875
879
|
dotted_setname = condense(setname + ZeroOrMore(dot + unsafe_name))
|
|
880
|
+
dotted_outer_setname = condense(outer_setname + ZeroOrMore(dot + unsafe_name))
|
|
876
881
|
unsafe_dotted_name = condense(unsafe_name + ZeroOrMore(dot + unsafe_name))
|
|
877
882
|
must_be_dotted_name = condense(refname + OneOrMore(dot + unsafe_name))
|
|
878
883
|
|
|
@@ -1566,7 +1571,7 @@ class Grammar(object):
|
|
|
1566
1571
|
type_params = Group(lbrack.suppress() + tokenlist(type_param, comma) + rbrack.suppress())
|
|
1567
1572
|
|
|
1568
1573
|
type_alias_stmt = Forward()
|
|
1569
|
-
type_alias_stmt_ref = keyword("type").suppress() +
|
|
1574
|
+
type_alias_stmt_ref = keyword("type").suppress() + outer_setname + Optional(type_params) + equals.suppress() + typedef_test
|
|
1570
1575
|
|
|
1571
1576
|
await_expr = Forward()
|
|
1572
1577
|
await_expr_ref = keyword("await").suppress() + atom_item
|
|
@@ -2310,7 +2315,7 @@ class Grammar(object):
|
|
|
2310
2315
|
with_stmt = Forward()
|
|
2311
2316
|
|
|
2312
2317
|
funcname_typeparams = Forward()
|
|
2313
|
-
funcname_typeparams_tokens =
|
|
2318
|
+
funcname_typeparams_tokens = dotted_outer_setname + Optional(type_params)
|
|
2314
2319
|
name_funcdef = condense(funcname_typeparams + parameters)
|
|
2315
2320
|
op_tfpdef = unsafe_typedef_default | condense(setname + Optional(default))
|
|
2316
2321
|
op_funcdef_arg = setname | condense(lparen.suppress() + op_tfpdef + rparen.suppress())
|
coconut/constants.py
CHANGED
|
@@ -119,6 +119,7 @@ py_version_str = sys.version.split()[0]
|
|
|
119
119
|
use_fast_pyparsing_reprs = get_bool_env_var("COCONUT_FAST_PYPARSING_REPRS", True)
|
|
120
120
|
|
|
121
121
|
warn_on_multiline_regex = False
|
|
122
|
+
warn_on_undefined_ParseResults_name = False
|
|
122
123
|
|
|
123
124
|
default_whitespace_chars = " \t\f" # the only non-newline whitespace Python allows
|
|
124
125
|
|
|
@@ -844,6 +845,7 @@ coconut_specific_builtins = (
|
|
|
844
845
|
"and_then",
|
|
845
846
|
"and_then_await",
|
|
846
847
|
"async_map",
|
|
848
|
+
"py_bytes",
|
|
847
849
|
"py_chr",
|
|
848
850
|
"py_dict",
|
|
849
851
|
"py_hex",
|
coconut/root.py
CHANGED
|
@@ -26,7 +26,7 @@ import sys as _coconut_sys
|
|
|
26
26
|
VERSION = "3.2.0"
|
|
27
27
|
VERSION_NAME = None
|
|
28
28
|
# False for release, int >= 1 for develop
|
|
29
|
-
DEVELOP =
|
|
29
|
+
DEVELOP = 16
|
|
30
30
|
ALPHA = False # for pre releases rather than post releases
|
|
31
31
|
|
|
32
32
|
assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
|
|
@@ -445,10 +445,10 @@ def primary_test_1() -> bool:
|
|
|
445
445
|
price = 100
|
|
446
446
|
assert 0 == (requested_quantity ?? default_quantity) * price
|
|
447
447
|
assert range(10) |> .[1] .. .[1:] == 2 == range(10) |> .[1:] |> .[1]
|
|
448
|
-
assert None?.herp(derp) is None # type: ignore
|
|
449
|
-
assert None?[herp].derp is None # type: ignore
|
|
450
|
-
assert None?(derp)[herp] is None # type: ignore
|
|
451
|
-
assert None?$(herp)(derp) is None # type: ignore
|
|
448
|
+
assert None?.herp(derp) is None # type: ignore # NOQA
|
|
449
|
+
assert None?[herp].derp is None # type: ignore # NOQA
|
|
450
|
+
assert None?(derp)[herp] is None # type: ignore # NOQA
|
|
451
|
+
assert None?$(herp)(derp) is None # type: ignore # NOQA
|
|
452
452
|
a: int[]? = None # type: ignore
|
|
453
453
|
assert a is None
|
|
454
454
|
assert range(5) |> iter |> reiterable |> .[1] == 1
|
|
@@ -962,7 +962,7 @@ def primary_test_1() -> bool:
|
|
|
962
962
|
def ret_abc():
|
|
963
963
|
return "abc"
|
|
964
964
|
)
|
|
965
|
-
assert ret_abc() == "abc"
|
|
965
|
+
assert ret_abc() == "abc" # NOQA
|
|
966
966
|
assert """" """ == '" '
|
|
967
967
|
assert "" == """"""
|
|
968
968
|
assert (,)(*(1, 2), 3) == (1, 2, 3)
|