Skip to content
Prev Previous commit
Next Next commit
Correct type return type
  • Loading branch information
ShaharNaveh committed Aug 1, 2025
commit e66d264492c270f0d4255681f79b43e0769908d2
6 changes: 0 additions & 6 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ def test_id(self):
self.assertGreater(self.generic_spam.get_id(), 0)
self.assertGreater(self.GenericMine.get_id(), 0)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_optimized(self):
self.assertFalse(self.top.is_optimized())

Expand Down Expand Up @@ -505,8 +503,6 @@ def test_bytes(self):
top = symtable.symtable(code, "?", "exec")
self.assertIsNotNone(find_block(top, "\u017d"))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_symtable_repr(self):
self.assertEqual(str(self.top), "<SymbolTable for module ?>")
self.assertEqual(str(self.spam), "<Function SymbolTable for spam in ?>")
Expand Down Expand Up @@ -558,8 +554,6 @@ def test_file(self):
self.assertIn(" local symbol 'spam': def_local", lines)
self.assertIn(" symbol table for function 'spam':", lines)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_stdin(self):
with support.captured_stdin() as stdin:
stdin.write(TEST_CODE)
Expand Down
19 changes: 15 additions & 4 deletions vm/src/stdlib/symtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ mod symtable {
builtins::{PyDictRef, PyStrRef},
compiler,
};
use rustpython_codegen::symboltable::{Symbol, SymbolFlags, SymbolScope, SymbolTable};
use rustpython_codegen::symboltable::{
CompilerScope, Symbol, SymbolFlags, SymbolScope, SymbolTable,
};
use std::fmt;

// Consts as defined at
Expand Down Expand Up @@ -101,7 +103,10 @@ mod symtable {
pub const TYPE_TYPE_ALIAS: i32 = 5;

#[pyattr]
pub const TYPE_TYPE_PARAM: i32 = 6;
pub const TYPE_TYPE_PARAMETERS: i32 = 6;

#[pyattr]
pub const TYPE_TYPE_VARIABLE: i32 = 7;

#[pyfunction]
fn symtable(
Expand Down Expand Up @@ -147,8 +152,14 @@ mod symtable {
}

#[pygetset(name = "type")]
fn typ(&self) -> String {
self.symtable.typ.to_string()
fn typ(&self) -> i32 {
match self.symtable.typ {
CompilerScope::Function => TYPE_FUNCTION,
CompilerScope::Class => TYPE_CLASS,
CompilerScope::Module => TYPE_MODULE,
CompilerScope::TypeParams => TYPE_TYPE_PARAMETERS,
_ => -1, // TODO: missing types from the C implementation
}
}
Comment on lines +154 to 163
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify integer constant mapping for type property.

The typ() method now returns integer constants instead of strings, which is a significant interface change. The mapping looks reasonable, but the fallback to -1 for unhandled cases may not align with CPython's behavior.


🏁 Script executed:

#!/bin/bash
# Check CPython's SymbolTable.get_type() return values
python3 -c "
import symtable
st = symtable.symtable('def f(): pass', '<string>', 'exec')
print('Module type:', repr(st.get_type()))
for child in st.get_children():
    print('Function type:', repr(child.get_type()))
"

Length of output: 297


Ensure type property matches CPython’s string-based API

CPython’s SymbolTable.get_type() returns strings like "module" and "function", not integer codes. Our Rust typ() currently returns integer constants and falls back to -1 for any unhandled cases, which:

  • Breaks compatibility with existing users expecting string values
  • Omits several type names (e.g. comprehensions, lambdas, generators) that CPython exposes

Please update the implementation to either:

  • Return Python strings matching CPython’s names, or
  • If you intentionally switch to integers, document the change clearly and replace the -1 fallback with a dedicated TYPE_UNKNOWN constant—and add mappings for all types in CPython’s C implementation.

Points to address:

  • Decide on integer vs. string return type and update docs accordingly
  • Remove the // TODO and implement mappings for missing CPython types (generator, lambda, comprehension, etc.)
  • Eliminate the -1 fallback or replace it with a well-defined default constant
🤖 Prompt for AI Agents
In vm/src/stdlib/symtable.rs lines 154 to 163, the typ() function currently
returns integer constants and uses -1 as a fallback, which breaks compatibility
with CPython's string-based SymbolTable.get_type() API and omits several type
names. To fix this, change the return type to Python strings matching CPython's
type names (e.g., "module", "function", "class", "generator", "lambda",
"comprehension"), remove the -1 fallback, and implement mappings for all missing
CPython types. Also update any related documentation to reflect the string
return type and remove the TODO comment.


#[pygetset]
Expand Down
Loading