Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,9 @@ impl SymbolTableAnalyzer {

// Analyze symbols in current scope
let function_like_scope = SymbolTableBuilder::is_function_like_scope(symbol_table.typ);
// Class implicit cells satisfy child requests, not this scope's own
// closure. Keep its free names out of drop_class_free().
let mut scope_free = IndexSet::default();
Comment thread
1ndahous3 marked this conversation as resolved.
for symbol in symbol_table.symbols.values_mut() {
self.analyze_symbol(
symbol,
Expand All @@ -760,7 +763,7 @@ impl SymbolTableAnalyzer {
if symbol.scope == SymbolScope::Free
|| symbol.flags.contains(SymbolFlags::DEF_FREE_CLASS)
{
newfree.insert(symbol.name.clone());
scope_free.insert(symbol.name.clone());
}
}

Expand Down Expand Up @@ -797,6 +800,7 @@ impl SymbolTableAnalyzer {
}
}

newfree.extend(scope_free);
Ok(newfree)
}

Expand Down
27 changes: 27 additions & 0 deletions extra_tests/snippets/syntax_annotations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import get_type_hints

from testutils import assert_raises

# Future f-string annotations canonicalize fields, not their literal padding.
for formatted, expected in (
("f'{ value }'", "f'{value}'"),
Expand Down Expand Up @@ -33,3 +35,28 @@ def func(s: str) -> int:
"from __future__ import annotations\nx: " + template, "<test>", "exec"
)
assert expected in annotation_code.co_consts


# Conditional annotations belong to the inner class, not an enclosing scope.
for future in ("", "from __future__ import annotations\n"):
Comment thread
1ndahous3 marked this conversation as resolved.
namespace = {}
exec(
future
+ """
class Outer:
class Inner:
if True:
included: int
if False:
omitted: str
""",
namespace,
)
assert get_type_hints(namespace["Outer"].Inner) == {"included": int}

with assert_raises(SyntaxError):
compile(
"class C:\n if True: x: int\n global __conditional_annotations__",
"<test>",
"exec",
)
Loading